!277 修复 压缩包特殊符文无法访问问题 调整统一url输出为url编码解决特殊符号问题 调整跨域方法和下载方法为一样 支持重定向

Merge pull request !277 from 高雄/master
This commit is contained in:
陈精华 2024-03-20 02:24:47 +00:00 committed by Gitee
commit 55bda80d33
No known key found for this signature in database
GPG Key ID: 173E9B9CA92EEF8F
8 changed files with 82 additions and 91 deletions

View File

@ -35,6 +35,7 @@
<aspose-cad.version>23.9</aspose-cad.version> <aspose-cad.version>23.9</aspose-cad.version>
<bcprov-jdk15on.version>1.70</bcprov-jdk15on.version> <bcprov-jdk15on.version>1.70</bcprov-jdk15on.version>
<juniversalchardet.version>1.0.3</juniversalchardet.version> <juniversalchardet.version>1.0.3</juniversalchardet.version>
<httpcomponents.version>4.5.14</httpcomponents.version>
<commons-cli.version>1.5.0</commons-cli.version> <commons-cli.version>1.5.0</commons-cli.version>
<commons-net.version>3.9.0</commons-net.version> <commons-net.version>3.9.0</commons-net.version>

View File

@ -99,6 +99,11 @@
<version>${xdocreport.version}</version> <version>${xdocreport.version}</version>
</dependency> </dependency>
<!-- poi start --> <!-- poi start -->
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>${httpcomponents.version}</version>
</dependency>
<!-- rar5 的支持 和其他众多压缩支持 可参考 package net.sf.sevenzipjbinding.ArchiveFormat; --> <!-- rar5 的支持 和其他众多压缩支持 可参考 package net.sf.sevenzipjbinding.ArchiveFormat; -->
<dependency> <dependency>

View File

