|
@@ -4,7 +4,6 @@ import cn.hutool.core.date.DateUtil;
|
|
import cn.hutool.core.img.Img;
|
|
import cn.hutool.core.img.Img;
|
|
import cn.hutool.core.io.FileUtil;
|
|
import cn.hutool.core.io.FileUtil;
|
|
import cn.hutool.core.util.URLUtil;
|
|
import cn.hutool.core.util.URLUtil;
|
|
-import com.gis.common.task.AsyncTask;
|
|
|
|
import lombok.extern.log4j.Log4j2;
|
|
import lombok.extern.log4j.Log4j2;
|
|
import org.apache.commons.lang3.StringUtils;
|
|
import org.apache.commons.lang3.StringUtils;
|
|
import org.junit.Test;
|
|
import org.junit.Test;
|
|
@@ -29,15 +28,10 @@ import java.util.*;
|
|
@Component
|
|
@Component
|
|
public class FileUtils {
|
|
public class FileUtils {
|
|
|
|
|
|
- /** 临时文件存放目录*/
|
|
|
|
- private final static String PROJECT_NAME = "cms_pano_fcb_dev_data/";
|
|
|
|
|
|
|
|
@Autowired
|
|
@Autowired
|
|
AliyunOssUtil aliyunOssUtil;
|
|
AliyunOssUtil aliyunOssUtil;
|
|
|
|
|
|
- @Autowired
|
|
|
|
- AsyncTask asyncTask;
|
|
|
|
-
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
* 多文件上传
|
|
* 多文件上传
|
|
@@ -165,41 +159,6 @@ public class FileUtils {
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
- /**
|
|
|
|
- * 重命名文件上传
|
|
|
|
- * @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
|
|
* 文件上传oss
|
|
* @param file
|
|
* @param file
|
|
@@ -424,72 +383,6 @@ public class FileUtils {
|
|
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
- * 使用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 ossBasePath
|
|
|
|
- * @param ossDomain
|
|
|
|
- * @return
|
|
|
|
- */
|
|
|
|
-// public String compressImgAndUploadOss2(MultipartFile file, String ossBasePath, String ossDomain, Integer width, Integer height, String imgName) throws Exception {
|
|
|
|
-// // 保存图片位置
|
|
|
|
-// String saveCompressImgPath = ossBasePath + imgName;
|
|
|
|
-// String ossUrl = null;
|
|
|
|
-//
|
|
|
|
-//// Img.from(new File(file.getInputStream())).scale(width, height).write(new File(saveCompressImgPath));
|
|
|
|
-// Img.from(new File("")).scale(width, height).write(new File(saveCompressImgPath));
|
|
|
|
-// log.info("图片压缩成功: " + saveCompressImgPath);
|
|
|
|
-//
|
|
|
|
-// if (FileUtil.isFile(saveCompressImgPath)) {
|
|
|
|
-// // 上传oss
|
|
|
|
-// String ossPath = ossBasePath + imgName;
|
|
|
|
-// log.info("ossPath: " + ossPath);
|
|
|
|
-// aliyunOssUtil.upload(saveCompressImgPath, ossPath);
|
|
|
|
-// ossUrl = ossDomain + ossPath;
|
|
|
|
-// log.info("图片上传成功: " + ossUrl);
|
|
|
|
-// } else {
|
|
|
|
-// log.error("缩略图不存在: " + saveCompressImgPath);
|
|
|
|
-// }
|
|
|
|
-//
|
|
|
|
-//
|
|
|
|
-// return ossUrl;
|
|
|
|
-// }
|
|
|
|
-
|
|
|
|
-
|
|
|
|
- /**
|
|
|
|
* 压缩图片
|
|
* 压缩图片
|
|
* 固定名称: 名称固定是xxx.jpg
|
|
* 固定名称: 名称固定是xxx.jpg
|
|
* @param inputFilePath
|
|
* @param inputFilePath
|