FileUtils.java 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. package cn.cslg.pas.common.utils;
  2. import cn.cslg.pas.Application;
  3. import cn.cslg.pas.common.vo.business.PatentProjectVO;
  4. import cn.hutool.core.util.IdUtil;
  5. import org.apache.commons.fileupload.FileItem;
  6. import org.apache.commons.fileupload.disk.DiskFileItemFactory;
  7. import org.springframework.boot.system.ApplicationHome;
  8. import org.springframework.stereotype.Service;
  9. import org.springframework.web.multipart.MultipartFile;
  10. import java.io.*;
  11. import java.net.MalformedURLException;
  12. import java.net.URL;
  13. import java.nio.charset.StandardCharsets;
  14. @Service
  15. public class FileUtils {
  16. public static final String FILE_SEPARATOR = System.getProperty("file.separator");
  17. public static final String COMMON_FILE = "file";
  18. public static final String BACKUP_FILE = "backup";
  19. public static String getStaticPath(String fileName) {
  20. //ApplicationHome类 返回target目录层级
  21. ApplicationHome ah = new ApplicationHome(Application.class);
  22. //获取 applicationHome 内的路径 ...\target\classes 到这一层级下
  23. File file = ah.getSource();
  24. //获取 file的parentFile 即最后一级之前的所有层级路径(包括盘符) 这里能获得到的最终层级为 ...\target 后续用FILE_SEPARATOR(系统路径分割通配符 即 "\") 以及fileName拼接生成存放文件的目录层级 即为根目录 root
  25. String rootPath =null;
  26. if (fileName!=null&&!fileName.equals(""))
  27. {
  28. rootPath= file.getParentFile().toString() + FILE_SEPARATOR + fileName;
  29. }
  30. else {
  31. rootPath = file.getParentFile().toString();
  32. }
  33. //根据上方生成的根目录路径 生成对应文件夹 没有就新建
  34. File root = new File(rootPath);
  35. if (!root.exists()) {
  36. root.mkdir();
  37. }
  38. //返回的最终形式为 盘符:\项目层级\target\file
  39. return rootPath;
  40. }
  41. public String analysisJsonFile() {
  42. ApplicationHome ah = new ApplicationHome(BackupUtils.class);
  43. File file = ah.getSource();
  44. String settingFilePath = file.getParentFile().toString() + FileUtils.FILE_SEPARATOR + "uploadSetting.json";
  45. BufferedReader reader = null;
  46. StringBuilder last = new StringBuilder();
  47. InputStreamReader inputStreamReader;
  48. try (FileInputStream fileInputStream = new FileInputStream(settingFilePath)) {
  49. inputStreamReader = new InputStreamReader(fileInputStream, StandardCharsets.UTF_8);
  50. reader = new BufferedReader(inputStreamReader);
  51. String tempString;
  52. while ((tempString = reader.readLine()) != null) {
  53. last.append(tempString);
  54. }
  55. reader.close();
  56. } catch (IOException e) {
  57. e.printStackTrace();
  58. } finally {
  59. if (reader != null) {
  60. try {
  61. reader.close();
  62. } catch (IOException e) {
  63. e.printStackTrace();
  64. }
  65. }
  66. }
  67. return last.toString();
  68. }
  69. public static File getFileByBytes(byte[] bytes, String prefix, String suffix) {
  70. BufferedOutputStream bos = null;
  71. FileOutputStream fos = null;
  72. File file = null;
  73. try {
  74. file = File.createTempFile(prefix, suffix);
  75. //输出流
  76. fos = new FileOutputStream(file);
  77. //缓冲流
  78. bos = new BufferedOutputStream(fos);
  79. //将字节数组写出
  80. bos.write(bytes);
  81. return file;
  82. } catch (Exception e) {
  83. e.printStackTrace();
  84. } finally {
  85. if (bos != null) {
  86. try {
  87. bos.close();
  88. } catch (IOException e) {
  89. e.printStackTrace();
  90. }
  91. }
  92. if (fos != null) {
  93. try {
  94. fos.close();
  95. } catch (IOException e) {
  96. e.printStackTrace();
  97. }
  98. }
  99. return file;
  100. }
  101. }
  102. public static File getFileByUrl(String dataUrl) throws IOException {
  103. URL url = new URL(dataUrl); //想要读取的url地址
  104. InputStream in = url.openStream();
  105. File file = File.createTempFile("new_url", ".pdf"); //创建文件
  106. OutputStream os = new FileOutputStream(file); //创建文件输出流
  107. int bytesRead;
  108. byte[] buffer = new byte[8192];
  109. int len = 8192;
  110. while ((bytesRead = in.read(buffer, 0, len)) != -1) {
  111. os.write(buffer, 0, bytesRead);
  112. }
  113. //关闭释放流
  114. os.close();
  115. in.close();
  116. return file;
  117. }
  118. public static File getPictureFileByUrl(String dataUrl) throws IOException {
  119. URL url = new URL(dataUrl); //想要读取的url地址
  120. InputStream in = url.openStream();
  121. File file = File.createTempFile("new_url", ".jpg"); //创建文件
  122. OutputStream os = new FileOutputStream(file); //创建文件输出流
  123. int bytesRead;
  124. byte[] buffer = new byte[8192];
  125. int len = 8192;
  126. while ((bytesRead = in.read(buffer, 0, len)) != -1) {
  127. os.write(buffer, 0, bytesRead);
  128. }
  129. //关闭释放流
  130. os.close();
  131. in.close();
  132. return file;
  133. }
  134. public String getPath(String url) {
  135. return getStaticPath(COMMON_FILE) + url;
  136. }
  137. public String getDirectoryName() {
  138. return DateUtils.getNowTimeFormat("yyyyMMdd");
  139. }
  140. public String getSavePath(String directoryName) {
  141. return getStaticPath(COMMON_FILE) + FILE_SEPARATOR + directoryName + FILE_SEPARATOR;
  142. }
  143. public String createDirectory() {
  144. String directoryName = this.getDirectoryName();
  145. String savePath = this.getSavePath(directoryName);
  146. File directory = new File(savePath);
  147. if (!directory.exists()) {
  148. directory.mkdir();
  149. }
  150. return directoryName;
  151. }
  152. public String createRandomDirectory() {
  153. String directoryName = IdUtil.simpleUUID();
  154. String savePath = this.getSavePath(directoryName);
  155. File directory = new File(savePath);
  156. if (!directory.exists()) {
  157. directory.mkdir();
  158. }
  159. return directoryName;
  160. }
  161. public static FileInputStream byteToFile(byte[] bytes) {
  162. String fileName = IdUtil.simpleUUID() + ".png";
  163. File file = new File(fileName);
  164. FileInputStream fileInputStream = null;
  165. try {
  166. OutputStream output = new FileOutputStream(file);
  167. BufferedOutputStream bufferedOutput = new BufferedOutputStream(output);
  168. bufferedOutput.write(bytes);
  169. fileInputStream = new FileInputStream(file);
  170. file.deleteOnExit();
  171. return fileInputStream;
  172. } catch (FileNotFoundException e) {
  173. e.printStackTrace();
  174. } catch (IOException e) {
  175. e.printStackTrace();
  176. }
  177. return fileInputStream;
  178. }
  179. public String getTempPath(String fileName) {
  180. String tempPath = getStaticPath(COMMON_FILE) + FILE_SEPARATOR + "temp";
  181. File file = new File(tempPath);
  182. if (!file.exists()) {
  183. file.mkdir();
  184. }
  185. return tempPath + FILE_SEPARATOR + fileName;
  186. }
  187. public String getDirectory(String fileName) {
  188. return FILE_SEPARATOR + this.createDirectory() + FILE_SEPARATOR + fileName;
  189. }
  190. public String getSystemPath(String url) {
  191. return getStaticPath(COMMON_FILE) + FILE_SEPARATOR + url;
  192. }
  193. public static void writeObjectToFile(Object object, String url) {
  194. //根据projectId查询对象
  195. try {
  196. ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(url));
  197. oos.writeObject(object);
  198. } catch (Exception e) {
  199. e.printStackTrace();
  200. }
  201. }
  202. public String readJson(String jsonPath) {
  203. File jsonFile = new File(jsonPath);
  204. try {
  205. FileReader fileReader = new FileReader(jsonFile);
  206. BufferedReader reader = new BufferedReader(fileReader);
  207. StringBuilder sb = new StringBuilder();
  208. while (true) {
  209. int ch = reader.read();
  210. if (ch != -1) {
  211. sb.append((char) ch);
  212. } else {
  213. break;
  214. }
  215. }
  216. fileReader.close();
  217. reader.close();
  218. return sb.toString();
  219. } catch (IOException e) {
  220. return "";
  221. }
  222. }
  223. public static String writeJson(String jsonPath, Object object) {
  224. // Map数据转化为Json,再转换为String
  225. String data =JsonUtils.objectToJson(object);
  226. File jsonFile = new File(jsonPath);
  227. try {
  228. // 文件不存在就创建文件
  229. if (!jsonFile.exists()) {
  230. jsonFile.createNewFile();
  231. }
  232. FileWriter fileWriter = new FileWriter(jsonFile.getAbsoluteFile(),false);
  233. BufferedWriter bw = new BufferedWriter(fileWriter);
  234. bw.write(data);
  235. bw.close();
  236. return "success";
  237. } catch (IOException e) {
  238. return "error";
  239. }
  240. }
  241. }