FileUtils.java 5.5 KB

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