文件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

@@ -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());