文件url采用base64加encodeURI双重编码,彻底解决各种奇葩文件名导致的下载异常

This commit is contained in:
chenkailing
2020-12-27 01:43:50 +08:00
committed by kl
parent aaf396fbc8
commit 486c09b24a
20 changed files with 113 additions and 1117 deletions

View File

@@ -4,13 +4,13 @@ import cn.keking.config.ConfigConstants;
import cn.keking.model.FileAttribute;
import cn.keking.model.FileType;
import cn.keking.service.cache.CacheService;
import cn.keking.utils.WebUtils;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;
import javax.servlet.http.HttpServletRequest;
import java.io.*;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@@ -189,55 +189,6 @@ public class FileHandlerService {
return fileName.substring(fileName.lastIndexOf(".") + 1);
}
/**
* 获取url中的参数
*
* @param url url
* @param name 参数名
* @return 参数值
*/
public String getUrlParameterReg(String url, String name) {
Map<String, String> mapRequest = new HashMap<>();
String strUrlParam = truncateUrlPage(url);
if (strUrlParam == null) {
return "";
}
//每个键值为一组
String[] arrSplit = strUrlParam.split("[&]");
for (String strSplit : arrSplit) {
String[] arrSplitEqual = strSplit.split("[=]");
//解析出键值
if (arrSplitEqual.length > 1) {
//正确解析
mapRequest.put(arrSplitEqual[0], arrSplitEqual[1]);
} else if (!arrSplitEqual[0].equals("")) {
//只有参数没有值,不加入
mapRequest.put(arrSplitEqual[0], "");
}
}
return mapRequest.get(name);
}
/**
* 去掉url中的路径留下请求参数部分
*
* @param strURL url地址
* @return url请求参数部分
*/
private String truncateUrlPage(String strURL) {
String strAllParam = null;
strURL = strURL.trim();
String[] arrSplit = strURL.split("[?]");
if (strURL.length() > 1) {
if (arrSplit.length > 1) {
if (arrSplit[1] != null) {
strAllParam = arrSplit[1];
}
}
}
return strAllParam;
}
/**
* 获取文件属性
*
@@ -249,7 +200,7 @@ public class FileHandlerService {
String suffix;
FileType type;
String fileName;
String fullFileName = this.getUrlParameterReg(url, "fullfilename");
String fullFileName = WebUtils.getUrlParameterReg(url, "fullfilename");
if (StringUtils.hasText(fullFileName)) {
fileName = fullFileName;
type = this.typeFromFileName(fullFileName);

View File

@@ -23,19 +23,11 @@ import static cn.keking.service.impl.OfficeFilePreviewImpl.getPreviewType;
public class CadFilePreviewImpl implements FilePreview {
private final FileHandlerService fileHandlerService;
private final DownloadUtils downloadUtils;
private final CadUtils cadUtils;
private final PdfUtils pdfUtils;
public CadFilePreviewImpl(FileHandlerService fileHandlerService,
DownloadUtils downloadUtils,
CadUtils cadUtils,
PdfUtils pdfUtils) {
public CadFilePreviewImpl(FileHandlerService fileHandlerService, CadUtils cadUtils, PdfUtils pdfUtils) {
this.fileHandlerService = fileHandlerService;
this.downloadUtils = downloadUtils;
this.cadUtils = cadUtils;
this.pdfUtils = pdfUtils;
@@ -58,7 +50,7 @@ public class CadFilePreviewImpl implements FilePreview {
// 判断之前是否已转换过,如果转换过,直接返回,否则执行转换
if (!fileHandlerService.listConvertedFiles().containsKey(pdfName) || !ConfigConstants.isCacheEnabled()) {
String filePath;
ReturnResponse<String> response = downloadUtils.downLoad(fileAttribute, null);
ReturnResponse<String> response = DownloadUtils.downLoad(fileAttribute, null);
if (0 != response.getCode()) {
model.addAttribute("fileType", suffix);
model.addAttribute("msg", response.getMsg());

View File

@@ -19,12 +19,10 @@ import org.springframework.util.StringUtils;
public class CompressFilePreviewImpl implements FilePreview {
private final FileHandlerService fileHandlerService;
private final DownloadUtils downloadUtils;
private final CompressFileReader compressFileReader;
public CompressFilePreviewImpl(FileHandlerService fileHandlerService, DownloadUtils downloadUtils, CompressFileReader compressFileReader) {
public CompressFilePreviewImpl(FileHandlerService fileHandlerService, CompressFileReader compressFileReader) {
this.fileHandlerService = fileHandlerService;
this.downloadUtils = downloadUtils;
this.compressFileReader = compressFileReader;
}
@@ -35,7 +33,7 @@ public class CompressFilePreviewImpl implements FilePreview {
String fileTree = null;
// 判断文件名是否存在(redis缓存读取)
if (!StringUtils.hasText(fileHandlerService.getConvertedFile(fileName)) || !ConfigConstants.isCacheEnabled()) {
ReturnResponse<String> response = downloadUtils.downLoad(fileAttribute, fileName);
ReturnResponse<String> response = DownloadUtils.downLoad(fileAttribute, fileName);
if (0 != response.getCode()) {
model.addAttribute("fileType", suffix);
model.addAttribute("msg", response.getMsg());

View File

@@ -17,13 +17,9 @@ import org.springframework.ui.Model;
@Service
public class MediaFilePreviewImpl implements FilePreview {
private final DownloadUtils downloadUtils;
private final FileHandlerService fileHandlerService;
public MediaFilePreviewImpl(DownloadUtils downloadUtils,
FileHandlerService fileHandlerService) {
this.downloadUtils = downloadUtils;
public MediaFilePreviewImpl(FileHandlerService fileHandlerService) {
this.fileHandlerService = fileHandlerService;
}
@@ -31,7 +27,7 @@ public class MediaFilePreviewImpl implements FilePreview {
public String filePreviewHandle(String url, Model model, FileAttribute fileAttribute) {
// 不是http开头浏览器不能直接访问需下载到本地
if (url != null && !url.toLowerCase().startsWith("http")) {
ReturnResponse<String> response = downloadUtils.downLoad(fileAttribute, fileAttribute.getName());
ReturnResponse<String> response = DownloadUtils.downLoad(fileAttribute, fileAttribute.getName());
if (0 != response.getCode()) {
model.addAttribute("fileType", fileAttribute.getSuffix());
model.addAttribute("msg", response.getMsg());

View File

@@ -24,13 +24,11 @@ public class OfficeFilePreviewImpl implements FilePreview {
private final FileHandlerService fileHandlerService;
private final PdfUtils pdfUtils;
private final DownloadUtils downloadUtils;
private final OfficeToPdfService officeToPdfService;
public OfficeFilePreviewImpl(FileHandlerService fileHandlerService, PdfUtils pdfUtils, DownloadUtils downloadUtils, OfficeToPdfService officeToPdfService) {
public OfficeFilePreviewImpl(FileHandlerService fileHandlerService, PdfUtils pdfUtils, OfficeToPdfService officeToPdfService) {
this.fileHandlerService = fileHandlerService;
this.pdfUtils = pdfUtils;
this.downloadUtils = downloadUtils;
this.officeToPdfService = officeToPdfService;
}
@@ -51,7 +49,7 @@ public class OfficeFilePreviewImpl implements FilePreview {
// 判断之前是否已转换过,如果转换过,直接返回,否则执行转换
if (!fileHandlerService.listConvertedFiles().containsKey(pdfName) || !ConfigConstants.isCacheEnabled()) {
String filePath;
ReturnResponse<String> response = downloadUtils.downLoad(fileAttribute, null);
ReturnResponse<String> response = DownloadUtils.downLoad(fileAttribute, null);
if (0 != response.getCode()) {
model.addAttribute("fileType", suffix);
model.addAttribute("msg", response.getMsg());

View File

@@ -21,19 +21,12 @@ import java.util.List;
public class PdfFilePreviewImpl implements FilePreview {
private final FileHandlerService fileHandlerService;
private final PdfUtils pdfUtils;
private final DownloadUtils downloadUtils;
private static final String FILE_DIR = ConfigConstants.getFileDir();
public PdfFilePreviewImpl(FileHandlerService fileHandlerService,
PdfUtils pdfUtils,
DownloadUtils downloadUtils) {
public PdfFilePreviewImpl(FileHandlerService fileHandlerService, PdfUtils pdfUtils) {
this.fileHandlerService = fileHandlerService;
this.pdfUtils = pdfUtils;
this.downloadUtils = downloadUtils;
}
@Override
@@ -47,7 +40,7 @@ public class PdfFilePreviewImpl implements FilePreview {
if (OfficeFilePreviewImpl.OFFICE_PREVIEW_TYPE_IMAGE.equals(officePreviewType) || OfficeFilePreviewImpl.OFFICE_PREVIEW_TYPE_ALL_IMAGES.equals(officePreviewType)) {
//当文件不存在时,就去下载
if (!fileHandlerService.listConvertedFiles().containsKey(pdfName) || !ConfigConstants.isCacheEnabled()) {
ReturnResponse<String> response = downloadUtils.downLoad(fileAttribute, fileName);
ReturnResponse<String> response = DownloadUtils.downLoad(fileAttribute, fileName);
if (0 != response.getCode()) {
model.addAttribute("fileType", suffix);
model.addAttribute("msg", response.getMsg());
@@ -76,7 +69,7 @@ public class PdfFilePreviewImpl implements FilePreview {
// 不是http开头浏览器不能直接访问需下载到本地
if (url != null && !url.toLowerCase().startsWith("http")) {
if (!fileHandlerService.listConvertedFiles().containsKey(pdfName) || !ConfigConstants.isCacheEnabled()) {
ReturnResponse<String> response = downloadUtils.downLoad(fileAttribute, pdfName);
ReturnResponse<String> response = DownloadUtils.downLoad(fileAttribute, pdfName);
if (0 != response.getCode()) {
model.addAttribute("fileType", suffix);
model.addAttribute("msg", response.getMsg());

View File

@@ -20,12 +20,8 @@ public class PictureFilePreviewImpl implements FilePreview {
private final FileHandlerService fileHandlerService;
private final DownloadUtils downloadUtils;
public PictureFilePreviewImpl(FileHandlerService fileHandlerService,
DownloadUtils downloadUtils) {
public PictureFilePreviewImpl(FileHandlerService fileHandlerService) {
this.fileHandlerService = fileHandlerService;
this.downloadUtils = downloadUtils;
}
@Override
@@ -39,7 +35,7 @@ public class PictureFilePreviewImpl implements FilePreview {
}
// 不是http开头浏览器不能直接访问需下载到本地
if (url != null && !url.toLowerCase().startsWith("http")) {
ReturnResponse<String> response = downloadUtils.downLoad(fileAttribute, null);
ReturnResponse<String> response = DownloadUtils.downLoad(fileAttribute, null);
if (0 != response.getCode()) {
model.addAttribute("fileType", fileAttribute.getSuffix());
model.addAttribute("msg", response.getMsg());

View File

@@ -23,16 +23,10 @@ public class SimTextFilePreviewImpl implements FilePreview {
public static final String TEXT_TYPE = "textType";
public static final String DEFAULT_TEXT_TYPE = "simText";
private final DownloadUtils downloadUtils;
public SimTextFilePreviewImpl(DownloadUtils downloadUtils) {
this.downloadUtils = downloadUtils;
}
@Override
public String filePreviewHandle(String url, Model model, FileAttribute fileAttribute) {
String fileName = fileAttribute.getName();
ReturnResponse<String> response = downloadUtils.downLoad(fileAttribute, fileName);
ReturnResponse<String> response = DownloadUtils.downLoad(fileAttribute, fileName);
if (0 != response.getCode()) {
model.addAttribute("msg", response.getMsg());
model.addAttribute("fileType", fileAttribute.getSuffix());