文件url采用base64加encodeURI双重编码,彻底解决各种奇葩文件名导致的下载异常
This commit is contained in:
@ -52,8 +52,8 @@ public class FileController {
|
||||
return new ObjectMapper().writeValueAsString(new ReturnResponse<String>(1, "存在同名文件,请先删除原有文件再次上传", null));
|
||||
}
|
||||
File outFile = new File(fileDir + demoPath);
|
||||
if (!outFile.exists()) {
|
||||
outFile.mkdirs();
|
||||
if (!outFile.exists() && !outFile.mkdirs()) {
|
||||
logger.error("创建文件夹【{}】失败,请检查目录权限!",fileDir + demoPath);
|
||||
}
|
||||
logger.info("上传文件:{}", fileDir + demoPath + fileName);
|
||||
try(InputStream in = file.getInputStream(); OutputStream out = new FileOutputStream(fileDir + demoPath + fileName)) {
|
||||
@ -72,8 +72,8 @@ public class FileController {
|
||||
}
|
||||
File file = new File(fileDir + demoPath + fileName);
|
||||
logger.info("删除文件:{}", file.getAbsolutePath());
|
||||
if (file.exists()) {
|
||||
file.delete();
|
||||
if (file.exists() && !file.delete()) {
|
||||
logger.error("删除文件【{}】失败,请检查目录权限!",file.getPath());
|
||||
}
|
||||
return new ObjectMapper().writeValueAsString(new ReturnResponse<String>(0, "SUCCESS", null));
|
||||
}
|
||||
@ -84,7 +84,7 @@ public class FileController {
|
||||
File file = new File(fileDir + demoPath);
|
||||
if (file.exists()) {
|
||||
Arrays.stream(Objects.requireNonNull(file.listFiles())).forEach(file1 -> {
|
||||
Map<String, String> fileName = new HashMap();
|
||||
Map<String, String> fileName = new HashMap<>();
|
||||
fileName.put("fileName", demoDir + "/" + file1.getName());
|
||||
list.add(fileName);
|
||||
});
|
||||
|
||||
@ -7,10 +7,12 @@ import cn.keking.service.FilePreviewFactory;
|
||||
import cn.keking.service.cache.CacheService;
|
||||
import cn.keking.utils.DownloadUtils;
|
||||
import cn.keking.service.FileHandlerService;
|
||||
import com.thoughtworks.xstream.core.util.Base64JavaUtilCodec;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.stereotype.Controller;
|
||||
import org.springframework.ui.Model;
|
||||
import org.springframework.util.Base64Utils;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMethod;
|
||||
import org.springframework.web.bind.annotation.ResponseBody;
|
||||
@ -33,22 +35,20 @@ public class OnlinePreviewController {
|
||||
private final FilePreviewFactory previewFactory;
|
||||
private final CacheService cacheService;
|
||||
private final FileHandlerService fileHandlerService;
|
||||
private final DownloadUtils downloadUtils;
|
||||
|
||||
public OnlinePreviewController(FilePreviewFactory filePreviewFactory, FileHandlerService fileHandlerService, CacheService cacheService, DownloadUtils downloadUtils) {
|
||||
public OnlinePreviewController(FilePreviewFactory filePreviewFactory, FileHandlerService fileHandlerService, CacheService cacheService) {
|
||||
this.previewFactory = filePreviewFactory;
|
||||
this.fileHandlerService = fileHandlerService;
|
||||
this.cacheService = cacheService;
|
||||
this.downloadUtils = downloadUtils;
|
||||
}
|
||||
|
||||
|
||||
@RequestMapping(value = "/onlinePreview")
|
||||
public String onlinePreview(String url, Model model, HttpServletRequest req) {
|
||||
FileAttribute fileAttribute = fileHandlerService.getFileAttribute(url,req);
|
||||
String fileUrl = new String(Base64Utils.decodeFromString(url));
|
||||
FileAttribute fileAttribute = fileHandlerService.getFileAttribute(fileUrl,req);
|
||||
FilePreview filePreview = previewFactory.get(fileAttribute);
|
||||
logger.info("预览文件url:{},previewType:{}", url, fileAttribute.getType());
|
||||
return filePreview.filePreviewHandle(url, model, fileAttribute);
|
||||
logger.info("预览文件url:{},previewType:{}", fileUrl, fileAttribute.getType());
|
||||
return filePreview.filePreviewHandle(fileUrl, model, fileAttribute);
|
||||
}
|
||||
|
||||
@RequestMapping(value = "/picturesPreview")
|
||||
@ -78,8 +78,8 @@ public class OnlinePreviewController {
|
||||
public void getCorsFile(String urlPath, HttpServletResponse response) {
|
||||
logger.info("下载跨域pdf文件url:{}", urlPath);
|
||||
try {
|
||||
byte[] bytes = downloadUtils.getBytesFromUrl(urlPath);
|
||||
downloadUtils.saveBytesToOutStream(bytes, response.getOutputStream());
|
||||
byte[] bytes = DownloadUtils.getBytesFromUrl(urlPath);
|
||||
DownloadUtils.saveBytesToOutStream(bytes, response.getOutputStream());
|
||||
} catch (IOException e) {
|
||||
logger.error("下载跨域pdf文件异常,url:{}", urlPath, e);
|
||||
}
|
||||
|
||||
@ -38,7 +38,7 @@ public class BaseUrlFilter implements Filter {
|
||||
pathBuilder.append(request.getScheme()).append("://").append(request.getServerName()).append(":")
|
||||
.append(request.getServerPort()).append(((HttpServletRequest) request).getContextPath()).append("/");
|
||||
String baseUrlTmp = ConfigConstants.getBaseUrl();
|
||||
if (baseUrlTmp != null && !ConfigConstants.DEFAULT_BASE_URL.equals(baseUrlTmp.toLowerCase())) {
|
||||
if (baseUrlTmp != null && !ConfigConstants.DEFAULT_BASE_URL.equalsIgnoreCase(baseUrlTmp)) {
|
||||
if (!baseUrlTmp.endsWith("/")) {
|
||||
baseUrlTmp = baseUrlTmp.concat("/");
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user