|
@@ -0,0 +1,242 @@
|
|
|
+package com.gis.common.util;
|
|
|
+
|
|
|
+import cn.hutool.core.io.FileUtil;
|
|
|
+import cn.hutool.core.util.ReUtil;
|
|
|
+import cn.hutool.core.util.StrUtil;
|
|
|
+import com.gis.common.constant.ConfigConstant;
|
|
|
+import com.gis.common.exception.BaseRuntimeException;
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
+import org.junit.Test;
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.stereotype.Component;
|
|
|
+import org.springframework.web.multipart.MultipartFile;
|
|
|
+
|
|
|
+import java.io.*;
|
|
|
+import java.util.HashMap;
|
|
|
+import java.util.Map;
|
|
|
+import java.util.concurrent.atomic.AtomicInteger;
|
|
|
+
|
|
|
+
|
|
|
+/**
|
|
|
+ * Created by owen on 2020/5/12 0012 17:21
|
|
|
+ */
|
|
|
+@Slf4j
|
|
|
+@Component
|
|
|
+public class FileUtils2 {
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ ConfigConstant configConstant;
|
|
|
+
|
|
|
+ // 确保同一时间上传文件的唯一性
|
|
|
+ private static final AtomicInteger ATOMIC_INTEGER = new AtomicInteger();
|
|
|
+
|
|
|
+ public boolean checkFile(MultipartFile file) {
|
|
|
+ //设置允许上传文件类型
|
|
|
+ String suffixList = configConstant.serverFileFallow;
|
|
|
+ // 获取文件后缀
|
|
|
+ if(file == null){
|
|
|
+ log.error("文件流为空不可上传");
|
|
|
+ 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.error("存在非法参数不能放行!请核对上传文件格式,重新刷新页面再次上传!输入文件后缀: {}", suffix);
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ *
|
|
|
+ * @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<>();
|
|
|
+ result.put("fileName", doFileName(file.getOriginalFilename()));
|
|
|
+ result.put("filePath", savePath + "/" + newName);
|
|
|
+ // 单位KB
|
|
|
+ result.put("fileSize", (file.getSize() / 1024));
|
|
|
+ return result;
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 2023-09-26
|
|
|
+ * 临时使用, 为了改bug
|
|
|
+ * @param file
|
|
|
+ * @param savePath
|
|
|
+ * @param isPinYinRename
|
|
|
+ */
|
|
|
+ public HashMap<String, Object> uploadOss(MultipartFile file, String savePath, boolean isPinYinRename) {
|
|
|
+ String newName = this.upload(file, savePath, isPinYinRename);
|
|
|
+ HashMap<String, Object> result = new HashMap<>();
|
|
|
+ result.put("fileName", doFileName(file.getOriginalFilename()));
|
|
|
+ result.put("filePath", savePath + "/" + newName);
|
|
|
+ Object filePath = result.get("filePath");
|
|
|
+
|
|
|
+ result.put("ossUrl", configConstant.serverDomain + filePath);
|
|
|
+ result.put("ossPath", filePath);
|
|
|
+ // 单位KB
|
|
|
+ result.put("fileSize", (file.getSize() / 1024));
|
|
|
+ return result;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 处理文件名过长问题
|
|
|
+ * @param fileName
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public String doFileName(String fileName){
|
|
|
+ int length = fileName.length();
|
|
|
+ // 名字长度少于32位不处理
|
|
|
+ if (length < 32){
|
|
|
+ return fileName;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 后缀
|
|
|
+ String suffix = StrUtil.subAfter(fileName, ".", true);
|
|
|
+ // 前缀
|
|
|
+ String sub = StrUtil.sub(fileName, 0, 30);
|
|
|
+ return sub + "." + suffix;
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 真删除文件
|
|
|
+ * @param path 参数是相对地址
|
|
|
+ */
|
|
|
+ public void del(String path){
|
|
|
+ if (StrUtil.isBlank(path)){
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ log.info("path: {}", path);
|
|
|
+ // 过滤 路径为/////, 后面必须跟非斜杠字符
|
|
|
+ String regex = "^[/|\\\\]{1,2}[^/|\\\\]\\S";
|
|
|
+ if (!ReUtil.contains(regex, path)){
|
|
|
+ log.warn("非法路径删除:{}", path);
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ String delPath = configConstant.serverBasePath + path;
|
|
|
+ FileUtil.del(delPath);
|
|
|
+ log.info("真删除文件: {}", delPath);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 路径必须包含中文或字母或符号
|
|
|
+ * @throws Exception
|
|
|
+ */
|
|
|
+ @Test
|
|
|
+ public void re() throws Exception {
|
|
|
+
|
|
|
+ System.out.println("/");
|
|
|
+ System.out.println("//");
|
|
|
+ System.out.println("///");
|
|
|
+ System.out.println("\\");
|
|
|
+ System.out.println("\\\\");
|
|
|
+ System.out.println("\\\\\\");
|
|
|
+// String regex = "^[/|\\\\]{0,2}\\S";
|
|
|
+// String regex = "^[/|\\\\](\\S+/?)+$";
|
|
|
+ String regex = "^[/|\\\\]{1,2}[^/|\\\\]\\S";
|
|
|
+
|
|
|
+
|
|
|
+// String path = "\\qq\\aaa";
|
|
|
+// String path = "/年后\\aaa";
|
|
|
+ String path = "//aaa";
|
|
|
+ System.out.println(path);
|
|
|
+ System.out.println(ReUtil.contains(regex, path));
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ @Test
|
|
|
+ public void test1() throws Exception {
|
|
|
+ File file = new File("E:\\桌面\\fsdownload\\1.jpg");
|
|
|
+ //字节输入流
|
|
|
+ InputStream fis = new FileInputStream(file);
|
|
|
+ //字节输出流
|
|
|
+ OutputStream fos = new FileOutputStream(file);
|
|
|
+
|
|
|
+ //字符输入流
|
|
|
+ Reader reader = new FileReader(file);
|
|
|
+ //字符输出流
|
|
|
+ Writer writer = new FileWriter(file);
|
|
|
+
|
|
|
+ //字节缓冲流
|
|
|
+ BufferedInputStream bis = new BufferedInputStream(fis);
|
|
|
+ BufferedOutputStream bos = new BufferedOutputStream(fos);
|
|
|
+
|
|
|
+ //字符缓冲流
|
|
|
+ BufferedReader bufferedReader = new BufferedReader(reader);
|
|
|
+ BufferedWriter bufferedWriter = new BufferedWriter(writer);
|
|
|
+
|
|
|
+ //转换流 字节流转字符流
|
|
|
+ InputStreamReader isr = new InputStreamReader(fis);
|
|
|
+ OutputStreamWriter osw = new OutputStreamWriter(fos);
|
|
|
+
|
|
|
+ //打印流
|
|
|
+ PrintWriter printWriter = new PrintWriter(file);
|
|
|
+ PrintStream printStream = new PrintStream(file);
|
|
|
+
|
|
|
+ //了解 什么时候使用过
|
|
|
+ InputStream in = System.in;
|
|
|
+ System.out.println(in);
|
|
|
+ PrintStream out = System.out;
|
|
|
+ System.out.println(out);
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ // jar包运行,只能用文件流的形式获取文件
|
|
|
+
|
|
|
+
|
|
|
+}
|