|
@@ -50,8 +50,12 @@ import org.springframework.stereotype.Service;
|
|
|
import java.io.*;
|
|
|
import java.net.HttpURLConnection;
|
|
|
import java.net.URL;
|
|
|
+import java.net.URLEncoder;
|
|
|
+import java.nio.charset.StandardCharsets;
|
|
|
import java.nio.file.Files;
|
|
|
import java.util.*;
|
|
|
+import java.util.regex.Matcher;
|
|
|
+import java.util.regex.Pattern;
|
|
|
import java.util.stream.Collectors;
|
|
|
|
|
|
@Service
|
|
@@ -574,7 +578,11 @@ public class LingmingguangziServiceImpl implements LingmingguangziService {
|
|
|
e.printStackTrace();
|
|
|
}
|
|
|
//上传附件
|
|
|
- detail.put("files",postWithFile(getErpUrl(type) + "pws/sys/common/upload", filePathSz + fileName,type));
|
|
|
+ try {
|
|
|
+ detail.put("files",postWithFile(getErpUrl(type) + "pws/sys/common/upload", filePathSz + fileName,type));
|
|
|
+ } catch (UnsupportedEncodingException e) {
|
|
|
+ throw new RuntimeException(e);
|
|
|
+ }
|
|
|
}else {
|
|
|
//将downloadurl下载到本地
|
|
|
try {
|
|
@@ -584,7 +592,11 @@ public class LingmingguangziServiceImpl implements LingmingguangziService {
|
|
|
e.printStackTrace();
|
|
|
}
|
|
|
//上传附件
|
|
|
- detail.put("files",postWithFile(getErpUrl(type) + "pws/sys/common/upload", filePathXg + fileName,type));
|
|
|
+ try {
|
|
|
+ detail.put("files",postWithFile(getErpUrl(type) + "pws/sys/common/upload", filePathXg + fileName,type));
|
|
|
+ } catch (UnsupportedEncodingException e) {
|
|
|
+ throw new RuntimeException(e);
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
return detail;
|
|
@@ -901,7 +913,33 @@ public class LingmingguangziServiceImpl implements LingmingguangziService {
|
|
|
return result;
|
|
|
}
|
|
|
|
|
|
- public String postWithFile(String url, String filePath,String type) {
|
|
|
+ public static String convertChineseToUtf8Hex(String input) {
|
|
|
+ StringBuilder sb = new StringBuilder();
|
|
|
+ Pattern pattern = Pattern.compile("[\\u4E00-\\u9FA5]");
|
|
|
+ Matcher matcher = pattern.matcher(input);
|
|
|
+
|
|
|
+ int i = 0;
|
|
|
+ while (i < input.length()) {
|
|
|
+ if (matcher.region(i, i + 1).find()) {
|
|
|
+ // 当前字符是中文字符
|
|
|
+ char chineseChar = input.charAt(i);
|
|
|
+ byte[] utf8Bytes = String.valueOf(chineseChar).getBytes(StandardCharsets.UTF_8);
|
|
|
+ StringBuilder hexString = new StringBuilder();
|
|
|
+ for (byte b : utf8Bytes) {
|
|
|
+ hexString.append(String.format("%02x", b));
|
|
|
+ }
|
|
|
+ sb.append(hexString.toString());
|
|
|
+ } else {
|
|
|
+ // 当前字符不是中文字符
|
|
|
+ sb.append(input.charAt(i));
|
|
|
+ }
|
|
|
+ i++;
|
|
|
+ }
|
|
|
+
|
|
|
+ return sb.toString();
|
|
|
+ }
|
|
|
+
|
|
|
+ public String postWithFile(String url, String filePath,String type) throws UnsupportedEncodingException {
|
|
|
// 文件路径
|
|
|
File file = new File(filePath);
|
|
|
|
|
@@ -917,7 +955,11 @@ public class LingmingguangziServiceImpl implements LingmingguangziService {
|
|
|
|
|
|
// 使用MultipartEntityBuilder来构建multipart请求
|
|
|
MultipartEntityBuilder builder = MultipartEntityBuilder.create();
|
|
|
- builder.addBinaryBody("file", file, ContentType.DEFAULT_BINARY, file.getName());
|
|
|
+
|
|
|
+ //如果fileUrl中包含中文,则需要对其进行编码
|
|
|
+ String encodeFileName = convertChineseToUtf8Hex(file.getName());
|
|
|
+
|
|
|
+ builder.addBinaryBody("file", file, ContentType.DEFAULT_BINARY, encodeFileName);
|
|
|
|
|
|
// 设置请求体
|
|
|
HttpEntity multipart = builder.build();
|
|
@@ -939,7 +981,9 @@ public class LingmingguangziServiceImpl implements LingmingguangziService {
|
|
|
String responseString = EntityUtils.toString(entity, "UTF-8"); // 指定字符集
|
|
|
System.out.println("Response Body: " + responseString);
|
|
|
Map parse = (Map) JSONObject.parse(responseString);
|
|
|
- fileName = getString(parse.get("message"));
|
|
|
+ if ((boolean)parse.get("success")){
|
|
|
+ fileName = getString(parse.get("message"));
|
|
|
+ }
|
|
|
// 释放响应实体资源(虽然EntityUtils.toString()在某些HttpClient版本中会自动处理)
|
|
|
EntityUtils.consume(entity);
|
|
|
}
|