|
@@ -0,0 +1,780 @@
|
|
|
+package com.gis.common.util;
|
|
|
+
|
|
|
+import cn.hutool.core.date.DateUtil;
|
|
|
+import cn.hutool.core.img.Img;
|
|
|
+import cn.hutool.core.io.FileUtil;
|
|
|
+import cn.hutool.core.util.URLUtil;
|
|
|
+import com.gis.common.task.AsyncTask;
|
|
|
+import lombok.extern.log4j.Log4j2;
|
|
|
+import org.apache.commons.lang3.StringUtils;
|
|
|
+import org.junit.Test;
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.core.io.ClassPathResource;
|
|
|
+import org.springframework.stereotype.Component;
|
|
|
+import org.springframework.util.ResourceUtils;
|
|
|
+import org.springframework.web.multipart.MultipartFile;
|
|
|
+
|
|
|
+import javax.imageio.ImageIO;
|
|
|
+import java.awt.image.BufferedImage;
|
|
|
+import java.io.*;
|
|
|
+import java.net.HttpURLConnection;
|
|
|
+import java.net.URL;
|
|
|
+import java.net.URLDecoder;
|
|
|
+import java.util.*;
|
|
|
+
|
|
|
+/**
|
|
|
+ * Created by owen on 2020/5/12 0012 17:21
|
|
|
+ */
|
|
|
+@Log4j2
|
|
|
+@Component
|
|
|
+public class FileUtils {
|
|
|
+
|
|
|
+ /** 临时文件存放目录*/
|
|
|
+ private final static String PROJECT_NAME = "cms_pano_fcb_dev_data/";
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ AliyunOssUtil aliyunOssUtil;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ AsyncTask asyncTask;
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 多文件上传
|
|
|
+ * savePath 路径(目录)
|
|
|
+ */
|
|
|
+ public static List<Map<String, String>> uploads(MultipartFile[] files, String savePath) throws IOException {
|
|
|
+ if (files == null) {
|
|
|
+ log.error("文件不能为空");
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+ String time = DateUtil.format(new Date(), "yyyyMMdd_HHmmssSSS");
|
|
|
+ savePath = savePath + time + File.separator;
|
|
|
+// FileUtil.mkdir(time);
|
|
|
+
|
|
|
+ ArrayList<Map<String, String>> list = new ArrayList<>();
|
|
|
+
|
|
|
+ String path;
|
|
|
+ int i = 0;
|
|
|
+ for (MultipartFile file : files) {
|
|
|
+ String fileName = file.getOriginalFilename();
|
|
|
+ String suffix = StringUtils.substringAfterLast(fileName, ".");
|
|
|
+ String newName = time + "_" + i + "." +suffix;
|
|
|
+ path = savePath + newName;
|
|
|
+ FileUtil.writeFromStream(file.getInputStream(), path);
|
|
|
+
|
|
|
+ HashMap<String, String> fileInfo = new HashMap<>();
|
|
|
+ fileInfo.put("path", path);
|
|
|
+ fileInfo.put("name", fileName);
|
|
|
+ fileInfo.put("newName", newName);
|
|
|
+ fileInfo.put("dir", time);
|
|
|
+
|
|
|
+ list.add(fileInfo);
|
|
|
+ ++ i;
|
|
|
+ }
|
|
|
+
|
|
|
+ return list;
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 从网络Url中下载文件
|
|
|
+ *
|
|
|
+ * @param urlStr
|
|
|
+ * @param fileName
|
|
|
+ * @param savePath
|
|
|
+ * @return
|
|
|
+ * @throws IOException
|
|
|
+ */
|
|
|
+ public static boolean downLoadFromUrl(String urlStr, String fileName, String savePath){
|
|
|
+ FileOutputStream fos = null;
|
|
|
+ InputStream inputStream = null;
|
|
|
+ try {
|
|
|
+ URL url = new URL(urlStr);
|
|
|
+ HttpURLConnection conn = (HttpURLConnection) url.openConnection();
|
|
|
+ // 设置超时间为3秒
|
|
|
+ conn.setConnectTimeout(3 * 1000);
|
|
|
+ // 防止屏蔽程序抓取而返回403错误
|
|
|
+ conn.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 5.0; Windows NT; DigExt)");
|
|
|
+
|
|
|
+ // 得到输入流
|
|
|
+ inputStream = conn.getInputStream();
|
|
|
+ // 获取自己数组
|
|
|
+ byte[] getData = readInputStream(inputStream);
|
|
|
+
|
|
|
+ // 文件保存位置
|
|
|
+ File saveDir = new File(savePath);
|
|
|
+ if (!saveDir.exists()) {
|
|
|
+ saveDir.mkdirs();
|
|
|
+ }
|
|
|
+ String filePath = saveDir + File.separator + fileName;
|
|
|
+ String filePathFolder = filePath.substring(0, filePath.lastIndexOf("/") + 1);
|
|
|
+ FileUtil.mkdir(filePathFolder);
|
|
|
+
|
|
|
+ File file = new File(filePath);
|
|
|
+ fos = new FileOutputStream(file);
|
|
|
+ fos.write(getData);
|
|
|
+ if (fos != null) {
|
|
|
+ fos.close();
|
|
|
+ }
|
|
|
+ if (inputStream != null) {
|
|
|
+ inputStream.close();
|
|
|
+ }
|
|
|
+ System.out.println("info:" + url + " download success");
|
|
|
+ } catch(FileNotFoundException e){
|
|
|
+ return false;
|
|
|
+ } catch (IOException e) {
|
|
|
+ return false;
|
|
|
+ }finally {
|
|
|
+ if (fos != null) {
|
|
|
+ try {
|
|
|
+ fos.close();
|
|
|
+ } catch (IOException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if (inputStream != null) {
|
|
|
+ try {
|
|
|
+ inputStream.close();
|
|
|
+ } catch (IOException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 从输入流中获取字节数组
|
|
|
+ *
|
|
|
+ * @param inputStream
|
|
|
+ * @return
|
|
|
+ * @throws IOException
|
|
|
+ */
|
|
|
+ private static byte[] readInputStream(InputStream inputStream) throws IOException {
|
|
|
+ byte[] buffer = new byte[1024];
|
|
|
+ int len = 0;
|
|
|
+ ByteArrayOutputStream bos = new ByteArrayOutputStream();
|
|
|
+ while ((len = inputStream.read(buffer)) != -1) {
|
|
|
+ bos.write(buffer, 0, len);
|
|
|
+ }
|
|
|
+ bos.close();
|
|
|
+ return bos.toByteArray();
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 重命名文件上传
|
|
|
+ * @param file
|
|
|
+ * @param savePath
|
|
|
+ * @return
|
|
|
+ * @throws IOException
|
|
|
+ */
|
|
|
+ public static String renameUpload(MultipartFile file, String savePath, String baseUrl) {
|
|
|
+ if (file == null) {
|
|
|
+ log.error("文件不能为空");
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+ String time = DateUtil.format(new Date(), "yyyyMMdd_HHmmssSSS");
|
|
|
+
|
|
|
+ String fileName = file.getOriginalFilename();
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ String suffix = StringUtils.substringAfterLast(fileName, ".");
|
|
|
+ String newName = time + "." +suffix;
|
|
|
+ savePath = savePath + newName;
|
|
|
+
|
|
|
+ try {
|
|
|
+ FileUtil.writeFromStream(file.getInputStream(), savePath);
|
|
|
+ } catch (IOException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+
|
|
|
+ String path = StringUtils.substringAfterLast(savePath, PROJECT_NAME);
|
|
|
+
|
|
|
+ return baseUrl+ PROJECT_NAME +path;
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ /***
|
|
|
+ * 文件上传oss
|
|
|
+ * @param file
|
|
|
+ * @param savePath 服务器保存地址
|
|
|
+ * @param ossBasePath oss目录地址
|
|
|
+ * @param ossDomain oss域名
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public Object renameUploadOss(MultipartFile file, String savePath, String ossBasePath, String ossDomain) {
|
|
|
+
|
|
|
+ if (file == null) {
|
|
|
+ log.error("文件不能为空");
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+ long size = file.getSize();
|
|
|
+ log.info("文件大小:" + size );
|
|
|
+ log.info("文件大小:" + (size/1000) + "kb");
|
|
|
+
|
|
|
+
|
|
|
+ String time = DateUtil.format(new Date(), "yyyyMMdd_HHmmssSSS");
|
|
|
+
|
|
|
+ String fileName = file.getOriginalFilename();
|
|
|
+ log.info("上传的文件名:" + fileName);
|
|
|
+
|
|
|
+ String fileType = getFileType(fileName);
|
|
|
+ String dirType = "image/";
|
|
|
+ if (fileType.equals("doc")) {
|
|
|
+ dirType = "doc/";
|
|
|
+ }
|
|
|
+
|
|
|
+ String suffix = StringUtils.substringAfterLast(fileName, ".");
|
|
|
+ String newName = time + "." +suffix;
|
|
|
+ savePath = savePath + dirType + newName;
|
|
|
+
|
|
|
+ log.info("savePath: {}", savePath);
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ try {
|
|
|
+ FileUtil.writeFromStream(file.getInputStream(), savePath);
|
|
|
+ } catch (IOException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+
|
|
|
+ // 上传oss
|
|
|
+ String ossPath = ossBasePath + dirType + newName;
|
|
|
+ aliyunOssUtil.upload(savePath, ossPath);
|
|
|
+
|
|
|
+ String ossUrl = ossDomain + ossPath + "?d=" + System.currentTimeMillis();
|
|
|
+ log.info("ossUrl: {}", ossUrl);
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ return ossUrl;
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 文件流上传,不存本地服务器,直接上传oss
|
|
|
+ * @param file
|
|
|
+ * @param ossBasePath
|
|
|
+ * @param ossDomain
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public Object renameUploadOssBye(MultipartFile file, String ossBasePath, String ossDomain) {
|
|
|
+
|
|
|
+ if (file == null) {
|
|
|
+ log.error("文件不能为空");
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+ long size = file.getSize();
|
|
|
+ log.info("文件大小:" + size );
|
|
|
+ log.info("文件大小:" + (size/1000) + "kb");
|
|
|
+
|
|
|
+
|
|
|
+ String time = DateUtil.format(new Date(), "yyyyMMdd_HHmmssSSS");
|
|
|
+
|
|
|
+ String fileName = file.getOriginalFilename();
|
|
|
+ log.info("上传的文件名:" + fileName);
|
|
|
+
|
|
|
+ String fileType = getFileType(fileName);
|
|
|
+ String dirType = "image/";
|
|
|
+ if (fileType.equals("doc")) {
|
|
|
+ dirType = "doc/";
|
|
|
+ }
|
|
|
+
|
|
|
+ String suffix = StringUtils.substringAfterLast(fileName, ".");
|
|
|
+ String newName = time + "." +suffix;
|
|
|
+
|
|
|
+ // 上传oss
|
|
|
+ String ossPath = ossBasePath + dirType + newName;
|
|
|
+ try {
|
|
|
+ aliyunOssUtil.upload(file.getBytes(), ossPath);
|
|
|
+ } catch (IOException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+
|
|
|
+ String ossUrl = ossDomain + ossPath;
|
|
|
+ log.info("ossUrl: {}", ossUrl);
|
|
|
+
|
|
|
+ return ossUrl;
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ /***
|
|
|
+ * 文件上传oss, 返回文件名跟地址
|
|
|
+ * @param file
|
|
|
+ * @param saveBasePath 服务器保存地址
|
|
|
+ * @param ossBasePath oss目录地址
|
|
|
+ * @param ossDomain oss域名
|
|
|
+ * @return 文件名,跟地址
|
|
|
+ */
|
|
|
+ public Map renameUploadOssMap(MultipartFile file, String saveBasePath, String ossBasePath, String ossDomain) {
|
|
|
+ if (file == null) {
|
|
|
+ log.error("文件不能为空");
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+ String time = DateUtil.format(new Date(), "yyyyMMdd_HHmmssSSS");
|
|
|
+
|
|
|
+ String fileName = file.getOriginalFilename();
|
|
|
+
|
|
|
+ HashMap<String, String> result = new HashMap<>();
|
|
|
+ result.put("fileName", fileName);
|
|
|
+
|
|
|
+ String fileType = getFileType(fileName);
|
|
|
+ String dirType = "image/";
|
|
|
+ if (fileType.equals("doc")) {
|
|
|
+ dirType = "doc/";
|
|
|
+ }
|
|
|
+
|
|
|
+ String suffix = StringUtils.substringAfterLast(fileName, ".");
|
|
|
+ String newName = time + "." +suffix;
|
|
|
+ String savePath = saveBasePath + dirType + newName;
|
|
|
+
|
|
|
+ log.info("savePath: {}", savePath);
|
|
|
+
|
|
|
+ try {
|
|
|
+ FileUtil.writeFromStream(file.getInputStream(), savePath);
|
|
|
+ } catch (IOException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+
|
|
|
+ // 上传oss
|
|
|
+ String ossPath = ossBasePath + dirType + newName;
|
|
|
+// AliyunOssUtil.upload(savePath, ossPath);
|
|
|
+ aliyunOssUtil.upload(savePath, ossPath);
|
|
|
+ String ossUrl = ossDomain + ossPath;
|
|
|
+ log.info("ossUrl: {}", ossUrl);
|
|
|
+ result.put("ossUrl", ossUrl);
|
|
|
+
|
|
|
+ // 删除服务器临时文件
|
|
|
+ FileUtil.del(savePath);
|
|
|
+
|
|
|
+ return result;
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 文件上传,用来上传指定文档,应用程序需要不同的文件,特意针对他们设计
|
|
|
+ * @param file
|
|
|
+ * @param savePath
|
|
|
+ * @param ossBasePath
|
|
|
+ * @param ossDomain
|
|
|
+ * @param type sample:样本文档, empty:空白文档
|
|
|
+ * @param code 用来做目录的,如果code为空,创建code; 不为空,就把文件放到code目录里
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public Map renameUploadOssMap(MultipartFile file, String savePath, String ossBasePath, String ossDomain, String type, String code) {
|
|
|
+ if (file == null) {
|
|
|
+ log.error("文件不能为空");
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 创建目录码
|
|
|
+ if (StringUtils.isBlank(code)) {
|
|
|
+ code = RandomUtils.getSceneCode("");
|
|
|
+ }
|
|
|
+
|
|
|
+ String fileName = file.getOriginalFilename();
|
|
|
+
|
|
|
+ HashMap<String, String> result = new HashMap<>();
|
|
|
+ result.put("fileName", fileName);
|
|
|
+
|
|
|
+ String dirType = "doc/" + code + "/";
|
|
|
+
|
|
|
+ String suffix = StringUtils.substringAfterLast(fileName, ".");
|
|
|
+ String newName = type + "." +suffix;
|
|
|
+ savePath = savePath + dirType + newName;
|
|
|
+
|
|
|
+ log.info("savePath: {}", savePath);
|
|
|
+
|
|
|
+ try {
|
|
|
+ FileUtil.writeFromStream(file.getInputStream(), savePath);
|
|
|
+ } catch (IOException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+
|
|
|
+ // 上传oss
|
|
|
+ String ossPath = ossBasePath + dirType + newName;
|
|
|
+// AliyunOssUtil.upload(savePath, ossPath);
|
|
|
+ aliyunOssUtil.upload(savePath, ossPath);
|
|
|
+ // code 是用来区分表格是否放在同一目录
|
|
|
+ String ossUrl = ossDomain + ossPath + "?code=" +code;
|
|
|
+ log.info("ossUrl: {}", ossUrl);
|
|
|
+ result.put("ossUrl", ossUrl);
|
|
|
+
|
|
|
+ // 删除服务器临时文件
|
|
|
+ FileUtil.del(savePath);
|
|
|
+
|
|
|
+ return result;
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 使用opencv 生成缩略图
|
|
|
+ * 压缩图片
|
|
|
+ * 需要图片比例是2:1,否则图片压出来会变形
|
|
|
+ */
|
|
|
+ public String compressImgAndUploadOss(String inputFilePath, String ossBasePath, String ossDomain) throws IOException, InterruptedException {
|
|
|
+ String serverBasePath = StringUtils.substringBeforeLast(inputFilePath, "/");
|
|
|
+
|
|
|
+ String time = DateUtil.format(new Date(), "yyyyMMdd_HHmmssSSS");
|
|
|
+ String suffix = StringUtils.substringAfterLast(inputFilePath, ".");
|
|
|
+ String fileName ="/thumb_"+ time + "." +suffix;
|
|
|
+
|
|
|
+ // 保存图片位置
|
|
|
+ String saveCompressImgPath = serverBasePath + fileName;
|
|
|
+
|
|
|
+// Img.from(new File(inputFilePath)).scale(300, 150).write(new File(saveCompressImgPath));
|
|
|
+ // ./DAGE_Downsample 100m.jpg out.jpg 300 150
|
|
|
+ String cmd = "/home/720yun/100m/DAGE_Downsample " + inputFilePath + " " + saveCompressImgPath + " 300 150";
|
|
|
+ log.info("cmd: " + cmd);
|
|
|
+ long start = System.currentTimeMillis();
|
|
|
+ CmdUtils.cmdThumb(cmd);
|
|
|
+ long end = System.currentTimeMillis();
|
|
|
+ log.info("openCV耗时: " + (end-start)/1000 + " s");
|
|
|
+ log.info("图片压缩成功: " + saveCompressImgPath);
|
|
|
+
|
|
|
+ // 上传oss
|
|
|
+ String ossPath = ossBasePath + "image/" + fileName;
|
|
|
+ log.info("ossPath: " + ossPath);
|
|
|
+ asyncTask.uploadOss(saveCompressImgPath, ossPath);
|
|
|
+ String ossUrl = ossDomain + ossPath;
|
|
|
+ log.info("图片上传成功: " + ossUrl);
|
|
|
+ return ossUrl;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 使用java 生成缩略图
|
|
|
+ * @param inputFilePath
|
|
|
+ * @param ossBasePath
|
|
|
+ * @param ossDomain
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public String compressImgAndUploadOss2(String inputFilePath, String ossBasePath, String ossDomain){
|
|
|
+ String serverBasePath = StringUtils.substringBeforeLast(inputFilePath, "/");
|
|
|
+
|
|
|
+ String time = DateUtil.format(new Date(), "yyyyMMdd_HHmmssSSS");
|
|
|
+ String suffix = StringUtils.substringAfterLast(inputFilePath, ".");
|
|
|
+ String fileName ="/thumb_"+ time + "." +suffix;
|
|
|
+
|
|
|
+ // 保存图片位置
|
|
|
+ String saveCompressImgPath = serverBasePath + fileName;
|
|
|
+ String ossUrl = null;
|
|
|
+ try {
|
|
|
+ Img.from(new File(inputFilePath)).scale(600, 300).write(new File(saveCompressImgPath));
|
|
|
+ log.info("图片压缩成功: " + saveCompressImgPath);
|
|
|
+
|
|
|
+ if (FileUtil.isFile(saveCompressImgPath)) {
|
|
|
+ // 上传oss
|
|
|
+ String ossPath = ossBasePath + fileName;
|
|
|
+ log.info("ossPath: " + ossPath);
|
|
|
+// asyncTask.uploadOss(saveCompressImgPath, ossPath);
|
|
|
+ aliyunOssUtil.upload(saveCompressImgPath, ossPath);
|
|
|
+ ossUrl = ossDomain + ossPath;
|
|
|
+ log.info("图片上传成功: " + ossUrl);
|
|
|
+ } else {
|
|
|
+ log.error("缩略图不存在: " + saveCompressImgPath);
|
|
|
+ }
|
|
|
+
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.error("图片格式有误,不支持此图片");
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ return ossUrl;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 按比例缩放
|
|
|
+ * @param inputFilePath
|
|
|
+ * @param ossBasePath
|
|
|
+ * @param ossDomain
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public String scaleImgAndUploadOss(String inputFilePath, String ossBasePath, String ossDomain){
|
|
|
+ String serverBasePath = StringUtils.substringBeforeLast(inputFilePath, "/");
|
|
|
+
|
|
|
+ String time = DateUtil.format(new Date(), "yyyyMMdd_HHmmssSSS");
|
|
|
+ String suffix = StringUtils.substringAfterLast(inputFilePath, ".");
|
|
|
+ String fileName ="thumb_"+ time + "." +suffix;
|
|
|
+
|
|
|
+ // 保存图片位置
|
|
|
+ String saveCompressImgPath = serverBasePath + fileName;
|
|
|
+ String ossUrl = null;
|
|
|
+ try {
|
|
|
+ ImageUtil.scale(inputFilePath, saveCompressImgPath, 0.1f);
|
|
|
+ log.info("图片压缩成功: " + saveCompressImgPath);
|
|
|
+
|
|
|
+ if (FileUtil.isFile(saveCompressImgPath)) {
|
|
|
+ // 上传oss
|
|
|
+ String ossPath = ossBasePath + fileName;
|
|
|
+ log.info("ossPath: " + ossPath);
|
|
|
+ aliyunOssUtil.upload(saveCompressImgPath, ossPath);
|
|
|
+ ossUrl = ossDomain + ossPath;
|
|
|
+ log.info("图片上传成功: " + ossUrl);
|
|
|
+ } else {
|
|
|
+ log.error("缩略图不存在: " + saveCompressImgPath);
|
|
|
+ }
|
|
|
+
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.error("图片格式有误,不支持此图片");
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ return ossUrl;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ public void compressImg(){
|
|
|
+ String inputFilePath = "C:\\Users\\Administrator\\Desktop\\33\\100m\\100m.jpg";
|
|
|
+ String saveCompressImgPath = "C:\\Users\\Administrator\\Desktop\\33\\100m\\thump.jpg";
|
|
|
+
|
|
|
+ Img.from(new File(inputFilePath)).scale(300, 150).write(new File(saveCompressImgPath));
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ public static boolean checkFile(MultipartFile file) {
|
|
|
+ //设置允许上传文件类型
|
|
|
+ String suffixList = ".jpg,.gif,.png,.ico,.bmp,.jpeg,.zip,.zp,.rar,.mp3,.mp4,.avi,.mov";
|
|
|
+ // 获取文件后缀
|
|
|
+ if(file == null){
|
|
|
+ log.info("文件流为空不可上传");
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ String fileName = file.getOriginalFilename();
|
|
|
+ String suffix = fileName.substring(fileName.lastIndexOf(".")
|
|
|
+ + 1, fileName.length());
|
|
|
+ if (suffixList.contains(suffix.trim().toLowerCase())) {
|
|
|
+ log.info("无非法参数可以放行!!!");
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+ log.info("存在非法参数不能放行!请核对上传文件格式,重新刷新页面再次上传!");
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 根据路径写入文件,适合oss
|
|
|
+ * @param inPath 网络输入路径
|
|
|
+ * @param outPath 保存文件路径
|
|
|
+ * @throws IOException
|
|
|
+ */
|
|
|
+ public static void fielWrite(String inPath, String outPath) throws IOException {
|
|
|
+ InputStream in = URLUtil.getStream(new URL(inPath));
|
|
|
+ FileUtil.writeFromStream(in, outPath);
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取文件资源路径
|
|
|
+ * 这方法,测试时是用当前类路径,当打包成jar包时时,会变成跟目录下。所以要把资源文件放入口类的资源文件夹
|
|
|
+ * @param filePath 文件路径
|
|
|
+ * @return
|
|
|
+ * @throws IOException
|
|
|
+ */
|
|
|
+ public static String getResourceUrl(String filePath) throws IOException {
|
|
|
+ String path = ResourceUtils.getURL("classpath:").getPath();
|
|
|
+ path = path + filePath;
|
|
|
+ return path;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * @param fileName 文件名
|
|
|
+ * @return 文件的type
|
|
|
+ */
|
|
|
+ private static String getFileType(String fileName) {
|
|
|
+ log.info("getContentType:" + fileName);
|
|
|
+ // 文件的后缀名
|
|
|
+ String fileExtension = fileName.substring(fileName.lastIndexOf("."));
|
|
|
+ if (".bmp".equalsIgnoreCase(fileExtension)) {
|
|
|
+ return "image";
|
|
|
+ }
|
|
|
+ if (".gif".equalsIgnoreCase(fileExtension)) {
|
|
|
+ return "image";
|
|
|
+ }
|
|
|
+ if (".jpeg".equalsIgnoreCase(fileExtension) || ".jpg".equalsIgnoreCase(fileExtension)
|
|
|
+ || ".png".equalsIgnoreCase(fileExtension)) {
|
|
|
+ return "image";
|
|
|
+ }
|
|
|
+ if (".txt".equalsIgnoreCase(fileExtension)) {
|
|
|
+ return "doc";
|
|
|
+ }
|
|
|
+
|
|
|
+ if (".ppt".equalsIgnoreCase(fileExtension) || "pptx".equalsIgnoreCase(fileExtension)) {
|
|
|
+ return "doc";
|
|
|
+ }
|
|
|
+ if (".xls".equalsIgnoreCase(fileExtension) || "xlsx".equalsIgnoreCase(fileExtension)) {
|
|
|
+ return "doc";
|
|
|
+ }
|
|
|
+ if (".doc".equalsIgnoreCase(fileExtension) || "docx".equalsIgnoreCase(fileExtension)) {
|
|
|
+ return "doc";
|
|
|
+ }
|
|
|
+ // 默认返回类型
|
|
|
+ return "image";
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ public static String getType(String type) {
|
|
|
+ log.info("getContentType:" + type);
|
|
|
+ // 文件的后缀名
|
|
|
+ if ("jpg".equalsIgnoreCase(type)) {
|
|
|
+ return "image";
|
|
|
+ }
|
|
|
+ if ("png".equalsIgnoreCase(type)) {
|
|
|
+ return "image";
|
|
|
+ }
|
|
|
+ if (".jpeg".equalsIgnoreCase(type)) {
|
|
|
+ return "image";
|
|
|
+ }
|
|
|
+
|
|
|
+ // 默认返回类型
|
|
|
+ return "";
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ /***
|
|
|
+ * 遍历每一个文件路径
|
|
|
+ * dirPath :目录路径
|
|
|
+ * eachFile
|
|
|
+ * 作用上传oss
|
|
|
+ */
|
|
|
+ public static Map eachFile(String dirPath, String sceneCode, String ossBasePath) throws IOException {
|
|
|
+ File file = new File(dirPath);
|
|
|
+ HashMap<String, String> result = new HashMap<>();
|
|
|
+ dfs(file, sceneCode, ossBasePath, result);
|
|
|
+ return result;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 递归解析
|
|
|
+ * @param file
|
|
|
+ * @param sceneCode
|
|
|
+ * @param ossBasePath
|
|
|
+ * @return
|
|
|
+ * @throws IOException
|
|
|
+ */
|
|
|
+ public static void dfs(File file, String sceneCode, String ossBasePath, HashMap<String, String> result) throws IOException {
|
|
|
+
|
|
|
+ File[] files = file.listFiles();
|
|
|
+
|
|
|
+ for (File every : files) {
|
|
|
+ // 是文件
|
|
|
+ if (every.isFile()) {
|
|
|
+ String filePath = every.getAbsolutePath();
|
|
|
+ String ossPath = StringUtils.substringAfterLast(filePath, sceneCode);
|
|
|
+ ossPath = ossBasePath + sceneCode + ossPath;
|
|
|
+
|
|
|
+ result.put(filePath, ossPath);
|
|
|
+// log.info("filePath, {}", every.getAbsolutePath());
|
|
|
+// log.info("ossPath, {}", ossPath);
|
|
|
+
|
|
|
+ // 是目录
|
|
|
+ } else {
|
|
|
+ dfs(every, sceneCode, ossBasePath, result);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 检查出图比较是否2:1
|
|
|
+ * @return
|
|
|
+ * @throws IOException
|
|
|
+ */
|
|
|
+ public Boolean checkScale(String path) throws IOException {
|
|
|
+ BufferedImage image = ImageIO.read(new File(path));
|
|
|
+ int srcWidth = image .getWidth(); // 源图宽度
|
|
|
+ int srcHeight = image .getHeight(); // 源图高度
|
|
|
+
|
|
|
+ boolean flag = false;
|
|
|
+ if (srcWidth % srcHeight == 0 ) {
|
|
|
+ flag = true;
|
|
|
+ }
|
|
|
+ log.info("srcWidth = " + srcWidth);
|
|
|
+ log.info("srcHeight = " + srcHeight);
|
|
|
+ return flag;
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 检查出图比较是否2:1
|
|
|
+ * @return
|
|
|
+ * @throws IOException
|
|
|
+ */
|
|
|
+ public static Boolean checkScale(InputStream input) throws IOException {
|
|
|
+ BufferedImage image = ImageIO.read(input);
|
|
|
+ int srcWidth = image .getWidth(); // 源图宽度
|
|
|
+ int srcHeight = image .getHeight(); // 源图高度
|
|
|
+
|
|
|
+ boolean flag = false;
|
|
|
+ if (srcWidth % srcHeight == 0 ) {
|
|
|
+ flag = true;
|
|
|
+ }
|
|
|
+ log.info("srcWidth = " + srcWidth);
|
|
|
+ log.info("srcHeight = " + srcHeight);
|
|
|
+ return flag;
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ @Test
|
|
|
+ public void testScale() throws IOException {
|
|
|
+ String path = "C:\\Users\\Administrator\\Desktop\\33\\28\\2.jpg";
|
|
|
+ BufferedImage image = ImageIO.read(new File(path));
|
|
|
+ int srcWidth = image .getWidth(); // 源图宽度
|
|
|
+ int srcHeight = image .getHeight(); // 源图高度
|
|
|
+
|
|
|
+ if (srcWidth % srcHeight == 0 ) {
|
|
|
+ System.out.println("图片比例正确");
|
|
|
+ } else {
|
|
|
+ System.out.println("图片比例有误");
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ System.out.println("srcWidth = " + srcWidth);
|
|
|
+ System.out.println("srcHeight = " + srcHeight);
|
|
|
+}
|
|
|
+
|
|
|
+ public static String getResource(){
|
|
|
+ String path = "";
|
|
|
+ try {
|
|
|
+ path = ResourceUtils.getURL("classpath:").getPath();
|
|
|
+ path = URLDecoder.decode(path,"utf-8");
|
|
|
+ } catch (Exception e) {
|
|
|
+ }
|
|
|
+ return path;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ // jar包运行,只能用文件流的形式获取文件
|
|
|
+ public static InputStream getResource(String path){
|
|
|
+ ClassPathResource classPathResource = new ClassPathResource(path);
|
|
|
+ InputStream inputStream = null;
|
|
|
+ try {
|
|
|
+ inputStream = classPathResource.getInputStream();
|
|
|
+ } catch (IOException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ return inputStream;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+}
|