|
@@ -1,214 +1,125 @@
|
|
|
package com.gis.common.util;
|
|
|
|
|
|
-import cn.hutool.core.date.DateUtil;
|
|
|
import cn.hutool.core.io.FileUtil;
|
|
|
-import cn.hutool.core.util.URLUtil;
|
|
|
+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.apache.commons.lang3.StringUtils;
|
|
|
-import org.junit.Test;
|
|
|
-import org.springframework.util.ResourceUtils;
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.stereotype.Component;
|
|
|
import org.springframework.web.multipart.MultipartFile;
|
|
|
|
|
|
-import java.io.*;
|
|
|
-import java.net.HttpURLConnection;
|
|
|
-import java.net.URL;
|
|
|
-import java.util.*;
|
|
|
+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 FileUtils {
|
|
|
|
|
|
+ @Autowired
|
|
|
+ ConfigConstant configConstant;
|
|
|
|
|
|
- /**
|
|
|
- * 多文件上传
|
|
|
- * savePath 路径(目录)
|
|
|
- */
|
|
|
- public static List<Map<String, String>> uploads(MultipartFile[] files, String savePath) throws IOException {
|
|
|
- if (files == null) {
|
|
|
- log.error("文件不能为空");
|
|
|
- return null;
|
|
|
- }
|
|
|
+ // 确保同一时间上传文件的唯一性
|
|
|
+ private static final AtomicInteger ATOMIC_INTEGER = new AtomicInteger();
|
|
|
|
|
|
- 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;
|
|
|
+ public boolean checkFile(MultipartFile file) {
|
|
|
+ //设置允许上传文件类型
|
|
|
+ String suffixList = configConstant.serverFileFallow;
|
|
|
+ // 获取文件后缀
|
|
|
+ if(file == null){
|
|
|
+ log.error("文件流为空不可上传");
|
|
|
+ return false;
|
|
|
}
|
|
|
-
|
|
|
- return list;
|
|
|
-
|
|
|
+ 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 savePath
|
|
|
- * @return
|
|
|
- * @throws IOException
|
|
|
+ * @param isPinYinRename false:时间戳重命名, true:用拼音重名文件
|
|
|
+ * @param savePath 保存地址(前面有斜杠, 后面没有),没有文件名
|
|
|
+ * @return 文件名
|
|
|
*/
|
|
|
- public static HashMap<String, String> upload(MultipartFile file, String savePath) throws IOException {
|
|
|
- if (file == null) {
|
|
|
- log.error("文件不能为空");
|
|
|
- return null;
|
|
|
- }
|
|
|
+ public String upload(MultipartFile file, String savePath, boolean isPinYinRename) {
|
|
|
|
|
|
- String time = DateUtil.format(new Date(), "yyyyMMdd_HHmmssSSS");
|
|
|
+ // 检查非法文件上传
|
|
|
+ boolean checkFile = this.checkFile(file);
|
|
|
+ if (!checkFile) {
|
|
|
+ throw new BaseRuntimeException("上传文件格式有误, 请重新上传");
|
|
|
+ }
|
|
|
|
|
|
+ // 文件目录
|
|
|
String fileName = file.getOriginalFilename();
|
|
|
- String suffix = StringUtils.substringAfterLast(fileName, ".");
|
|
|
- String newName = time + "." +suffix;
|
|
|
- savePath = savePath + newName;
|
|
|
-
|
|
|
- FileUtil.writeFromStream(file.getInputStream(), savePath);
|
|
|
- HashMap<String, String> fileInfo = new HashMap<>();
|
|
|
+ String newName;
|
|
|
+ if (isPinYinRename){
|
|
|
+ newName = RegexUtil.getPinyinName(fileName);
|
|
|
+ } else {
|
|
|
+// String suffix = StrUtil.substringAfterLast(fileName, ".");
|
|
|
+ String suffix = StrUtil.subAfter(fileName, ".", true);
|
|
|
+ newName = DateUtils.getDateTime() + ATOMIC_INTEGER.incrementAndGet() + "." + suffix;
|
|
|
+ }
|
|
|
|
|
|
- fileInfo.put("path", savePath);
|
|
|
- fileInfo.put("name", fileName);
|
|
|
- fileInfo.put("newName", newName);
|
|
|
+ savePath = configConstant.serverBasePath + savePath + "/" + newName;
|
|
|
+ log.info("保存文件地址:{}", savePath);
|
|
|
|
|
|
- return fileInfo;
|
|
|
+ try {
|
|
|
+ FileUtil.writeFromStream(file.getInputStream(), savePath);
|
|
|
|
|
|
+ return newName;
|
|
|
+ } catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ return null;
|
|
|
}
|
|
|
|
|
|
-
|
|
|
/**
|
|
|
- * 根据路径写入文件,适合oss
|
|
|
- * @param inPath 网络输入路径
|
|
|
- * @param outPath 保存文件路径
|
|
|
- * @throws IOException
|
|
|
+ *
|
|
|
+ * @param file
|
|
|
+ * @param isPinYinRename false:时间戳重命名, true:用拼音重名文件
|
|
|
+ * @param savePath 保存地址(前面有斜杠, 后面没有),没有文件名
|
|
|
+ * @return map
|
|
|
*/
|
|
|
- public static void fielWrite(String inPath, String outPath) throws IOException {
|
|
|
- InputStream in = URLUtil.getStream(new URL(inPath));
|
|
|
- FileUtil.writeFromStream(in, outPath);
|
|
|
- }
|
|
|
+ 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", file.getOriginalFilename());
|
|
|
+ result.put("filePath", savePath + "/" + newName);
|
|
|
+ return result;
|
|
|
|
|
|
-
|
|
|
- /**
|
|
|
- * 获取文件资源路径
|
|
|
- * 这方法,测试时是用当前类路径,当打包成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;
|
|
|
}
|
|
|
|
|
|
-
|
|
|
/**
|
|
|
- * 从网络Url中下载文件
|
|
|
- *
|
|
|
- * @param urlStr
|
|
|
- * @param fileName
|
|
|
- * @param savePath
|
|
|
- * @return
|
|
|
- * @throws IOException
|
|
|
+ * 真删除文件
|
|
|
+ * @param path 参数是相对地址
|
|
|
*/
|
|
|
- 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();
|
|
|
- }
|
|
|
- }
|
|
|
+ public void del(String path){
|
|
|
+ if (StrUtil.isNotBlank(path)){
|
|
|
+ String delPath = configConstant.serverBasePath + path;
|
|
|
+ FileUtil.del(delPath);
|
|
|
+ log.info("真删除文件: {}", delPath);
|
|
|
}
|
|
|
- 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();
|
|
|
- }
|
|
|
|
|
|
|
|
|
- @Test
|
|
|
- public void test(){
|
|
|
- String a = "https://super.4dage.com/data/690/vision.modeldata";
|
|
|
- downLoadFromUrl("https://super.4dage.com/data/690/vision.modeldata", "vision.modeldata", "F:\\test\\bigScene\\");
|
|
|
- }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
}
|