FileUtil.java 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  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,
  151. String licensePlate) {
  152. if (StringUtils.isEmpty(url)) return "";
  153. String showPath = String.format("%s%s%s", showUrl, dir, fileName);
  154. String uploadPath = path + dir;
  155. try {
  156. //存在
  157. File existFile = new File(uploadPath + fileName);
  158. if (existFile.exists()) {
  159. log.error("存在的文件:" + existFile.getPath());
  160. log.error("存在的文件的展示地址:" + showPath);
  161. return showPath;
  162. }
  163. File folder = new File(uploadPath);
  164. if (!folder.isDirectory()) {
  165. folder.mkdirs();
  166. }
  167. URI u = URI.create(url);
  168. InputStream inputStream = u.toURL().openStream();
  169. File file = new File(uploadPath + licensePlate + ".zip");
  170. copyInputStreamToFile(inputStream, file);
  171. CompressUtil.unZip(file.getPath(), uploadPath, password, fileName, licensePlate);
  172. file.delete();
  173. return showPath;
  174. } catch (MalformedURLException e) {
  175. throw new RuntimeException(e);
  176. } catch (IOException e) {
  177. throw new RuntimeException(e);
  178. }
  179. }
  180. /**
  181. * 将 url 中的 中文转码
  182. *
  183. * @param url url链接
  184. * @return 转码后的url
  185. */
  186. public static String urlEncodeChinese(String url) {
  187. try {
  188. Matcher matcher = Pattern.compile("[\\u4e00-\\u9fa5]").matcher(url);
  189. String tmp;
  190. while (matcher.find()) {
  191. tmp = matcher.group();
  192. url = url.replaceAll(tmp, URLEncoder.encode(tmp, "UTF-8"));
  193. }
  194. } catch (UnsupportedEncodingException e) {
  195. e.printStackTrace();
  196. }
  197. return url;
  198. }
  199. /**
  200. * 网络url转文件
  201. *
  202. * @param url 保司返回的保单地址
  203. * @param dir 目录 杠结束 carInsPolicy/
  204. * @param path 文件真实目录 D:/upload/
  205. * @param showUrl 展示地址 http://sxzgkj.baoxianzhanggui.com/web-api/晋A12345/晋A12345-交强险保单.pdf
  206. * 修改 以车牌生成的保单会覆盖,同一个车牌每年会生成一个保单
  207. * 展示地址 http://sxzgkj.baoxianzhanggui.com/web-api/子订单号/子订单号-车牌号-保单号-交强险保单.pdf
  208. * @author: lig
  209. * @date: 2023年09月21日 0021
  210. */
  211. public static String netUrlToFileNew(String url, String dir, String fileName, String path, String showUrl,boolean flag) {
  212. // String s = "http://eserviceinterface.ciitc.com.cn/ePolicyServices/insurancePolicy.do?downFiles=key&amp&pkid=a7370fea493945868b8ca847d3ef9d9e";
  213. // String path = "D:/upload/";
  214. // String dir = "carInsPolicy/";
  215. if (StringUtils.isEmpty(url)) return "";
  216. String showPath = String.format("%s%s%s", showUrl, dir, fileName);
  217. String uploadPath = path + dir;
  218. try {
  219. //存在
  220. File existFile = new File(uploadPath + fileName);
  221. if (existFile.exists()) {
  222. if(!flag){
  223. log.error("存在的文件:" + existFile.getPath());
  224. log.error("存在的文件的展示地址:" + showPath);
  225. return showPath;
  226. }else{
  227. //刪除重新生成
  228. existFile.delete();
  229. existFile = new File(uploadPath + fileName);
  230. }
  231. }
  232. File folder = new File(uploadPath);
  233. if (!folder.isDirectory()) {
  234. folder.mkdirs();
  235. }
  236. String encodeUrl = urlEncodeChinese(url);
  237. URI u = URI.create(encodeUrl);
  238. HtppsUtils.disableSSLCertificateChecking();
  239. InputStream inputStream = u.toURL().openStream();
  240. File file = new File(uploadPath + fileName);
  241. copyInputStreamToFile(inputStream, file);
  242. log.error("文件路径:" + file.getPath());
  243. return showPath;
  244. } catch (MalformedURLException e) {
  245. throw new RuntimeException(e);
  246. } catch (IOException e) {
  247. throw new RuntimeException(e);
  248. }
  249. }
  250. public static String netUrlStoreToLocalNew(String url, String dir, String fileName, String path, String showUrl, String password,
  251. String licensePlate,boolean flag) {
  252. if (StringUtils.isEmpty(url)) return "";
  253. String showPath = String.format("%s%s%s", showUrl, dir, fileName);
  254. String uploadPath = path + dir;
  255. try {
  256. //存在
  257. File existFile = new File(uploadPath + fileName);
  258. if (existFile.exists()) {
  259. if(!flag){
  260. log.error("存在的文件:" + existFile.getPath());
  261. log.error("存在的文件的展示地址:" + showPath);
  262. return showPath;
  263. }else{
  264. //刪除重新生成
  265. existFile.delete();
  266. existFile = new File(uploadPath + fileName);
  267. }
  268. }
  269. File folder = new File(uploadPath);
  270. if (!folder.isDirectory()) {
  271. folder.mkdirs();
  272. }
  273. URI u = URI.create(url);
  274. InputStream inputStream = u.toURL().openStream();
  275. File file = new File(uploadPath + licensePlate + ".zip");
  276. copyInputStreamToFile(inputStream, file);
  277. CompressUtil.unZip(file.getPath(), uploadPath, password, fileName, licensePlate);
  278. file.delete();
  279. return showPath;
  280. } catch (MalformedURLException e) {
  281. throw new RuntimeException(e);
  282. } catch (IOException e) {
  283. throw new RuntimeException(e);
  284. }
  285. }
  286. }