| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341 |
- package com.ydtech.utils.baidu;
- import com.ydtech.modules.ins.utils.CompressUtil;
- import com.ydtech.utils.StringUtils;
- import lombok.extern.slf4j.Slf4j;
- import java.io.*;
- import java.net.MalformedURLException;
- import java.net.URI;
- import java.net.URLEncoder;
- import java.util.regex.Matcher;
- import java.util.regex.Pattern;
- /**
- * 文件读取工具类
- */
- @Slf4j
- public class FileUtil {
- /**
- * 读取文件内容,作为字符串返回
- */
- public static String readFileAsString(String filePath) throws IOException {
- File file = new File(filePath);
- if (!file.exists()) {
- throw new FileNotFoundException(filePath);
- }
- if (file.length() > 1024 * 1024 * 1024) {
- throw new IOException("File is too large");
- }
- StringBuilder sb = new StringBuilder((int) (file.length()));
- // 创建字节输入流
- FileInputStream fis = new FileInputStream(filePath);
- // 创建一个长度为10240的Buffer
- byte[] bbuf = new byte[10240];
- // 用于保存实际读取的字节数
- int hasRead = 0;
- while ((hasRead = fis.read(bbuf)) > 0) {
- sb.append(new String(bbuf, 0, hasRead));
- }
- fis.close();
- return sb.toString();
- }
- /**
- * 根据文件路径读取byte[] 数组
- */
- public static byte[] readFileByBytes(String filePath) throws IOException {
- File file = new File(filePath);
- if (!file.exists()) {
- throw new FileNotFoundException(filePath);
- } else {
- ByteArrayOutputStream bos = new ByteArrayOutputStream((int) file.length());
- BufferedInputStream in = null;
- try {
- in = new BufferedInputStream(new FileInputStream(file));
- short bufSize = 1024;
- byte[] buffer = new byte[bufSize];
- int len1;
- while (-1 != (len1 = in.read(buffer, 0, bufSize))) {
- bos.write(buffer, 0, len1);
- }
- byte[] var7 = bos.toByteArray();
- return var7;
- } finally {
- try {
- if (in != null) {
- in.close();
- }
- } catch (IOException var14) {
- var14.printStackTrace();
- }
- bos.close();
- }
- }
- }
- /**
- * 网络url转文件
- *
- * @param url 保司返回的保单地址
- * @param dir 目录 杠结束 carInsPolicy/
- * @param path 文件真实目录 D:/upload/
- * @param showUrl 展示地址 http://sxzgkj.baoxianzhanggui.com/web-api/晋A12345/晋A12345-交强险保单.pdf
- * 修改 以车牌生成的保单会覆盖,同一个车牌每年会生成一个保单
- * 展示地址 http://sxzgkj.baoxianzhanggui.com/web-api/子订单号/子订单号-车牌号-保单号-交强险保单.pdf
- * @author: lig
- * @date: 2023年09月21日 0021
- */
- public static String netUrlToFile(String url, String dir, String fileName, String path, String showUrl) {
- // String s = "http://eserviceinterface.ciitc.com.cn/ePolicyServices/insurancePolicy.do?downFiles=key&&pkid=a7370fea493945868b8ca847d3ef9d9e";
- // String path = "D:/upload/";
- // String dir = "carInsPolicy/";
- if (StringUtils.isEmpty(url)) return "";
- String showPath = String.format("%s%s%s", showUrl, dir, fileName);
- String uploadPath = path + dir;
- try {
- //存在
- File existFile = new File(uploadPath + fileName);
- if (existFile.exists()) {
- log.error("存在的文件:" + existFile.getPath());
- log.error("存在的文件的展示地址:" + showPath);
- return showPath;
- }
- File folder = new File(uploadPath);
- if (!folder.isDirectory()) {
- folder.mkdirs();
- }
- String encodeUrl = urlEncodeChinese(url);
- URI u = URI.create(encodeUrl);
- InputStream inputStream = u.toURL().openStream();
- File file = new File(uploadPath + fileName);
- copyInputStreamToFile(inputStream, file);
- log.error("文件路径:" + file.getPath());
- return showPath;
- } catch (MalformedURLException e) {
- throw new RuntimeException(e);
- } catch (IOException e) {
- throw new RuntimeException(e);
- }
- }
- /**
- * 文件存在
- *
- * @param dir 目录 杠结束 carInsPolicy/
- * @param path 文件真实目录 D:/upload/
- * @param showUrl 展示地址 http://sxzgkj.baoxianzhanggui.com/web-api/晋A12345/晋A12345-交强险保单.pdf
- * @author: lig
- * @date: 2023年09月21日 0021
- */
- public static String fileExist(String dir, String fileName, String path, String showUrl) {
- String showPath = String.format("%s%s%s", showUrl, dir, fileName);
- String uploadPath = path + dir;
- //存在
- File existFile = new File(uploadPath + fileName);
- if (existFile.exists()) {
- log.error("存在的文件:" + existFile.getPath());
- log.error("存在的文件的展示地址:" + showPath);
- return showPath;
- }
- return "";
- }
- // InputStream -> File
- public static void copyInputStreamToFile(InputStream inputStream, File file)
- throws IOException {
- try (FileOutputStream outputStream = new FileOutputStream(file)) {
- int read;
- byte[] bytes = new byte[1024];
- while ((read = inputStream.read(bytes)) != -1) {
- outputStream.write(bytes, 0, read);
- }
- // commons-io
- //IOUtils.copy(inputStream, outputStream);
- }
- }
- public static String netUrlStoreToLocal(String url, String dir, String fileName, String path, String showUrl, String password,String licensePlate,Boolean isBz) {
- if (StringUtils.isEmpty(url)) return "";
- String showPath = String.format("%s%s%s", showUrl, dir, fileName);
- String uploadPath = path + dir;
- try {
- //存在
- File existFile = new File(uploadPath + fileName);
- if (existFile.exists()) {
- log.error("存在的文件:" + existFile.getPath());
- log.error("存在的文件的展示地址:" + showPath);
- return showPath;
- }
- File folder = new File(uploadPath);
- if (!folder.isDirectory()) {
- folder.mkdirs();
- }
- URI u = URI.create(url);
- InputStream inputStream = u.toURL().openStream();
- File file = new File(uploadPath + licensePlate + ".zip");
- copyInputStreamToFile(inputStream, file);
- CompressUtil.unZip(file.getPath(), uploadPath, password, fileName, licensePlate,isBz);
- file.delete();
- return showPath;
- } catch (MalformedURLException e) {
- throw new RuntimeException(e);
- } catch (IOException e) {
- throw new RuntimeException(e);
- }
- }
- /**
- * 将 url 中的 中文转码
- *
- * @param url url链接
- * @return 转码后的url
- */
- public static String urlEncodeChinese(String url) {
- try {
- Matcher matcher = Pattern.compile("[\\u4e00-\\u9fa5]").matcher(url);
- String tmp;
- while (matcher.find()) {
- tmp = matcher.group();
- url = url.replaceAll(tmp, URLEncoder.encode(tmp, "UTF-8"));
- }
- } catch (UnsupportedEncodingException e) {
- e.printStackTrace();
- }
- return url;
- }
- /**
- * 网络url转文件
- *
- * @param url 保司返回的保单地址
- * @param dir 目录 杠结束 carInsPolicy/
- * @param path 文件真实目录 D:/upload/
- * @param showUrl 展示地址 http://sxzgkj.baoxianzhanggui.com/web-api/晋A12345/晋A12345-交强险保单.pdf
- * 修改 以车牌生成的保单会覆盖,同一个车牌每年会生成一个保单
- * 展示地址 http://sxzgkj.baoxianzhanggui.com/web-api/子订单号/子订单号-车牌号-保单号-交强险保单.pdf
- * @author: lig
- * @date: 2023年09月21日 0021
- */
- public static String netUrlToFileNew(String url, String dir, String fileName, String path, String showUrl,boolean flag) {
- // String s = "http://eserviceinterface.ciitc.com.cn/ePolicyServices/insurancePolicy.do?downFiles=key&&pkid=a7370fea493945868b8ca847d3ef9d9e";
- // String path = "D:/upload/";
- // String dir = "carInsPolicy/";
- if (StringUtils.isEmpty(url)) return "";
- String showPath = String.format("%s%s%s", showUrl, dir, fileName);
- String uploadPath = path + dir;
- try {
- //存在
- File existFile = new File(uploadPath + fileName);
- if (existFile.exists()) {
- if(!flag){
- log.error("存在的文件:" + existFile.getPath());
- log.error("存在的文件的展示地址:" + showPath);
- return showPath;
- }else{
- //刪除重新生成
- existFile.delete();
- existFile = new File(uploadPath + fileName);
- }
- }
- File folder = new File(uploadPath);
- if (!folder.isDirectory()) {
- folder.mkdirs();
- }
- String encodeUrl = urlEncodeChinese(url);
- URI u = URI.create(encodeUrl);
- HtppsUtils.disableSSLCertificateChecking();
- InputStream inputStream = u.toURL().openStream();
- File file = new File(uploadPath + fileName);
- copyInputStreamToFile(inputStream, file);
- log.error("文件路径:" + file.getPath());
- return showPath;
- } catch (MalformedURLException e) {
- throw new RuntimeException(e);
- } catch (IOException e) {
- throw new RuntimeException(e);
- }
- }
- public static String netUrlStoreToLocalNew(String url, String dir, String fileName, String path, String showUrl, String password,String licensePlate,boolean flag,Boolean isBz) {
- if (StringUtils.isEmpty(url)){ return "";}
- String showPath = String.format("%s%s%s", showUrl, dir, fileName);
- String uploadPath = path + dir;
- try {
- //存在
- File existFile = new File(uploadPath + fileName);
- if (existFile.exists()) {
- if(!flag){
- log.error("存在的文件:" + existFile.getPath());
- log.error("存在的文件的展示地址:" + showPath);
- return showPath;
- }else{
- //刪除重新生成
- existFile.delete();
- existFile = new File(uploadPath + fileName);
- }
- }
- File folder = new File(uploadPath);
- if (!folder.isDirectory()) {
- folder.mkdirs();
- }
- URI u = URI.create(url);
- InputStream inputStream = u.toURL().openStream();
- File file = new File(uploadPath + licensePlate + ".zip");
- copyInputStreamToFile(inputStream, file);
- CompressUtil.unZip(file.getPath(), uploadPath, password, fileName, licensePlate,isBz);
- file.delete();
- return showPath;
- } catch (MalformedURLException e) {
- throw new RuntimeException(e);
- } catch (IOException e) {
- throw new RuntimeException(e);
- }
- }
- }
|