@ -421,7 +421,7 @@ public class FileHandlerService implements InitializingBean {
public static String getSubString(String str, String posStr) { public static String getSubString(String str, String posStr) {
return str.substring(str.indexOf(posStr) + posStr.length()); return str.substring(str.indexOf(posStr) + posStr.length());
} }
/** /**
* 获取文件属性 * 获取文件属性
* *

View File

@ -6,23 +6,22 @@ import cn.keking.model.ReturnResponse;
import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.ObjectMapper;
import io.mola.galimatias.GalimatiasParseException; import io.mola.galimatias.GalimatiasParseException;
import org.apache.commons.io.FileUtils; import org.apache.commons.io.FileUtils;
import org.apache.http.client.HttpClient;
import org.apache.http.impl.client.DefaultRedirectStrategy;
import org.apache.http.impl.client.HttpClientBuilder;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import org.springframework.http.HttpMethod; import org.springframework.http.HttpMethod;
import org.springframework.http.MediaType; import org.springframework.http.MediaType;
import org.springframework.http.client.SimpleClientHttpRequestFactory; import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils; import org.springframework.util.StringUtils;
import org.springframework.web.client.RequestCallback; import org.springframework.web.client.RequestCallback;
import org.springframework.web.client.RestClientException;
import org.springframework.web.client.RestTemplate; import org.springframework.web.client.RestTemplate;
import java.io.File; import java.io.File;
import java.io.FileNotFoundException; import java.io.FileNotFoundException;
import java.io.IOException; import java.io.IOException;
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;
@ -41,6 +40,7 @@ public class DownloadUtils {
private static final String URL_PARAM_FTP_PASSWORD = "ftp.password"; private static final String URL_PARAM_FTP_PASSWORD = "ftp.password";
private static final String URL_PARAM_FTP_CONTROL_ENCODING = "ftp.control.encoding"; private static final String URL_PARAM_FTP_CONTROL_ENCODING = "ftp.control.encoding";
private static final RestTemplate restTemplate = new RestTemplate(); private static final RestTemplate restTemplate = new RestTemplate();
private static final HttpComponentsClientHttpRequestFactory factory = new HttpComponentsClientHttpRequestFactory();
private static final ObjectMapper mapper = new ObjectMapper(); private static final ObjectMapper mapper = new ObjectMapper();
@ -90,12 +90,12 @@ public class DownloadUtils {
if (!fileAttribute.getSkipDownLoad()) { if (!fileAttribute.getSkipDownLoad()) {
if (isHttpUrl(url)) { if (isHttpUrl(url)) {
File realFile = new File(realPath); File realFile = new File(realPath);
SimpleClientHttpRequestFactory httpFactory = new SimpleClientHttpRequestFactory(); factory.setConnectionRequestTimeout(2000); //设置超时时间
//连接超时10秒默认无限制单位毫秒 factory.setConnectTimeout(10000);
httpFactory.setConnectTimeout(60 * 1000); factory.setReadTimeout(72000);
//读取超时5秒,默认无限限制,单位毫秒 HttpClient httpClient = HttpClientBuilder.create().setRedirectStrategy(new DefaultRedirectStrategy()).build();
httpFactory.setReadTimeout(60 * 1000); factory.setHttpClient(httpClient); //加入重定向方法
restTemplate.setRequestFactory(httpFactory); restTemplate.setRequestFactory(factory);
RequestCallback requestCallback = request -> { RequestCallback requestCallback = request -> {
request.getHeaders().setAccept(Arrays.asList(MediaType.APPLICATION_OCTET_STREAM, MediaType.ALL)); request.getHeaders().setAccept(Arrays.asList(MediaType.APPLICATION_OCTET_STREAM, MediaType.ALL));
String proxyAuthorization = fileAttribute.getKkProxyAuthorization(); String proxyAuthorization = fileAttribute.getKkProxyAuthorization();

View File

@ -75,15 +75,18 @@ public class RarUtils {
public static boolean judge(char c){ public static boolean judge(char c){
return (c >= '0' && c <= '9') || (c >= 'a' && c <= 'z' || c >= 'A' && c <= 'Z'); return (c >= '0' && c <= '9') || (c >= 'a' && c <= 'z' || c >= 'A' && c <= 'Z');
} }
public static String specialSymbols(String str) {
//去除压缩包文件字符串中特殊符号
Pattern p = Pattern.compile("\\s|\t|\r|\n|\\+|#|&|=|\\p{P}");
// Pattern p = Pattern.compile("\\s|\\+|#|&|=|\\p{P}");
Matcher m = p.matcher(str);
return m.replaceAll("");
}
public static boolean isMessyCode(String strName) { public static boolean isMessyCode(String strName) {
//去除字符串中的空格 制表符 换行 回车 //去除字符串中的空格 制表符 换行 回车
Pattern p = Pattern.compile("\\s*|\t*|\r*|\n*"); strName = specialSymbols(strName);
Matcher m = p.matcher(strName);
String after = m.replaceAll("").replaceAll("\\+", "").replaceAll("#", "").replaceAll("&", "");
//去除字符串中的标点符号
String temp = after.replaceAll("\\p{P}", "");
//处理之后转换成字符数组 //处理之后转换成字符数组
char[] ch = temp.trim().toCharArray(); char[] ch = strName.trim().toCharArray();
for (char c : ch) { for (char c : ch) {
//判断是否是数字或者英文字符 //判断是否是数字或者英文字符
if (!judge(c)) { if (!judge(c)) {

View File

@ -5,7 +5,6 @@ import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import org.springframework.util.Base64Utils; import org.springframework.util.Base64Utils;
import org.springframework.util.ObjectUtils;
import org.springframework.web.multipart.MultipartFile; import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.util.HtmlUtils; import org.springframework.web.util.HtmlUtils;
@ -74,15 +73,20 @@ public class WebUtils {
*/ */
public static String urlEncoderencode(String urlStr) { public static String urlEncoderencode(String urlStr) {
String fullFileName = getUrlParameterReg(urlStr, "fullfilename"); //获取文件名 String fullFileName = getUrlParameterReg(urlStr, "fullfilename"); //获取流文件名
if (!ObjectUtils.isEmpty(fullFileName)) { //判断是否启用了 流接入方法 if (org.springframework.util.StringUtils.hasText(fullFileName)) {
urlStr = clearFullfilenameParam(urlStr); //去掉流接入 拼接命令 // 移除fullfilename参数
urlStr = clearFullfilenameParam(urlStr);
} else {
fullFileName = getFileNameFromURL(urlStr); //获取文件名
} }
try { if (!UrlEncoderUtils.hasUrlEncoded(fullFileName)) { //判断文件名是否转义
urlStr = URLEncoder.encode(urlStr, "UTF-8").replaceAll("\\+", "%20"); try {
urlStr = urlStr.replaceAll("%3A", ":").replaceAll("%2F", "/").replaceAll("%3F", "?").replaceAll("%26", "&"); urlStr = URLEncoder.encode(urlStr, "UTF-8").replaceAll("\\+", "%20").replaceAll("%3A", ":").replaceAll("%2F", "/").replaceAll("%3F", "?").replaceAll("%26", "&");
} catch (UnsupportedEncodingException e) { } catch (UnsupportedEncodingException e) {
e.printStackTrace(); e.printStackTrace();
}
} }
return urlStr; return urlStr;
} }

View File

