FileUtils.java 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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. {
  24. rootPath= file.getParentFile().toString() + FILE_SEPARATOR + fileName;
  25. }
  26. else {
  27. rootPath = file.getParentFile().toString();
  28. }
  29. //根据上方生成的根目录路径 生成对应文件夹 没有就新建
  30. File root = new File(rootPath);
  31. if (!root.exists()) {
  32. root.mkdir();
  33. }
  34. //返回的最终形式为 盘符:\项目层级\target\file
  35. return rootPath;
  36. }
  37. public static File getPictureFileByUrl(String dataUrl) throws IOException {
  38. URL url = new URL(dataUrl); //想要读取的url地址
  39. InputStream in = url.openStream();
  40. File file = File.createTempFile("new_url", ".jpg"); //创建文件
  41. OutputStream os = new FileOutputStream(file); //创建文件输出流
  42. int bytesRead;
  43. byte[] buffer = new byte[8192];
  44. int len = 8192;
  45. while ((bytesRead = in.read(buffer, 0, len)) != -1) {
  46. os.write(buffer, 0, bytesRead);
  47. }
  48. //关闭释放流
  49. os.close();
  50. in.close();
  51. return file;
  52. }
  53. public String analysisJsonFile() {
  54. ApplicationHome ah = new ApplicationHome(ToolUtil.class);
  55. File file = ah.getSource();
  56. String settingFilePath = file.getParentFile().toString() + FileUtils.FILE_SEPARATOR + "uploadSetting.json";
  57. BufferedReader reader = null;
  58. StringBuilder last = new StringBuilder();
  59. InputStreamReader inputStreamReader;
  60. try (FileInputStream fileInputStream = new FileInputStream(settingFilePath)) {
  61. inputStreamReader = new InputStreamReader(fileInputStream, StandardCharsets.UTF_8);
  62. reader = new BufferedReader(inputStreamReader);
  63. String tempString;
  64. while ((tempString = reader.readLine()) != null) {
  65. last.append(tempString);
  66. }
  67. reader.close();
  68. } catch (IOException e) {
  69. e.printStackTrace();
  70. } finally {
  71. if (reader != null) {
  72. try {
  73. reader.close();
  74. } catch (IOException e) {
  75. e.printStackTrace();
  76. }
  77. }
  78. }
  79. return last.toString();
  80. }
  81. public static File getFileByBytes(byte[] bytes, String prefix, String suffix) {
  82. BufferedOutputStream bos = null;
  83. FileOutputStream fos = null;
  84. File file = null;
  85. try {
  86. file = File.createTempFile(prefix, suffix);
  87. //输出流
  88. fos = new FileOutputStream(file);
  89. //缓冲流
  90. bos = new BufferedOutputStream(fos);
  91. //将字节数组写出
  92. bos.write(bytes);
  93. return file;
  94. } catch (Exception e) {
  95. e.printStackTrace();
  96. } finally {
  97. if (bos != null) {
  98. try {
  99. bos.close();
  100. } catch (IOException e) {
  101. e.printStackTrace();
  102. }
  103. }
  104. if (fos != null) {
  105. try {
  106. fos.close();
  107. } catch (IOException e) {
  108. e.printStackTrace();
  109. }
  110. }
  111. return file;
  112. }
  113. }
  114. public static File multipartFileToFile(MultipartFile file) throws Exception {
  115. File toFile = null;
  116. if (file == null ||file.equals("") || file.getSize() <= 0) {
  117. file = null;
  118. } else {
  119. InputStream ins = null;
  120. ins = file.getInputStream();
  121. toFile = new File(file.getOriginalFilename());
  122. inputStreamToFile(ins, toFile);
  123. ins.close();
  124. }
  125. return toFile;
  126. }
  127. //获取流文件
  128. private static void inputStreamToFile(InputStream ins, File file) {
  129. try {
  130. OutputStream os = new FileOutputStream(file);
  131. int bytesRead = 0;
  132. byte[] buffer = new byte[8192];
  133. while ((bytesRead = ins.read(buffer, 0, 8192)) != -1) {
  134. os.write(buffer, 0, bytesRead);
  135. }
  136. os.close();
  137. ins.close();
  138. } catch (Exception e) {
  139. e.printStackTrace();
  140. }
  141. }
  142. }