|
@@ -20,9 +20,11 @@ import org.springframework.web.multipart.MultipartFile;
|
|
|
import java.io.IOException;
|
|
import java.io.IOException;
|
|
|
import java.io.InputStream;
|
|
import java.io.InputStream;
|
|
|
import java.net.MalformedURLException;
|
|
import java.net.MalformedURLException;
|
|
|
|
|
+import java.net.URI;
|
|
|
import java.net.URL;
|
|
import java.net.URL;
|
|
|
import java.net.URLConnection;
|
|
import java.net.URLConnection;
|
|
|
-import java.net.URLDecoder;
|
|
|
|
|
|
|
+import java.net.URLEncoder;
|
|
|
|
|
+import java.nio.charset.StandardCharsets;
|
|
|
import java.time.LocalDateTime;
|
|
import java.time.LocalDateTime;
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
@@ -75,7 +77,9 @@ public class FileUploadController extends BaseController {
|
|
|
public HttpResult<SysUploadFiles> uploadUrlFile(String urlString, String fileName) {
|
|
public HttpResult<SysUploadFiles> uploadUrlFile(String urlString, String fileName) {
|
|
|
SysUploadFiles sysUploadFiles = new SysUploadFiles();
|
|
SysUploadFiles sysUploadFiles = new SysUploadFiles();
|
|
|
try {
|
|
try {
|
|
|
- URL url = new URL(URLDecoder.decode(urlString));
|
|
|
|
|
|
|
+ // Spring MVC 会自动 URL 解码,需重新编码含中文的 URL
|
|
|
|
|
+ String safeUrl = encodeUrlQuery(urlString);
|
|
|
|
|
+ URL url = new URL(safeUrl);
|
|
|
URLConnection connection = url.openConnection();
|
|
URLConnection connection = url.openConnection();
|
|
|
// 获取文件大小
|
|
// 获取文件大小
|
|
|
long contentLength = connection.getContentLengthLong();
|
|
long contentLength = connection.getContentLengthLong();
|
|
@@ -101,6 +105,45 @@ public class FileUploadController extends BaseController {
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 对 URL 中的 query 参数进行编码(Spring @RequestParam 会自动解码,需在此重新编码中文等非ASCII字符)
|
|
|
|
|
+ */
|
|
|
|
|
+ private String encodeUrlQuery(String urlStr) {
|
|
|
|
|
+ try {
|
|
|
|
|
+ URL url = new URL(urlStr);
|
|
|
|
|
+ String query = url.getQuery();
|
|
|
|
|
+ if (query == null || query.isEmpty()) {
|
|
|
|
|
+ return urlStr;
|
|
|
|
|
+ }
|
|
|
|
|
+ // 逐个参数编码
|
|
|
|
|
+ StringBuilder encodedQuery = new StringBuilder();
|
|
|
|
|
+ String[] params = query.split("&");
|
|
|
|
|
+ for (int i = 0; i < params.length; i++) {
|
|
|
|
|
+ if (i > 0) {
|
|
|
|
|
+ encodedQuery.append("&");
|
|
|
|
|
+ }
|
|
|
|
|
+ int eqIdx = params[i].indexOf('=');
|
|
|
|
|
+ if (eqIdx > 0) {
|
|
|
|
|
+ String key = params[i].substring(0, eqIdx);
|
|
|
|
|
+ String value = params[i].substring(eqIdx + 1);
|
|
|
|
|
+ encodedQuery.append(URLEncoder.encode(key, StandardCharsets.UTF_8))
|
|
|
|
|
+ .append("=")
|
|
|
|
|
+ .append(URLEncoder.encode(value, StandardCharsets.UTF_8));
|
|
|
|
|
+ } else {
|
|
|
|
|
+ encodedQuery.append(params[i]);
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ // 用 URI 重建 URL,确保路径和 query 都正确编码
|
|
|
|
|
+ int port = url.getPort();
|
|
|
|
|
+ String authority = port == -1 ? url.getHost() : url.getHost() + ":" + port;
|
|
|
|
|
+ URI uri = new URI(url.getProtocol(), authority, url.getPath(),
|
|
|
|
|
+ encodedQuery.toString(), url.getRef());
|
|
|
|
|
+ return uri.toASCIIString();
|
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
|
+ return urlStr; // 兜底返回原值
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
private String getFileExtension(String fileName) {
|
|
private String getFileExtension(String fileName) {
|
|
|
return fileName.substring(fileName.lastIndexOf("."));
|
|
return fileName.substring(fileName.lastIndexOf("."));
|
|
|
}
|
|
}
|