@ -8,28 +8,33 @@ import cn.keking.service.cache.CacheService;
import cn.keking.service.impl.OtherFilePreviewImpl; import cn.keking.service.impl.OtherFilePreviewImpl;
import cn.keking.utils.KkFileUtils; import cn.keking.utils.KkFileUtils;
import cn.keking.utils.WebUtils; import cn.keking.utils.WebUtils;
import com.fasterxml.jackson.databind.ObjectMapper;
import fr.opensagres.xdocreport.core.io.IOUtils; import fr.opensagres.xdocreport.core.io.IOUtils;
import io.mola.galimatias.GalimatiasParseException;
import org.apache.commons.codec.binary.Base64; import org.apache.commons.codec.binary.Base64;
import org.apache.http.client.HttpClient;
import org.apache.http.impl.client.DefaultRedirectStrategy;
import org.apache.http.impl.client.HttpClientBuilder;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import org.springframework.http.HttpMethod;
import org.springframework.http.MediaType;
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
import org.springframework.stereotype.Controller; import org.springframework.stereotype.Controller;
import org.springframework.ui.Model; import org.springframework.ui.Model;
import org.springframework.util.StringUtils; import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody; import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.client.RequestCallback;
import org.springframework.web.client.RestTemplate;
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpServletResponse;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.URL; import java.net.URL;
import java.net.URLDecoder;
import java.nio.charset.StandardCharsets;
import java.util.Arrays; import java.util.Arrays;
import java.util.List; import java.util.List;
import java.util.Map;
import static cn.keking.service.FilePreview.PICTURE_FILE_PREVIEW_PAGE; import static cn.keking.service.FilePreview.PICTURE_FILE_PREVIEW_PAGE;
@ -46,6 +51,9 @@ public class OnlinePreviewController {
private final CacheService cacheService; private final CacheService cacheService;
private final FileHandlerService fileHandlerService; private final FileHandlerService fileHandlerService;
private final OtherFilePreviewImpl otherFilePreview; private final OtherFilePreviewImpl otherFilePreview;
private static final RestTemplate restTemplate = new RestTemplate();
private static final HttpComponentsClientHttpRequestFactory factory = new HttpComponentsClientHttpRequestFactory();
private static final ObjectMapper mapper = new ObjectMapper();
public OnlinePreviewController(FilePreviewFactory filePreviewFactory, FileHandlerService fileHandlerService, CacheService cacheService, OtherFilePreviewImpl otherFilePreview) { public OnlinePreviewController(FilePreviewFactory filePreviewFactory, FileHandlerService fileHandlerService, CacheService cacheService, OtherFilePreviewImpl otherFilePreview) {
this.previewFactory = filePreviewFactory; this.previewFactory = filePreviewFactory;
@ -106,83 +114,53 @@ public class OnlinePreviewController {
* @param response response * @param response response
*/ */
@GetMapping("/getCorsFile") @GetMapping("/getCorsFile")
public void getCorsFile(String urlPath, HttpServletResponse response) throws IOException { public void getCorsFile(String urlPath, HttpServletResponse response,FileAttribute fileAttribute) throws IOException {
URL url;
try { try {
urlPath = WebUtils.decodeUrl(urlPath); urlPath = WebUtils.decodeUrl(urlPath);
url = WebUtils.normalizedURL(urlPath);
} catch (Exception ex) { } catch (Exception ex) {
logger.error(String.format(BASE64_DECODE_ERROR_MSG, urlPath),ex); logger.error(String.format(BASE64_DECODE_ERROR_MSG, urlPath),ex);
return; return;
} }
HttpURLConnection urlcon = null;
InputStream inputStream = null;
String urlStr;
assert urlPath != null; assert urlPath != null;
if (!urlPath.toLowerCase().startsWith("http") && !urlPath.toLowerCase().startsWith("https") && !urlPath.toLowerCase().startsWith("ftp")) { if (!urlPath.toLowerCase().startsWith("http") && !urlPath.toLowerCase().startsWith("https") && !urlPath.toLowerCase().startsWith("ftp")) {
logger.info("读取跨域文件异常可能存在非法访问urlPath{}", urlPath); logger.info("读取跨域文件异常可能存在非法访问urlPath{}", urlPath);
return; return;
} }
logger.info("下载跨域pdf文件url{}", urlPath); InputStream inputStream = null;
if (!urlPath.toLowerCase().startsWith("ftp:")){ logger.info("读取跨域pdf文件url{}", urlPath);
if (!urlPath.toLowerCase().startsWith("ftp:")) {
factory.setConnectionRequestTimeout(2000);
factory.setConnectTimeout(10000);
factory.setReadTimeout(72000);
HttpClient httpClient = HttpClientBuilder.create().setRedirectStrategy(new DefaultRedirectStrategy()).build();
factory.setHttpClient(httpClient);
restTemplate.setRequestFactory(factory);
RequestCallback requestCallback = request -> {
request.getHeaders().setAccept(Arrays.asList(MediaType.APPLICATION_OCTET_STREAM, MediaType.ALL));
String proxyAuthorization = fileAttribute.getKkProxyAuthorization();
if(StringUtils.hasText(proxyAuthorization)){
Map<String,String> proxyAuthorizationMap = mapper.readValue(proxyAuthorization, Map.class);
proxyAuthorizationMap.forEach((key, value) -> request.getHeaders().set(key, value));
}
};
try { try {
URL url = WebUtils.normalizedURL(urlPath); restTemplate.execute(url.toURI(), HttpMethod.GET, requestCallback, fileResponse -> {
urlcon=(HttpURLConnection)url.openConnection(); IOUtils.copy(fileResponse.getBody(), response.getOutputStream());
urlcon.setConnectTimeout(30000); return null;
urlcon.setReadTimeout(30000); });
urlcon.setInstanceFollowRedirects(false); } catch (Exception e) {
int responseCode = urlcon.getResponseCode(); System.out.println(e);
if ( responseCode == 403 || responseCode == 500) { //403 500
logger.error("读取跨域文件异常url{},错误:{}", urlPath,responseCode);
return ;
}
if (responseCode == HttpURLConnection.HTTP_MOVED_PERM || responseCode == HttpURLConnection.HTTP_MOVED_TEMP) { //301 302
url =new URL(urlcon.getHeaderField("Location"));
urlcon=(HttpURLConnection)url.openConnection();
} if (responseCode == 404 ) { //404
try {
urlStr = URLDecoder.decode(urlPath, StandardCharsets.UTF_8.name());
urlStr = URLDecoder.decode(urlStr, StandardCharsets.UTF_8.name());
url = WebUtils.normalizedURL(urlStr);
urlcon=(HttpURLConnection)url.openConnection();
urlcon.setConnectTimeout(30000);
urlcon.setReadTimeout(30000);
urlcon.setInstanceFollowRedirects(false);
responseCode = urlcon.getResponseCode();
if (responseCode == HttpURLConnection.HTTP_MOVED_PERM || responseCode == HttpURLConnection.HTTP_MOVED_TEMP) { //301 302
url =new URL(urlcon.getHeaderField("Location"));
}
if(responseCode == 404 ||responseCode == 403 || responseCode == 500 ){
logger.error("读取跨域文件异常url{},错误:{}", urlPath,responseCode);
return ;
}
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}finally {
assert urlcon != null;
urlcon.disconnect();
}
}
if(urlPath.contains( ".svg")) {
response.setContentType("image/svg+xml");
}
inputStream=(url).openStream();
IOUtils.copy(inputStream, response.getOutputStream());
} catch (IOException | GalimatiasParseException e) {
logger.error("读取跨域文件异常url{}", urlPath);
} finally {
assert urlcon != null;
urlcon.disconnect();
IOUtils.closeQuietly(inputStream);
} }
} else { }else{
try { try {
URL url = WebUtils.normalizedURL(urlPath);
if(urlPath.contains(".svg")) { if(urlPath.contains(".svg")) {
response.setContentType("image/svg+xml"); response.setContentType("image/svg+xml");
} }
inputStream = (url).openStream(); inputStream = (url).openStream();
IOUtils.copy(inputStream, response.getOutputStream()); IOUtils.copy(inputStream, response.getOutputStream());
} catch (IOException | GalimatiasParseException e) { } catch (IOException e) {
logger.error("读取跨域文件异常url{}", urlPath); logger.error("读取跨域文件异常url{}", urlPath);
} finally { } finally {
IOUtils.closeQuietly(inputStream); IOUtils.closeQuietly(inputStream);

View File

@ -51,7 +51,7 @@
}; };
function chooseNode(event, treeId, treeNode) { function chooseNode(event, treeId, treeNode) {
if (!treeNode.isParent) { if (!treeNode.isParent) {
var path = '${baseUrl}' + treeNode.id + "?kkCompressfileKey=" + '${fileTree}'+"&kkCompressfilepath=" + treeNode.id+"&fullfilename="+treeNode.name; var path = '${baseUrl}' + treeNode.id + "?kkCompressfileKey=" + encodeURIComponent('${fileTree}')+"&kkCompressfilepath=" + encodeURIComponent(treeNode.id)+"&fullfilename="+encodeURIComponent(treeNode.name);
location.href = "${baseUrl}onlinePreview?url=" + encodeURIComponent(Base64.encode(path)); location.href = "${baseUrl}onlinePreview?url=" + encodeURIComponent(Base64.encode(path));
} }
} }