修复本地服务上传中文名下载不了的问题
This commit is contained in:
parent
a10cd6db85
commit
28103703fe
|
|
@ -39,14 +39,20 @@ public class FileDownloadUtils {
|
||||||
}
|
}
|
||||||
|
|
||||||
private static boolean downloadHttpFile(HttpServletResponse response, String urlStr) throws IOException {
|
private static boolean downloadHttpFile(HttpServletResponse response, String urlStr) throws IOException {
|
||||||
URL url = new URL(urlStr);
|
//处理中文文件名的问题
|
||||||
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
|
String fileName = urlStr.substring(urlStr.lastIndexOf('/') + 1);
|
||||||
|
String encodedFileName = URLEncoder.encode(fileName, "UTF-8");
|
||||||
|
String newPath = urlStr.substring(0, urlStr.lastIndexOf('/') + 1) + encodedFileName;
|
||||||
|
|
||||||
|
URL urlNew = new URL(newPath);
|
||||||
|
HttpURLConnection connection = (HttpURLConnection) urlNew.openConnection();
|
||||||
connection.setRequestMethod("GET");
|
connection.setRequestMethod("GET");
|
||||||
|
|
||||||
com.bonus.file.utils.FileUtils.setResponseHeaderByUrl(response, urlStr);
|
com.bonus.file.utils.FileUtils.setResponseHeaderByUrl(response, urlStr);
|
||||||
|
try {
|
||||||
try (InputStream inputStream = connection.getInputStream();
|
int code = connection.getResponseCode();
|
||||||
ServletOutputStream outputStream = response.getOutputStream()) {
|
InputStream inputStream = connection.getInputStream();
|
||||||
|
ServletOutputStream outputStream = response.getOutputStream();
|
||||||
|
|
||||||
byte[] buffer = new byte[4096];
|
byte[] buffer = new byte[4096];
|
||||||
int bytesRead = -1;
|
int bytesRead = -1;
|
||||||
|
|
@ -74,7 +80,13 @@ public class FileDownloadUtils {
|
||||||
String[] credentials = userInfo != null ? userInfo.split(":") : new String[]{"anonymous", ""};
|
String[] credentials = userInfo != null ? userInfo.split(":") : new String[]{"anonymous", ""};
|
||||||
String username = credentials[0];
|
String username = credentials[0];
|
||||||
String password = credentials.length > 1 ? credentials[1] : "";
|
String password = credentials.length > 1 ? credentials[1] : "";
|
||||||
String remoteFilePath = url.getPath();
|
|
||||||
|
//处理中文文件名的问题
|
||||||
|
String urlPath = url.getPath();
|
||||||
|
String path = urlPath.substring(urlStr.lastIndexOf('/') + 1);
|
||||||
|
String encodedFileName = URLEncoder.encode(path, "UTF-8");
|
||||||
|
|
||||||
|
String remoteFilePath = urlPath.substring(0, urlStr.lastIndexOf('/') + 1) + encodedFileName;;
|
||||||
|
|
||||||
ftpClient.connect(host, port);
|
ftpClient.connect(host, port);
|
||||||
ftpClient.login(username, password);
|
ftpClient.login(username, password);
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue