FileUtil.java 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  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. * 修改 以车牌生成的保单会覆盖,同一个车牌每年会生成一个保单
  80. * 展示地址 http://sxzgkj.baoxianzhanggui.com/web-api/子订单号/子订单号-车牌号-保单号-交强险保单.pdf
  81. * @author: lig
  82. * @date: 2023年09月21日 0021
  83. */
  84. public static String netUrlToFile(String url, String dir, String fileName, String path, String showUrl) {
  85. // String s = "http://eserviceinterface.ciitc.com.cn/ePolicyServices/insurancePolicy.do?downFiles=key&amp&pkid=a7370fea493945868b8ca847d3ef9d9e";
  86. // String path = "D:/upload/";
  87. // String dir = "carInsPolicy/";
  88. if (StringUtils.isEmpty(url)) return "";
  89. String showPath = String.format("%s%s%s", showUrl, dir, fileName);
  90. String uploadPath = path + dir;
  91. try {
  92. //存在
  93. File existFile = new File(uploadPath + fileName);
  94. if (existFile.exists()) {
  95. log.error("存在的文件:" + existFile.getPath());
  96. log.error("存在的文件的展示地址:" + showPath);
  97. return showPath;
  98. }
  99. File folder = new File(uploadPath);
  100. if (!folder.isDirectory()) {
  101. folder.mkdirs();
  102. }
  103. String encodeUrl = urlEncodeChinese(url);
  104. URI u = URI.create(encodeUrl);
  105. InputStream inputStream = u.toURL().openStream();
  106. File file = new File(uploadPath + fileName);
  107. copyInputStreamToFile(inputStream, file);
  108. log.error("文件路径:" + file.getPath());
  109. return showPath;
  110. } catch (MalformedURLException e) {
  111. throw new RuntimeException(e);
  112. } catch (IOException e) {
  113. throw new RuntimeException(e);
  114. }
  115. }
  116. /**
  117. * 文件存在
  118. *
  119. * @param dir 目录 杠结束 carInsPolicy/
  120. * @param path 文件真实目录 D:/upload/
  121. * @param showUrl 展示地址 http://sxzgkj.baoxianzhanggui.com/web-api/晋A12345/晋A12345-交强险保单.pdf
  122. * @author: lig
  123. * @date: 2023年09月21日 0021
  124. */
  125. public static String fileExist(String dir, String fileName, String path, String showUrl) {
  126. String showPath = String.format("%s%s%s", showUrl, dir, fileName);
  127. String uploadPath = path + dir;
  128. //存在
  129. File existFile = new File(uploadPath + fileName);
  130. if (existFile.exists()) {
  131. log.error("存在的文件:" + existFile.getPath());
  132. log.error("存在的文件的展示地址:" + showPath);
  133. return showPath;
  134. }
  135. return "";
  136. }
  137. // InputStream -> File
  138. public static void copyInputStreamToFile(InputStream inputStream, File file)
  139. throws IOException {
  140. try (FileOutputStream outputStream = new FileOutputStream(file)) {
  141. int read;
  142. byte[] bytes = new byte[1024];
  143. while ((read = inputStream.read(bytes)) != -1) {
  144. outputStream.write(bytes, 0, read);
  145. }
  146. // commons-io
  147. //IOUtils.copy(inputStream, outputStream);
  148. }
  149. }
  150. public static String netUrlStoreToLocal(String url, String dir, String fileName, String path, String showUrl, String password,String licensePlate,Boolean isBz) {
  151. if (StringUtils.isEmpty(url)) return "";
  152. String showPath = String.format("%s%s%s", showUrl, dir, fileName);
  153. String uploadPath = path + dir;
  154. try {
  155. //存在
  156. File existFile = new File(uploadPath + fileName);
  157. if (existFile.exists()) {
  158. log.error("存在的文件:" + existFile.getPath());
  159. log.error("存在的文件的展示地址:" + showPath);
  160. return showPath;
  161. }
  162. File folder = new File(uploadPath);
  163. if (!folder.isDirectory()) {
  164. folder.mkdirs();
  165. }
  166. URI u = URI.create(url);
  167. InputStream inputStream = u.toURL().openStream();
  168. File file = new File(uploadPath + licensePlate + ".zip");
  169. copyInputStreamToFile(inputStream, file);
  170. CompressUtil.unZip(file.getPath(), uploadPath, password, fileName, licensePlate,isBz);
  171. file.delete();
  172. return showPath;
  173. } catch (MalformedURLException e) {
  174. throw new RuntimeException(e);
  175. } catch (IOException e) {
  176. throw new RuntimeException(e);
  177. }
  178. }
  179. /**
  180. * 将 url 中的 中文转码
  181. *
  182. * @param url url链接
  183. * @return 转码后的url
  184. */
  185. public static String urlEncodeChinese(String url) {
  186. try {
  187. Matcher matcher = Pattern.compile("[\\u4e00-\\u9fa5]").matcher(url);
  188. String tmp;
  189. while (matcher.find()) {
  190. tmp = matcher.group();
  191. url = url.replaceAll(tmp, URLEncoder.encode(tmp, "UTF-8"));
  192. }
  193. } catch (UnsupportedEncodingException e) {
  194. e.printStackTrace();
  195. }
  196. return url;
  197. }
  198. /**
  199. * 网络url转文件
  200. *
  201. * @param url 保司返回的保单地址
  202. * @param dir 目录 杠结束 carInsPolicy/
  203. * @param path 文件真实目录 D:/upload/
  204. * @param showUrl 展示地址 http://sxzgkj.baoxianzhanggui.com/web-api/晋A12345/晋A12345-交强险保单.pdf
  205. * 修改 以车牌生成的保单会覆盖,同一个车牌每年会生成一个保单
  206. * 展示地址 http://sxzgkj.baoxianzhanggui.com/web-api/子订单号/子订单号-车牌号-保单号-交强险保单.pdf
  207. * @author: lig
  208. * @date: 2023年09月21日 0021
  209. */
  210. public static String netUrlToFileNew(String url, String dir, String fileName, String path, String showUrl,boolean flag) {
  211. // String s = "http://eserviceinterface.ciitc.com.cn/ePolicyServices/insurancePolicy.do?downFiles=key&amp&pkid=a7370fea493945868b8ca847d3ef9d9e";
  212. // String path = "D:/upload/";
  213. // String dir = "carInsPolicy/";
  214. if (StringUtils.isEmpty(url)) return "";
  215. String showPath = String.format("%s%s%s", showUrl, dir, fileName);
  216. String uploadPath = path + dir;
  217. try {
  218. //存在
  219. File existFile = new File(uploadPath + fileName);
  220. if (existFile.exists()) {
  221. if(!flag){
  222. log.error("存在的文件:" + existFile.getPath());
  223. log.error("存在的文件的展示地址:" + showPath);
  224. return showPath;
  225. }else{
  226. //刪除重新生成
  227. existFile.delete();
  228. existFile = new File(uploadPath + fileName);
  229. }
  230. }
  231. File folder = new File(uploadPath);
  232. if (!folder.isDirectory()) {
  233. folder.mkdirs();
  234. }
  235. String encodeUrl = urlEncodeChinese(url);
  236. URI u = URI.create(encodeUrl);
  237. HtppsUtils.disableSSLCertificateChecking();
  238. InputStream inputStream = u.toURL().openStream();
  239. File file = new File(uploadPath + fileName);
  240. copyInputStreamToFile(inputStream, file);
  241. log.error("文件路径:" + file.getPath());
  242. return showPath;
  243. } catch (MalformedURLException e) {
  244. throw new RuntimeException(e);
  245. } catch (IOException e) {
  246. throw new RuntimeException(e);
  247. }
  248. }
  249. public static String netUrlStoreToLocalNew(String url, String dir, String fileName, String path, String showUrl, String password,String licensePlate,boolean flag,Boolean isBz) {
  250. if (StringUtils.isEmpty(url)){ return "";}
  251. String showPath = String.format("%s%s%s", showUrl, dir, fileName);
  252. String uploadPath = path + dir;
  253. try {
  254. //存在
  255. File existFile = new File(uploadPath + fileName);
  256. if (existFile.exists()) {
  257. if(!flag){
  258. log.error("存在的文件:" + existFile.getPath());
  259. log.error("存在的文件的展示地址:" + showPath);
  260. return showPath;
  261. }else{
  262. //刪除重新生成
  263. existFile.delete();
  264. existFile = new File(uploadPath + fileName);
  265. }
  266. }
  267. File folder = new File(uploadPath);
  268. if (!folder.isDirectory()) {
  269. folder.mkdirs();
  270. }
  271. URI u = URI.create(url);
  272. InputStream inputStream = u.toURL().openStream();
  273. File file = new File(uploadPath + licensePlate + ".zip");
  274. copyInputStreamToFile(inputStream, file);
  275. CompressUtil.unZip(file.getPath(), uploadPath, password, fileName, licensePlate,isBz);
  276. file.delete();
  277. return showPath;
  278. } catch (MalformedURLException e) {
  279. throw new RuntimeException(e);
  280. } catch (IOException e) {
  281. throw new RuntimeException(e);
  282. }
  283. }
  284. }