|
@@ -4,11 +4,15 @@ import cn.hutool.core.date.DateUtil;
|
|
|
import cn.hutool.core.img.ImgUtil;
|
|
|
import cn.hutool.core.io.FileUtil;
|
|
|
import cn.hutool.core.lang.Validator;
|
|
|
+import cn.hutool.core.util.StrUtil;
|
|
|
import cn.hutool.core.util.URLUtil;
|
|
|
+import com.gis.common.constant.ConfigConstant;
|
|
|
import com.gis.common.exception.BaseRuntimeException;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
import org.apache.commons.lang3.StringUtils;
|
|
|
import org.junit.Test;
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.stereotype.Component;
|
|
|
import org.springframework.util.ResourceUtils;
|
|
|
import org.springframework.web.multipart.MultipartFile;
|
|
|
|
|
@@ -17,13 +21,20 @@ import java.io.IOException;
|
|
|
import java.io.InputStream;
|
|
|
import java.net.URL;
|
|
|
import java.util.*;
|
|
|
+import java.util.concurrent.atomic.AtomicInteger;
|
|
|
|
|
|
/**
|
|
|
* Created by owen on 2020/5/12 0012 17:21
|
|
|
*/
|
|
|
@Slf4j
|
|
|
+@Component
|
|
|
public class FileUtils {
|
|
|
|
|
|
+ @Autowired
|
|
|
+ ConfigConstant configConstant;
|
|
|
+
|
|
|
+ // 确保同一时间上传文件的唯一性
|
|
|
+ private static final AtomicInteger ATOMIC_INTEGER = new AtomicInteger();
|
|
|
|
|
|
/**
|
|
|
* 多文件上传
|
|
@@ -65,6 +76,68 @@ public class FileUtils {
|
|
|
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ *
|
|
|
+ * @param file
|
|
|
+ * @param isPinYinRename false:时间戳重命名, true:用拼音重名文件
|
|
|
+ * @param savePath 保存地址(前面有斜杠, 后面没有),没有文件名
|
|
|
+ * @return 文件名
|
|
|
+ */
|
|
|
+ public String upload(MultipartFile file, String savePath, boolean isPinYinRename) {
|
|
|
+
|
|
|
+ // 检查非法文件上传
|
|
|
+ boolean checkFile = this.checkFile(file);
|
|
|
+ if (!checkFile) {
|
|
|
+ throw new BaseRuntimeException("上传文件格式有误, 请重新上传");
|
|
|
+ }
|
|
|
+
|
|
|
+ // 文件目录
|
|
|
+ String fileName = file.getOriginalFilename();
|
|
|
+ String newName;
|
|
|
+ if (isPinYinRename){
|
|
|
+ newName = RegexUtil.getPinyinName(fileName);
|
|
|
+ } else {
|
|
|
+ String suffix = StrUtil.subAfter(fileName, ".", true);
|
|
|
+ newName = DateUtils.getDateTime() + ATOMIC_INTEGER.incrementAndGet() + "." + suffix;
|
|
|
+ }
|
|
|
+
|
|
|
+ savePath = configConstant.serverBasePath + savePath + "/" + newName;
|
|
|
+ log.info("保存文件地址:{}", savePath);
|
|
|
+
|
|
|
+ try {
|
|
|
+ FileUtil.writeFromStream(file.getInputStream(), savePath);
|
|
|
+
|
|
|
+ return newName;
|
|
|
+ } catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ *
|
|
|
+ * @param file
|
|
|
+ * @param isPinYinRename false:时间戳重命名, true:用拼音重名文件
|
|
|
+ * @param savePath 保存地址(前面有斜杠, 后面没有),没有文件名
|
|
|
+ * @return map
|
|
|
+ */
|
|
|
+ public Map<String, Object> uploadMap(MultipartFile file, String savePath, boolean isPinYinRename) {
|
|
|
+ String newName = this.upload(file, savePath, isPinYinRename);
|
|
|
+ HashMap<String, Object> result = new HashMap<>();
|
|
|
+ // 防止名字过长,超过数据库长度
|
|
|
+ String originalFilename = file.getOriginalFilename();
|
|
|
+ if (originalFilename != null && originalFilename.length() > 64){
|
|
|
+ String suffix = StrUtil.subAfter(originalFilename, ".", true);
|
|
|
+ originalFilename = originalFilename.substring(64);
|
|
|
+ originalFilename = originalFilename + "." + suffix;
|
|
|
+ }
|
|
|
+ result.put("fileName", originalFilename);
|
|
|
+ result.put("filePath", savePath + "/" + newName);
|
|
|
+ return result;
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
|
|
|
/**
|
|
|
* 单文件上传
|
|
@@ -210,7 +283,7 @@ public class FileUtils {
|
|
|
|
|
|
public static boolean checkFile(MultipartFile file) {
|
|
|
//设置允许上传文件类型
|
|
|
- String suffixList = ".jpg,.gif,.png,.ico,.bmp,.jpeg,.zip,.zp,.rar,.mp3,.mp4,.avi,.mov,.4dage";
|
|
|
+ String suffixList = ".jpg,.gif,.png,.ico,.bmp,.jpeg,.zip,.zp,.rar,.mp3,.mp4,.avi,.mov,.4dage,.dam";
|
|
|
// 获取文件后缀
|
|
|
if(file == null){
|
|
|
log.info("文件流为空不可上传");
|