修复 下载方法 包含空格 和+转换错误的问题 调整下载方法输出报错方式

This commit is contained in:
gaoxiongzaq 2023-10-24 16:51:26 +08:00
parent 724db1936b
commit 7597864337
2 changed files with 5 additions and 9 deletions

View File

@ -207,7 +207,7 @@ public class FileHandlerService implements InitializingBean {
String pdfFolder = pdfName.substring(0, pdfName.length() - 4); String pdfFolder = pdfName.substring(0, pdfName.length() - 4);
String urlPrefix; String urlPrefix;
try { try {
urlPrefix = baseUrl + URLEncoder.encode(pdfFolder, uriEncoding).replaceAll("\\+", "%20"); urlPrefix = baseUrl + URLEncoder.encode(pdfFolder, uriEncoding).replaceAll("\\+", "%2B");
} catch (UnsupportedEncodingException e) { } catch (UnsupportedEncodingException e) {
logger.error("UnsupportedEncodingException", e); logger.error("UnsupportedEncodingException", e);
urlPrefix = baseUrl + pdfFolder; urlPrefix = baseUrl + pdfFolder;
@ -464,7 +464,7 @@ public class FileHandlerService implements InitializingBean {
url = WebUtils.encodeUrlFileName(url); url = WebUtils.encodeUrlFileName(url);
if(UrlEncoderUtils.hasUrlEncoded(fileName) && UrlEncoderUtils.hasUrlEncoded(suffix)){ //判断文件名是否转义 if(UrlEncoderUtils.hasUrlEncoded(fileName) && UrlEncoderUtils.hasUrlEncoded(suffix)){ //判断文件名是否转义
try { try {
fileName = URLDecoder.decode(fileName, "UTF-8").replaceAll("\\+", "%20"); fileName = URLDecoder.decode(fileName, "UTF-8").replaceAll("\\+", "%2B").replaceAll(" ", "%20");
suffix = URLDecoder.decode(suffix, "UTF-8"); suffix = URLDecoder.decode(suffix, "UTF-8");
} catch (UnsupportedEncodingException e) { } catch (UnsupportedEncodingException e) {
e.printStackTrace(); e.printStackTrace();

View File

@ -22,6 +22,7 @@ import java.io.FileNotFoundException;
import java.io.IOException; import java.io.IOException;
import java.net.URI; import java.net.URI;
import java.net.URL; import java.net.URL;
import java.net.URLEncoder;
import java.util.Arrays; import java.util.Arrays;
import java.util.Map; import java.util.Map;
import java.util.UUID; import java.util.UUID;
@ -54,7 +55,7 @@ public class DownloadUtils {
String urlStr = null; String urlStr = null;
try { try {
SslUtils.ignoreSsl(); SslUtils.ignoreSsl();
urlStr = fileAttribute.getUrl().replaceAll("\\+", "%20"); urlStr = fileAttribute.getUrl().replaceAll("\\+", "%2B").replaceAll(" ", "%20");
} catch (Exception e) { } catch (Exception e) {
logger.error("忽略SSL证书异常:", e); logger.error("忽略SSL证书异常:", e);
} }
@ -110,16 +111,11 @@ public class DownloadUtils {
FileUtils.copyToFile(fileResponse.getBody(), realFile); FileUtils.copyToFile(fileResponse.getBody(), realFile);
return null; return null;
}); });
} catch (RestClientException e) { } catch (Exception e) {
if (e.getMessage().contains("404 Not Found") || e.getMessage().contains("403 Not Found") || e.getMessage().contains("500 Not Found") ){
response.setCode(1); response.setCode(1);
response.setContent(null); response.setContent(null);
response.setMsg("下载失败:" + e); response.setMsg("下载失败:" + e);
return response; return response;
}else {
e.printStackTrace();
}
} }
} else if (isFtpUrl(url)) { } else if (isFtpUrl(url)) {
String ftpUsername = WebUtils.getUrlParameterReg(fileAttribute.getUrl(), URL_PARAM_FTP_USERNAME); String ftpUsername = WebUtils.getUrlParameterReg(fileAttribute.getUrl(), URL_PARAM_FTP_USERNAME);