|
|
@@ -6,7 +6,9 @@ import com.ydtech.utils.StringUtils;
|
|
|
import java.io.*;
|
|
|
import java.net.MalformedURLException;
|
|
|
import java.net.URI;
|
|
|
-import java.util.Arrays;
|
|
|
+import java.net.URLEncoder;
|
|
|
+import java.util.regex.Matcher;
|
|
|
+import java.util.regex.Pattern;
|
|
|
|
|
|
/**
|
|
|
* 文件读取工具类
|
|
|
@@ -80,10 +82,10 @@ public class FileUtil {
|
|
|
/**
|
|
|
* 网络url转文件
|
|
|
*
|
|
|
- * @param url 保司返回的保单地址
|
|
|
- * @param dir 目录 杠结束 carInsPolicy/
|
|
|
- * @param path 文件真实目录 D:/upload/
|
|
|
- * @param showUrl 展示地址 http://sxzgkj.baoxianzhanggui.com/web-api/晋A12345/晋A12345-交强险保单.pdf
|
|
|
+ * @param url 保司返回的保单地址
|
|
|
+ * @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
|
|
|
*/
|
|
|
@@ -110,7 +112,8 @@ public class FileUtil {
|
|
|
folder.mkdirs();
|
|
|
}
|
|
|
|
|
|
- URI u = URI.create(url);
|
|
|
+ String encodeUrl = urlEncodeChinese(url);
|
|
|
+ URI u = URI.create(encodeUrl);
|
|
|
InputStream inputStream = u.toURL().openStream();
|
|
|
File file = new File(uploadPath + fileName);
|
|
|
copyInputStreamToFile(inputStream, file);
|
|
|
@@ -126,13 +129,13 @@ public class FileUtil {
|
|
|
|
|
|
}
|
|
|
|
|
|
+
|
|
|
/**
|
|
|
* 文件存在
|
|
|
*
|
|
|
- *
|
|
|
- * @param dir 目录 杠结束 carInsPolicy/
|
|
|
- * @param path 文件真实目录 D:/upload/
|
|
|
- * @param showUrl 展示地址 http://sxzgkj.baoxianzhanggui.com/web-api/晋A12345/晋A12345-交强险保单.pdf
|
|
|
+ * @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
|
|
|
*/
|
|
|
@@ -173,7 +176,9 @@ public class FileUtil {
|
|
|
}
|
|
|
|
|
|
}
|
|
|
- public static String netUrlStoreToLocal(String url, String dir, String fileName, String path, String showUrl,String password,String licensePlate) {
|
|
|
+
|
|
|
+ public static String netUrlStoreToLocal(String url, String dir, String fileName, String path, String showUrl, String password,
|
|
|
+ String licensePlate) {
|
|
|
if (StringUtils.isEmpty(url)) return "";
|
|
|
String showPath = String.format("%s%s%s", showUrl, dir, fileName);
|
|
|
String uploadPath = path + dir;
|
|
|
@@ -209,12 +214,26 @@ public class FileUtil {
|
|
|
}
|
|
|
|
|
|
}
|
|
|
- public static void main(String[] args) {
|
|
|
- String licensePlate = "晋ABCDEF";
|
|
|
- String url = "https://gears.pc.ehuatai.com:9040/P/PYr63mm";
|
|
|
- String dir = "carInsPolicy/" + licensePlate + "/";
|
|
|
- String fileName = licensePlate + ".zip";
|
|
|
- String path = "D:/upload/";
|
|
|
- String showUrl = "http://127.0.0.1:8080/upload/";
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 将 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;
|
|
|
}
|
|
|
+
|
|
|
+
|
|
|
}
|