FileUtils.java 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. package cn.cslg.pas.common.utils;
  2. import cn.cslg.pas.Application;
  3. import cn.hutool.core.util.IdUtil;
  4. import org.apache.commons.fileupload.FileItem;
  5. import org.apache.commons.fileupload.disk.DiskFileItemFactory;
  6. import org.springframework.boot.system.ApplicationHome;
  7. import org.springframework.stereotype.Service;
  8. import org.springframework.web.multipart.MultipartFile;
  9. import java.io.*;
  10. import java.net.MalformedURLException;
  11. import java.net.URL;
  12. import java.nio.charset.StandardCharsets;
  13. @Service
  14. public class FileUtils {
  15. public static final String FILE_SEPARATOR = System.getProperty("file.separator");
  16. public static final String COMMON_FILE = "file";
  17. public static final String BACKUP_FILE = "backup";
  18. public static String getStaticPath(String fileName) {
  19. //ApplicationHome类 返回target目录层级
  20. ApplicationHome ah = new ApplicationHome(Application.class);
  21. //获取 applicationHome 内的路径 ...\target\classes 到这一层级下
  22. File file = ah.getSource();
  23. //获取 file的parentFile 即最后一级之前的所有层级路径(包括盘符) 这里能获得到的最终层级为 ...\target 后续用FILE_SEPARATOR(系统路径分割通配符 即 "\") 以及fileName拼接生成存放文件的目录层级 即为根目录 root
  24. String rootPath = file.getParentFile().toString() + FILE_SEPARATOR + fileName;
  25. //根据上方生成的根目录路径 生成对应文件夹 没有就新建
  26. File root = new File(rootPath);
  27. if (!root.exists()) {
  28. root.mkdir();
  29. }
  30. //返回的最终形式为 盘符:\项目层级\target\file
  31. return rootPath;
  32. }
  33. public String analysisJsonFile() {
  34. ApplicationHome ah = new ApplicationHome(BackupUtils.class);
  35. File file = ah.getSource();
  36. String settingFilePath = file.getParentFile().toString() + FileUtils.FILE_SEPARATOR + "uploadSetting.json";
  37. BufferedReader reader = null;
  38. StringBuilder last = new StringBuilder();
  39. InputStreamReader inputStreamReader;
  40. try (FileInputStream fileInputStream = new FileInputStream(settingFilePath)) {
  41. inputStreamReader = new InputStreamReader(fileInputStream, StandardCharsets.UTF_8);
  42. reader = new BufferedReader(inputStreamReader);
  43. String tempString;
  44. while ((tempString = reader.readLine()) != null) {
  45. last.append(tempString);
  46. }
  47. reader.close();
  48. } catch (IOException e) {
  49. e.printStackTrace();
  50. } finally {
  51. if (reader != null) {
  52. try {
  53. reader.close();
  54. } catch (IOException e) {
  55. e.printStackTrace();
  56. }
  57. }
  58. }
  59. return last.toString();
  60. }
  61. public static File getFileByBytes(byte[] bytes, String prefix, String suffix) {
  62. BufferedOutputStream bos = null;
  63. FileOutputStream fos = null;
  64. File file = null;
  65. try {
  66. file = File.createTempFile(prefix, suffix);
  67. //输出流
  68. fos = new FileOutputStream(file);
  69. //缓冲流
  70. bos = new BufferedOutputStream(fos);
  71. //将字节数组写出
  72. bos.write(bytes);
  73. return file;
  74. } catch (Exception e) {
  75. e.printStackTrace();
  76. } finally {
  77. if (bos != null) {
  78. try {
  79. bos.close();
  80. } catch (IOException e) {
  81. e.printStackTrace();
  82. }
  83. }
  84. if (fos != null) {
  85. try {
  86. fos.close();
  87. } catch (IOException e) {
  88. e.printStackTrace();
  89. }
  90. }
  91. return file;
  92. }
  93. }
  94. public static File getFileByUrl(String dataUrl) throws IOException {
  95. URL url = new URL(dataUrl); //想要读取的url地址
  96. InputStream in = url.openStream();
  97. File file = File.createTempFile("new_url", ".pdf"); //创建文件
  98. OutputStream os = new FileOutputStream(file); //创建文件输出流
  99. int bytesRead;
  100. byte[] buffer = new byte[8192];
  101. int len = 8192;
  102. while ((bytesRead = in.read(buffer, 0, len)) != -1) {
  103. os.write(buffer, 0, bytesRead);
  104. }
  105. //关闭释放流
  106. os.close();
  107. in.close();
  108. return file;
  109. }
  110. public static File getPictureFileByUrl(String dataUrl) throws IOException {
  111. URL url = new URL(dataUrl); //想要读取的url地址
  112. InputStream in = url.openStream();
  113. File file = File.createTempFile("new_url", ".jpg"); //创建文件
  114. OutputStream os = new FileOutputStream(file); //创建文件输出流
  115. int bytesRead;
  116. byte[] buffer = new byte[8192];
  117. int len = 8192;
  118. while ((bytesRead = in.read(buffer, 0, len)) != -1) {
  119. os.write(buffer, 0, bytesRead);
  120. }
  121. //关闭释放流
  122. os.close();
  123. in.close();
  124. return file;
  125. }
  126. public String getPath(String url) {
  127. return getStaticPath(COMMON_FILE) + url;
  128. }
  129. public String getDirectoryName() {
  130. return DateUtils.getNowTimeFormat("yyyyMMdd");
  131. }
  132. public String getSavePath(String directoryName) {
  133. return getStaticPath(COMMON_FILE) + FILE_SEPARATOR + directoryName + FILE_SEPARATOR;
  134. }
  135. public String createDirectory() {
  136. String directoryName = this.getDirectoryName();
  137. String savePath = this.getSavePath(directoryName);
  138. File directory = new File(savePath);
  139. if (!directory.exists()) {
  140. directory.mkdir();
  141. }
  142. return directoryName;
  143. }
  144. public static FileInputStream byteToFile(byte[] bytes) {
  145. String fileName = IdUtil.simpleUUID() + ".png";
  146. File file = new File(fileName);
  147. FileInputStream fileInputStream = null;
  148. try {
  149. OutputStream output = new FileOutputStream(file);
  150. BufferedOutputStream bufferedOutput = new BufferedOutputStream(output);
  151. bufferedOutput.write(bytes);
  152. fileInputStream = new FileInputStream(file);
  153. file.deleteOnExit();
  154. return fileInputStream;
  155. } catch (FileNotFoundException e) {
  156. e.printStackTrace();
  157. } catch (IOException e) {
  158. e.printStackTrace();
  159. }
  160. return fileInputStream;
  161. }
  162. }