FileUtils.java 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. package cn.cslg.pas.common.utils;
  2. import org.apache.commons.fileupload.FileItem;
  3. import org.apache.commons.fileupload.disk.DiskFileItemFactory;
  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.MalformedURLException;
  9. import java.net.URL;
  10. import java.nio.charset.StandardCharsets;
  11. @Service
  12. public class FileUtils {
  13. public static final String FILE_SEPARATOR = System.getProperty("file.separator");
  14. public static final String COMMON_FILE = "file";
  15. public static final String BACKUP_FILE = "backup";
  16. public static String getStaticPath(String fileName) {
  17. //ApplicationHome类 返回target目录层级
  18. ApplicationHome ah = new ApplicationHome(FileUtils.class);
  19. //获取 applicationHome 内的路径 ...\target\classes 到这一层级下
  20. File file = ah.getSource();
  21. //获取 file的parentFile 即最后一级之前的所有层级路径(包括盘符) 这里能获得到的最终层级为 ...\target 后续用FILE_SEPARATOR(系统路径分割通配符 即 "\") 以及fileName拼接生成存放文件的目录层级 即为根目录 root
  22. String rootPath = file.getParentFile().toString() + FILE_SEPARATOR + fileName;
  23. //根据上方生成的根目录路径 生成对应文件夹 没有就新建
  24. File root = new File(rootPath);
  25. if (!root.exists()) {
  26. root.mkdir();
  27. }
  28. //返回的最终形式为 盘符:\项目层级\target\file
  29. return rootPath;
  30. }
  31. public String analysisJsonFile() {
  32. ApplicationHome ah = new ApplicationHome(BackupUtils.class);
  33. File file = ah.getSource();
  34. String settingFilePath = file.getParentFile().toString() + FileUtils.FILE_SEPARATOR + "uploadSetting.json";
  35. BufferedReader reader = null;
  36. StringBuilder last = new StringBuilder();
  37. InputStreamReader inputStreamReader;
  38. try (FileInputStream fileInputStream = new FileInputStream(settingFilePath)) {
  39. inputStreamReader = new InputStreamReader(fileInputStream, StandardCharsets.UTF_8);
  40. reader = new BufferedReader(inputStreamReader);
  41. String tempString;
  42. while ((tempString = reader.readLine()) != null) {
  43. last.append(tempString);
  44. }
  45. reader.close();
  46. } catch (IOException e) {
  47. e.printStackTrace();
  48. } finally {
  49. if (reader != null) {
  50. try {
  51. reader.close();
  52. } catch (IOException e) {
  53. e.printStackTrace();
  54. }
  55. }
  56. }
  57. return last.toString();
  58. }
  59. public static File getFileByBytes(byte[] bytes, String prefix, String suffix) {
  60. BufferedOutputStream bos = null;
  61. FileOutputStream fos = null;
  62. File file = null;
  63. try {
  64. file = File.createTempFile(prefix, suffix);
  65. //输出流
  66. fos = new FileOutputStream(file);
  67. //缓冲流
  68. bos = new BufferedOutputStream(fos);
  69. //将字节数组写出
  70. bos.write(bytes);
  71. return file;
  72. } catch (Exception e) {
  73. e.printStackTrace();
  74. } finally {
  75. if (bos != null) {
  76. try {
  77. bos.close();
  78. } catch (IOException e) {
  79. e.printStackTrace();
  80. }
  81. }
  82. if (fos != null) {
  83. try {
  84. fos.close();
  85. } catch (IOException e) {
  86. e.printStackTrace();
  87. }
  88. }
  89. return file;
  90. }
  91. }
  92. public static File getFileByUrl(String dataUrl) throws IOException {
  93. URL url = new URL(dataUrl); //想要读取的url地址
  94. InputStream in = url.openStream();
  95. File file = File.createTempFile("new_url", ".jpg"); //创建文件
  96. OutputStream os = new FileOutputStream(file); //创建文件输出流
  97. int bytesRead;
  98. byte[] buffer = new byte[8192];
  99. int len = 8192;
  100. while ((bytesRead = in.read(buffer, 0, len)) != -1) {
  101. os.write(buffer, 0, bytesRead);
  102. }
  103. //关闭释放流
  104. os.close();
  105. in.close();
  106. return file;
  107. }
  108. }