FileUtils.java 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  1. package cn.cslg.pas.common.utils;
  2. import cn.cslg.pas.Application;
  3. import cn.hutool.core.util.IdUtil;
  4. import com.alibaba.fastjson.JSON;
  5. import org.springframework.boot.system.ApplicationHome;
  6. import org.springframework.stereotype.Service;
  7. import org.springframework.web.multipart.MultipartFile;
  8. import java.io.*;
  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(Application.class);
  19. //获取 applicationHome 内的路径 ...\target\classes 到这一层级下
  20. File file = ah.getSource();
  21. //获取 file的parentFile 即最后一级之前的所有层级路径(包括盘符) 这里能获得到的最终层级为 ...\target 后续用FILE_SEPARATOR(系统路径分割通配符 即 "\") 以及fileName拼接生成存放文件的目录层级 即为根目录 root
  22. String rootPath = null;
  23. if (fileName != null && !fileName.equals("")) {
  24. rootPath = file.getParentFile().toString() + FILE_SEPARATOR + fileName;
  25. } else {
  26. rootPath = file.getParentFile().toString();
  27. }
  28. //根据上方生成的根目录路径 生成对应文件夹 没有就新建
  29. File root = new File(rootPath);
  30. if (!root.exists()) {
  31. root.mkdir();
  32. }
  33. //返回的最终形式为 盘符:\项目层级\target\file
  34. return rootPath;
  35. }
  36. public String analysisJsonFile() {
  37. ApplicationHome ah = new ApplicationHome(BackupUtils.class);
  38. File file = ah.getSource();
  39. // String settingFilePath = file.getParentFile().toString() + FileUtils.FILE_SEPARATOR + "\\jsons\\" + "uploadSetting.json";
  40. String settingFilePath = file.getParentFile().toString() + FileUtils.FILE_SEPARATOR + "uploadSetting.json";
  41. BufferedReader reader = null;
  42. StringBuilder last = new StringBuilder();
  43. InputStreamReader inputStreamReader;
  44. try (FileInputStream fileInputStream = new FileInputStream(settingFilePath)) {
  45. inputStreamReader = new InputStreamReader(fileInputStream, StandardCharsets.UTF_8);
  46. reader = new BufferedReader(inputStreamReader);
  47. String tempString;
  48. while ((tempString = reader.readLine()) != null) {
  49. last.append(tempString);
  50. }
  51. reader.close();
  52. } catch (IOException e) {
  53. e.printStackTrace();
  54. } finally {
  55. if (reader != null) {
  56. try {
  57. reader.close();
  58. } catch (IOException e) {
  59. e.printStackTrace();
  60. }
  61. }
  62. }
  63. return last.toString();
  64. }
  65. public static File getFileByBytes(byte[] bytes, String prefix, String suffix) {
  66. BufferedOutputStream bos = null;
  67. FileOutputStream fos = null;
  68. File file = null;
  69. try {
  70. file = File.createTempFile(prefix, suffix);
  71. //输出流
  72. fos = new FileOutputStream(file);
  73. //缓冲流
  74. bos = new BufferedOutputStream(fos);
  75. //将字节数组写出
  76. bos.write(bytes);
  77. return file;
  78. } catch (Exception e) {
  79. e.printStackTrace();
  80. } finally {
  81. if (bos != null) {
  82. try {
  83. bos.close();
  84. } catch (IOException e) {
  85. e.printStackTrace();
  86. }
  87. }
  88. if (fos != null) {
  89. try {
  90. fos.close();
  91. } catch (IOException e) {
  92. e.printStackTrace();
  93. }
  94. }
  95. return file;
  96. }
  97. }
  98. public static File getFileByUrl(String dataUrl) throws IOException {
  99. URL url = new URL(dataUrl); //想要读取的url地址
  100. InputStream in = url.openStream();
  101. File file = File.createTempFile("new_url", ".pdf"); //创建文件
  102. OutputStream os = new FileOutputStream(file); //创建文件输出流
  103. int bytesRead;
  104. byte[] buffer = new byte[8192];
  105. int len = 8192;
  106. while ((bytesRead = in.read(buffer, 0, len)) != -1) {
  107. os.write(buffer, 0, bytesRead);
  108. }
  109. //关闭释放流
  110. os.close();
  111. in.close();
  112. return file;
  113. }
  114. public static File getPictureFileByUrl(String dataUrl) throws IOException {
  115. URL url = new URL(dataUrl); //想要读取的url地址
  116. InputStream in = url.openStream();
  117. File file = File.createTempFile("new_url", ".jpg"); //创建文件
  118. OutputStream os = new FileOutputStream(file); //创建文件输出流
  119. int bytesRead;
  120. byte[] buffer = new byte[8192];
  121. int len = 8192;
  122. while ((bytesRead = in.read(buffer, 0, len)) != -1) {
  123. os.write(buffer, 0, bytesRead);
  124. }
  125. //关闭释放流
  126. os.close();
  127. in.close();
  128. return file;
  129. }
  130. public String getPath(String url) {
  131. return getStaticPath(COMMON_FILE) + url;
  132. }
  133. public String getDirectoryName() {
  134. return DateUtils.getNowTimeFormat("yyyyMMdd");
  135. }
  136. public String getSavePath(String directoryName) {
  137. return getStaticPath(COMMON_FILE) + FILE_SEPARATOR + directoryName + FILE_SEPARATOR;
  138. }
  139. public String createDirectory() {
  140. String directoryName = this.getDirectoryName();
  141. String savePath = this.getSavePath(directoryName);
  142. File directory = new File(savePath);
  143. if (!directory.exists()) {
  144. directory.mkdir();
  145. }
  146. return directoryName;
  147. }
  148. public String createRandomDirectory() {
  149. String directoryName = IdUtil.simpleUUID();
  150. String savePath = this.getSavePath(directoryName);
  151. File directory = new File(savePath);
  152. if (!directory.exists()) {
  153. directory.mkdir();
  154. }
  155. return directoryName;
  156. }
  157. public static FileInputStream byteToFile(byte[] bytes) {
  158. String fileName = IdUtil.simpleUUID() + ".png";
  159. File file = new File(fileName);
  160. FileInputStream fileInputStream = null;
  161. try {
  162. OutputStream output = new FileOutputStream(file);
  163. BufferedOutputStream bufferedOutput = new BufferedOutputStream(output);
  164. bufferedOutput.write(bytes);
  165. fileInputStream = new FileInputStream(file);
  166. file.deleteOnExit();
  167. return fileInputStream;
  168. } catch (FileNotFoundException e) {
  169. e.printStackTrace();
  170. } catch (IOException e) {
  171. e.printStackTrace();
  172. }
  173. return fileInputStream;
  174. }
  175. public String getTempPath(String fileName) {
  176. String tempPath = getStaticPath(COMMON_FILE) + FILE_SEPARATOR + "temp";
  177. File file = new File(tempPath);
  178. if (!file.exists()) {
  179. file.mkdir();
  180. }
  181. return tempPath + FILE_SEPARATOR + fileName;
  182. }
  183. public String getDirectory(String fileName) {
  184. return FILE_SEPARATOR + this.createDirectory() + FILE_SEPARATOR + fileName;
  185. }
  186. public String getSystemPath(String url) {
  187. return getStaticPath(COMMON_FILE) + FILE_SEPARATOR + url;
  188. }
  189. public static String getSystemPath2(String url) {
  190. return getStaticPath(COMMON_FILE) + FILE_SEPARATOR + url;
  191. }
  192. public static void writeFile(String json, String FilePath) {
  193. try {
  194. File file = new File(FilePath);
  195. // if file doesnt exists, then create it
  196. if (!file.exists()) {
  197. file.createNewFile();
  198. } else {
  199. file.delete();
  200. file.createNewFile();
  201. }
  202. // true = append file
  203. FileWriter fileWritter = new FileWriter(file, false);
  204. BufferedWriter bufferWritter = new BufferedWriter(fileWritter);
  205. bufferWritter.write(json);
  206. bufferWritter.close();
  207. } catch (IOException e) {
  208. e.printStackTrace();
  209. }
  210. }
  211. public static File getFileByName(File file, String name) {
  212. for (File file1 : file.listFiles()) {
  213. if (file1.getName().equals(name)) {
  214. return file1;
  215. }
  216. }
  217. return null;
  218. }
  219. public static void writeFile(Object object, File file) {
  220. String json = JSON.toJSONString(object);
  221. try {
  222. // if file doesnt exists, then create it
  223. if (!file.exists()) {
  224. file.createNewFile();
  225. } else {
  226. file.delete();
  227. file.createNewFile();
  228. }
  229. // true = append file
  230. FileWriter fileWritter = new FileWriter(file, false);
  231. BufferedWriter bufferWritter = new BufferedWriter(fileWritter);
  232. bufferWritter.write(json);
  233. bufferWritter.close();
  234. } catch (IOException e) {
  235. e.printStackTrace();
  236. }
  237. }
  238. public static File multipartFileToFile(MultipartFile file) throws Exception {
  239. File toFile = null;
  240. if (file == null || file.equals("") || file.getSize() <= 0) {
  241. file = null;
  242. } else {
  243. InputStream ins = null;
  244. ins = file.getInputStream();
  245. toFile = new File(file.getOriginalFilename());
  246. inputStreamToFile(ins, toFile);
  247. ins.close();
  248. }
  249. return toFile;
  250. }
  251. //获取流文件
  252. private static void inputStreamToFile(InputStream ins, File file) {
  253. try {
  254. OutputStream os = new FileOutputStream(file);
  255. int bytesRead = 0;
  256. byte[] buffer = new byte[8192];
  257. while ((bytesRead = ins.read(buffer, 0, 8192)) != -1) {
  258. os.write(buffer, 0, bytesRead);
  259. }
  260. os.close();
  261. ins.close();
  262. } catch (Exception e) {
  263. e.printStackTrace();
  264. }
  265. }
  266. }