统一 sftp、ftp、webdav 下载文件 contentType 为 application/octet-stream, 避免浏览器自动进行默认预览动作

This commit is contained in:
zhaojun
2022-08-01 15:20:06 +08:00
parent 27db6ed14a
commit c8bd52e26a
3 changed files with 20 additions and 8 deletions

View File

@@ -4,7 +4,6 @@ import cn.hutool.core.io.FileUtil;
import cn.hutool.core.io.IoUtil;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.MediaTypeFactory;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
@@ -14,7 +13,6 @@ import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Objects;
import java.util.Optional;
import java.util.function.Function;
/**
@@ -58,11 +56,8 @@ public class RequestHolder {
HttpServletResponse response = RequestHolder.getResponse();
String fileName = FileUtil.getName(path);
Optional<MediaType> mediaTypeOptional = MediaTypeFactory.getMediaType(path);
MediaType mediaType = mediaTypeOptional.orElse(MediaType.APPLICATION_OCTET_STREAM);
response.addHeader(HttpHeaders.CONTENT_DISPOSITION, "attachment;filename=" + StringUtils.encodeAllIgnoreSlashes(fileName));
response.setContentType(mediaType.getType());
response.setContentType(MediaType.APPLICATION_OCTET_STREAM.getType());
OutputStream outputStream = response.getOutputStream();

View File

@@ -22,6 +22,8 @@ import org.apache.commons.net.ftp.FTPFile;
import org.springframework.beans.factory.config.ConfigurableBeanFactory;
import org.springframework.context.annotation.Scope;
import org.springframework.core.io.Resource;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
@@ -161,9 +163,15 @@ public class FtpServiceImpl extends ProxyTransferService<FtpParam> {
HttpServletResponse response = RequestHolder.getResponse();
try {
pathAndName = StringUtils.concat(param.getBasePath(), pathAndName);
OutputStream outputStream = response.getOutputStream();
String fileName = FileUtil.getName(pathAndName);
String folderName = FileUtil.getParent(pathAndName, 1);
OutputStream outputStream = response.getOutputStream();
response.setContentType(MediaType.APPLICATION_OCTET_STREAM.getType());
response.addHeader(HttpHeaders.CONTENT_DISPOSITION, "attachment;filename=" + StringUtils.encodeAllIgnoreSlashes(fileName));
ftp.download(folderName, fileName, outputStream);
} catch (Exception e) {
throw new DownloadFileException(storageId, "下载文件失败", e);

View File

@@ -21,6 +21,8 @@ import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.config.ConfigurableBeanFactory;
import org.springframework.context.annotation.Scope;
import org.springframework.core.io.Resource;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
@@ -137,8 +139,15 @@ public class SftpServiceImpl extends ProxyTransferService<SftpParam> {
HttpServletResponse response = RequestHolder.getResponse();
try {
pathAndName = StringUtils.concat(param.getBasePath(), pathAndName);
String fileName = FileUtil.getName(pathAndName);
OutputStream outputStream = response.getOutputStream();
sftp.download(StringUtils.concat(param.getBasePath(), pathAndName), outputStream);
response.setContentType(MediaType.APPLICATION_OCTET_STREAM.getType());
response.addHeader(HttpHeaders.CONTENT_DISPOSITION, "attachment;filename=" + StringUtils.encodeAllIgnoreSlashes(fileName));
sftp.download(pathAndName, outputStream);
return null;
} catch (IOException e) {
throw new RuntimeException("下载文件失败", e);