FileUtils.java 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. package cn.cslg.pas.common.utils;
  2. import cn.cslg.pas.common.model.dto.UploadFileDTO;
  3. import cn.hutool.core.io.FileUtil;
  4. import cn.hutool.core.util.IdUtil;
  5. import org.apache.commons.fileupload.FileItem;
  6. import org.apache.commons.fileupload.disk.DiskFileItemFactory;
  7. import org.springframework.boot.system.ApplicationHome;
  8. import org.springframework.stereotype.Service;
  9. import org.springframework.web.multipart.MultipartFile;
  10. import org.springframework.web.multipart.commons.CommonsMultipartFile;
  11. import java.io.*;
  12. import java.net.HttpURLConnection;
  13. import java.net.URL;
  14. import java.nio.charset.StandardCharsets;
  15. @Service
  16. public class FileUtils {
  17. public static final String FILE_SEPARATOR = System.getProperty("file.separator");
  18. public static final String COMMON_FILE = "file";
  19. public static final String BACKUP_FILE = "backup";
  20. public static String getStaticPath(String fileName) {
  21. //ApplicationHome类 返回target目录层级
  22. ApplicationHome ah = new ApplicationHome(FileUtils.class);
  23. //获取 applicationHome 内的路径 ...\target\classes 到这一层级下
  24. File file = ah.getSource();
  25. //获取 file的parentFile 即最后一级之前的所有层级路径(包括盘符) 这里能获得到的最终层级为 ...\target 后续用FILE_SEPARATOR(系统路径分割通配符 即 "\") 以及fileName拼接生成存放文件的目录层级 即为根目录 root
  26. String rootPath = file.getParentFile().toString() + FILE_SEPARATOR + fileName;
  27. //根据上方生成的根目录路径 生成对应文件夹 没有就新建
  28. File root = new File(rootPath);
  29. if (!root.exists()) {
  30. root.mkdir();
  31. }
  32. //返回的最终形式为 盘符:\项目层级\target\file
  33. return rootPath;
  34. }
  35. public UploadFileDTO uploadFile(MultipartFile file) {
  36. UploadFileDTO fileDTO = new UploadFileDTO();
  37. //以下操作为 先取得传入文件的完整文件名 (文件名 + 后缀) 然后用FileUtil分别取出 文件名 和 不带 "." 的后缀 然后将这些内容装配进 实体中
  38. //file.getOriginFilename 获取源文件
  39. //FileUtil.getPrefix 返回主文件名
  40. fileDTO.setName(FileUtil.getPrefix(file.getOriginalFilename()));
  41. //FileUtil.extName 获取文件的扩展名(后缀名),扩展名不带 "."
  42. fileDTO.setExtName(FileUtil.extName(file.getOriginalFilename()));
  43. //获取目录名 用时间作为目录名称
  44. String directoryName = this.getDirectoryName();
  45. //用IdUtil生成的UUID 与实体中的 extName(后缀名) 拼接后作为文件名 simpleUUID与randomUUID的区别是 simpleUUID 没有 "-"
  46. String fileName = IdUtil.simpleUUID() + "." + fileDTO.getExtName();
  47. //将完整文件名进行装配
  48. fileDTO.setFileName(fileName);
  49. //生成存储文件的路径
  50. String savePath = this.getSavePath(directoryName);
  51. //根据生成存储文件的路径 生成对应文件夹 没有就新建
  52. File directory = new File(savePath);
  53. if (!directory.exists()) {
  54. directory.mkdir();
  55. }
  56. this.saveFile(file, savePath + fileName);
  57. fileDTO.setPath(FILE_SEPARATOR + directoryName + FILE_SEPARATOR + fileName);
  58. fileDTO.setFileSize(file.getSize());
  59. return fileDTO;
  60. }
  61. public String createDirectory() {
  62. String directoryName = this.getDirectoryName();
  63. String savePath = this.getSavePath(directoryName);
  64. File directory = new File(savePath);
  65. if (!directory.exists()) {
  66. directory.mkdir();
  67. }
  68. return directoryName;
  69. }
  70. public void saveFile(MultipartFile file, String path) {
  71. try {
  72. file.transferTo(new File(path));
  73. } catch (Exception e) {
  74. e.printStackTrace();
  75. }
  76. }
  77. public String getTempPath(String fileName) {
  78. String tempPath = getStaticPath(COMMON_FILE) + FILE_SEPARATOR + "temp";
  79. File file = new File(tempPath);
  80. if (!file.exists()) {
  81. file.mkdir();
  82. }
  83. return tempPath + FILE_SEPARATOR + fileName;
  84. }
  85. public String getSavePath(String directoryName) {
  86. return getStaticPath(COMMON_FILE) + FILE_SEPARATOR + directoryName + FILE_SEPARATOR;
  87. }
  88. public String getDirectory(String fileName) {
  89. return FILE_SEPARATOR + this.createDirectory() + FILE_SEPARATOR + fileName;
  90. }
  91. public String getDirectory2(String directoryName) {
  92. return FILE_SEPARATOR + directoryName + FILE_SEPARATOR;
  93. }
  94. public String getSystemPath(String url) {
  95. return getStaticPath(COMMON_FILE) + FILE_SEPARATOR + url;
  96. }
  97. public String getSystemPath() {
  98. return getStaticPath(COMMON_FILE);
  99. }
  100. public String getDirectoryName() {
  101. return DateUtils.getNowTimeFormat("yyyyMMdd");
  102. }
  103. public String getPath(String url) {
  104. return getStaticPath(COMMON_FILE) + url;
  105. }
  106. public String analysisJsonFile() {
  107. ApplicationHome ah = new ApplicationHome(BackupUtils.class);
  108. File file = ah.getSource();
  109. String settingFilePath = file.getParentFile().toString() + FileUtils.FILE_SEPARATOR + "uploadSetting.json";
  110. BufferedReader reader = null;
  111. StringBuilder last = new StringBuilder();
  112. InputStreamReader inputStreamReader;
  113. try (FileInputStream fileInputStream = new FileInputStream(settingFilePath)) {
  114. inputStreamReader = new InputStreamReader(fileInputStream, StandardCharsets.UTF_8);
  115. reader = new BufferedReader(inputStreamReader);
  116. String tempString;
  117. while ((tempString = reader.readLine()) != null) {
  118. last.append(tempString);
  119. }
  120. reader.close();
  121. } catch (IOException e) {
  122. e.printStackTrace();
  123. } finally {
  124. if (reader != null) {
  125. try {
  126. reader.close();
  127. } catch (IOException e) {
  128. e.printStackTrace();
  129. }
  130. }
  131. }
  132. return last.toString();
  133. }
  134. public String analysisJsonFile(String fileName) {
  135. ApplicationHome ah = new ApplicationHome(BackupUtils.class);
  136. File file = ah.getSource();
  137. String settingFilePath = file.getParentFile().toString() + FileUtils.FILE_SEPARATOR + "uploadSetting.json";
  138. BufferedReader reader = null;
  139. StringBuilder last = new StringBuilder();
  140. InputStreamReader inputStreamReader;
  141. try (FileInputStream fileInputStream = new FileInputStream(settingFilePath)) {
  142. inputStreamReader = new InputStreamReader(fileInputStream, StandardCharsets.UTF_8);
  143. reader = new BufferedReader(inputStreamReader);
  144. String tempString;
  145. while ((tempString = reader.readLine()) != null) {
  146. last.append(tempString);
  147. }
  148. reader.close();
  149. } catch (IOException e) {
  150. e.printStackTrace();
  151. } finally {
  152. if (reader != null) {
  153. try {
  154. reader.close();
  155. } catch (IOException e) {
  156. e.printStackTrace();
  157. }
  158. }
  159. }
  160. return last.toString();
  161. }
  162. public static MultipartFile fileToMultipartFile(File file) {
  163. DiskFileItemFactory diskFileItemFactory = new DiskFileItemFactory(16, null);
  164. FileItem item = diskFileItemFactory.createItem(file.getName(), "text/plain", true, file.getName());
  165. int bytesRead = 0;
  166. byte[] buffer = new byte[8192];
  167. try {
  168. FileInputStream fis = new FileInputStream(file);
  169. OutputStream os = item.getOutputStream();
  170. int len = 8192;
  171. while ((bytesRead = fis.read(buffer, 0, len)) != -1) {
  172. os.write(buffer, 0, bytesRead);
  173. }
  174. os.close();
  175. fis.close();
  176. } catch (IOException e) {
  177. e.printStackTrace();
  178. }
  179. return (MultipartFile) new CommonsMultipartFile(item);
  180. }
  181. public static File getFile(String url) throws Exception {
  182. //读取图片类型
  183. String fileName = url.substring(url.lastIndexOf("."), url.length());
  184. File file = null;
  185. URL urlfile;
  186. InputStream inStream = null;
  187. OutputStream os = null;
  188. try {
  189. file = File.createTempFile("new_url", ".jpg");
  190. //获取文件
  191. urlfile = new URL(url);
  192. inStream = urlfile.openStream();
  193. os = new FileOutputStream(file);
  194. int bytesRead = 0;
  195. byte[] buffer = new byte[8192];
  196. while ((bytesRead = inStream.read(buffer, 0, 8192)) != -1) {
  197. os.write(buffer, 0, bytesRead);
  198. }
  199. } catch (Exception e) {
  200. e.printStackTrace();
  201. } finally {
  202. try {
  203. if (null != os) {
  204. os.close();
  205. }
  206. if (null != inStream) {
  207. inStream.close();
  208. }
  209. } catch (Exception e) {
  210. e.printStackTrace();
  211. }
  212. }
  213. return file;
  214. }
  215. }