|
@@ -2,7 +2,6 @@ package com.gis.common.util;
|
|
|
|
|
|
import cn.hutool.core.date.DateUtil;
|
|
|
import cn.hutool.core.img.Img;
|
|
|
-import cn.hutool.core.img.ImgUtil;
|
|
|
import cn.hutool.core.io.FileUtil;
|
|
|
import cn.hutool.core.util.URLUtil;
|
|
|
import com.gis.common.task.AsyncTask;
|
|
@@ -258,6 +257,53 @@ public class FileUtils {
|
|
|
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * 文件流上传,不存本地服务器,直接上传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;
|
|
|
+
|
|
|
+ }
|
|
|
|
|
|
|
|
|
|
|
@@ -467,19 +513,25 @@ public class FileUtils {
|
|
|
Img.from(new File(inputFilePath)).scale(300, 150).write(new File(saveCompressImgPath));
|
|
|
}
|
|
|
|
|
|
-// @Test
|
|
|
-// public void compressImg2(){
|
|
|
-// String inputFilePath = "C:\\Users\\Administrator\\Desktop\\33\\100m\\100m.jpg";
|
|
|
-// String saveCompressImgPath = "C:\\Users\\Administrator\\Desktop\\33\\100m\\thump.jpg";
|
|
|
-// try {
|
|
|
-// Thumbnails.of(inputFilePath).size(300, 150).toFile(saveCompressImgPath);
|
|
|
-// } catch (IOException e) {
|
|
|
-// e.printStackTrace();
|
|
|
-// }
|
|
|
-//// 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
|