|
@@ -15,9 +15,11 @@ import lombok.SneakyThrows;
|
|
|
import java.io.ByteArrayInputStream;
|
|
|
import java.io.File;
|
|
|
import java.io.IOException;
|
|
|
+import java.io.UnsupportedEncodingException;
|
|
|
import java.net.HttpURLConnection;
|
|
|
import java.net.MalformedURLException;
|
|
|
import java.net.URL;
|
|
|
+import java.net.URLEncoder;
|
|
|
import java.util.Date;
|
|
|
|
|
|
/**
|
|
@@ -135,4 +137,16 @@ public class FileUtils {
|
|
|
}
|
|
|
return fullPath.substring(lastSlashIndex + 1);
|
|
|
}
|
|
|
+
|
|
|
+ public static String encodePathSegment(String pathSegment) {
|
|
|
+ try {
|
|
|
+ // 使用UTF-8编码,符合现代Web标准
|
|
|
+ String encoded = URLEncoder.encode(pathSegment, "UTF-8");
|
|
|
+ // 替换空格编码:URLEncoder会将空格转为+,而URL路径中推荐使用%20
|
|
|
+ return encoded.replace("+", "%20");
|
|
|
+ } catch (UnsupportedEncodingException e) {
|
|
|
+ // UTF-8是Java标准编码,不会抛出此异常
|
|
|
+ throw new RuntimeException("URL编码失败", e);
|
|
|
+ }
|
|
|
+ }
|
|
|
}
|