FileUtil.java 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. package com.ydtech.utils.baidu;
  2. import com.ydtech.modules.ins.utils.CompressUtil;
  3. import com.ydtech.utils.StringUtils;
  4. import lombok.extern.slf4j.Slf4j;
  5. import java.io.*;
  6. import java.net.MalformedURLException;
  7. import java.net.URI;
  8. import java.net.URLEncoder;
  9. import java.util.regex.Matcher;
  10. import java.util.regex.Pattern;
  11. /**
  12. * 文件读取工具类
  13. */
  14. @Slf4j
  15. public class FileUtil {
  16. /**
  17. * 读取文件内容,作为字符串返回
  18. */
  19. public static String readFileAsString(String filePath) throws IOException {
  20. File file = new File(filePath);
  21. if (!file.exists()) {
  22. throw new FileNotFoundException(filePath);
  23. }
  24. if (file.length() > 1024 * 1024 * 1024) {
  25. throw new IOException("File is too large");
  26. }
  27. StringBuilder sb = new StringBuilder((int) (file.length()));
  28. // 创建字节输入流
  29. FileInputStream fis = new FileInputStream(filePath);
  30. // 创建一个长度为10240的Buffer
  31. byte[] bbuf = new byte[10240];
  32. // 用于保存实际读取的字节数
  33. int hasRead = 0;
  34. while ((hasRead = fis.read(bbuf)) > 0) {
  35. sb.append(new String(bbuf, 0, hasRead));
  36. }
  37. fis.close();
  38. return sb.toString();
  39. }
  40. /**
  41. * 根据文件路径读取byte[] 数组
  42. */
  43. public static byte[] readFileByBytes(String filePath) throws IOException {
  44. File file = new File(filePath);
  45. if (!file.exists()) {
  46. throw new FileNotFoundException(filePath);
  47. } else {
  48. ByteArrayOutputStream bos = new ByteArrayOutputStream((int) file.length());
  49. BufferedInputStream in = null;
  50. try {
  51. in = new BufferedInputStream(new FileInputStream(file));
  52. short bufSize = 1024;
  53. byte[] buffer = new byte[bufSize];
  54. int len1;
  55. while (-1 != (len1 = in.read(buffer, 0, bufSize))) {
  56. bos.write(buffer, 0, len1);
  57. }
  58. byte[] var7 = bos.toByteArray();
  59. return var7;
  60. } finally {
  61. try {
  62. if (in != null) {
  63. in.close();
  64. }
  65. } catch (IOException var14) {
  66. var14.printStackTrace();
  67. }
  68. bos.close();
  69. }
  70. }
  71. }
  72. /**
  73. * 网络url转文件
  74. *
  75. * @param url 保司返回的保单地址
  76. * @param dir 目录 杠结束 carInsPolicy/
  77. * @param path 文件真实目录 D:/upload/
  78. * @param showUrl 展示地址 http://sxzgkj.baoxianzhanggui.com/web-api/晋A12345/晋A12345-交强险保单.pdf
  79. * @author: lig
  80. * @date: 2023年09月21日 0021
  81. */
  82. public static String netUrlToFile(String url, String dir, String fileName, String path, String showUrl) {
  83. // String s = "http://eserviceinterface.ciitc.com.cn/ePolicyServices/insurancePolicy.do?downFiles=key&amp&pkid=a7370fea493945868b8ca847d3ef9d9e";
  84. // String path = "D:/upload/";
  85. // String dir = "carInsPolicy/";
  86. if (StringUtils.isEmpty(url)) return "";
  87. String showPath = String.format("%s%s%s", showUrl, dir, fileName);
  88. String uploadPath = path + dir;
  89. try {
  90. //存在
  91. File existFile = new File(uploadPath + fileName);
  92. if (existFile.exists()) {
  93. log.error("存在的文件:" + existFile.getPath());
  94. log.error("存在的文件的展示地址:" + showPath);
  95. return showPath;
  96. }
  97. File folder = new File(uploadPath);
  98. if (!folder.isDirectory()) {
  99. folder.mkdirs();
  100. }
  101. String encodeUrl = urlEncodeChinese(url);
  102. URI u = URI.create(encodeUrl);
  103. InputStream inputStream = u.toURL().openStream();
  104. File file = new File(uploadPath + fileName);
  105. copyInputStreamToFile(inputStream, file);
  106. log.error("文件路径:" + file.getPath());
  107. return showPath;
  108. } catch (MalformedURLException e) {
  109. throw new RuntimeException(e);
  110. } catch (IOException e) {
  111. throw new RuntimeException(e);
  112. }
  113. }
  114. /**
  115. * 文件存在
  116. *
  117. * @param dir 目录 杠结束 carInsPolicy/
  118. * @param path 文件真实目录 D:/upload/
  119. * @param showUrl 展示地址 http://sxzgkj.baoxianzhanggui.com/web-api/晋A12345/晋A12345-交强险保单.pdf
  120. * @author: lig
  121. * @date: 2023年09月21日 0021
  122. */
  123. public static String fileExist(String dir, String fileName, String path, String showUrl) {
  124. String showPath = String.format("%s%s%s", showUrl, dir, fileName);
  125. String uploadPath = path + dir;
  126. //存在
  127. File existFile = new File(uploadPath + fileName);
  128. if (existFile.exists()) {
  129. log.error("存在的文件:" + existFile.getPath());
  130. log.error("存在的文件的展示地址:" + showPath);
  131. return showPath;
  132. }
  133. return "";
  134. }
  135. // InputStream -> File
  136. private static void copyInputStreamToFile(InputStream inputStream, File file)
  137. throws IOException {
  138. try (FileOutputStream outputStream = new FileOutputStream(file)) {
  139. int read;
  140. byte[] bytes = new byte[1024];
  141. while ((read = inputStream.read(bytes)) != -1) {
  142. outputStream.write(bytes, 0, read);
  143. }
  144. // commons-io
  145. //IOUtils.copy(inputStream, outputStream);
  146. }
  147. }
  148. public static String netUrlStoreToLocal(String url, String dir, String fileName, String path, String showUrl, String password,
  149. String licensePlate) {
  150. if (StringUtils.isEmpty(url)) return "";
  151. String showPath = String.format("%s%s%s", showUrl, dir, fileName);
  152. String uploadPath = path + dir;
  153. try {
  154. //存在
  155. File existFile = new File(uploadPath + fileName);
  156. if (existFile.exists()) {
  157. log.error("存在的文件:" + existFile.getPath());
  158. log.error("存在的文件的展示地址:" + showPath);
  159. return showPath;
  160. }
  161. File folder = new File(uploadPath);
  162. if (!folder.isDirectory()) {
  163. folder.mkdirs();
  164. }
  165. URI u = URI.create(url);
  166. InputStream inputStream = u.toURL().openStream();
  167. File file = new File(uploadPath + licensePlate + ".zip");
  168. copyInputStreamToFile(inputStream, file);
  169. CompressUtil.unZip(file.getPath(), uploadPath, password, fileName, licensePlate);
  170. file.delete();
  171. return showPath;
  172. } catch (MalformedURLException e) {
  173. throw new RuntimeException(e);
  174. } catch (IOException e) {
  175. throw new RuntimeException(e);
  176. }
  177. }
  178. /**
  179. * 将 url 中的 中文转码
  180. *
  181. * @param url url链接
  182. * @return 转码后的url
  183. */
  184. public static String urlEncodeChinese(String url) {
  185. try {
  186. Matcher matcher = Pattern.compile("[\\u4e00-\\u9fa5]").matcher(url);
  187. String tmp;
  188. while (matcher.find()) {
  189. tmp = matcher.group();
  190. url = url.replaceAll(tmp, URLEncoder.encode(tmp, "UTF-8"));
  191. }
  192. } catch (UnsupportedEncodingException e) {
  193. e.printStackTrace();
  194. }
  195. return url;
  196. }
  197. }