From c8bd52e26a983701b11e8cc45c9125d4fd4019a7 Mon Sep 17 00:00:00 2001 From: zhaojun <873019219@qq.com> Date: Mon, 1 Aug 2022 15:20:06 +0800 Subject: [PATCH] =?UTF-8?q?:sparkles:=20=E7=BB=9F=E4=B8=80=20sftp=E3=80=81?= =?UTF-8?q?ftp=E3=80=81webdav=20=E4=B8=8B=E8=BD=BD=E6=96=87=E4=BB=B6=20con?= =?UTF-8?q?tentType=20=E4=B8=BA=20application/octet-stream,=20=E9=81=BF?= =?UTF-8?q?=E5=85=8D=E6=B5=8F=E8=A7=88=E5=99=A8=E8=87=AA=E5=8A=A8=E8=BF=9B?= =?UTF-8?q?=E8=A1=8C=E9=BB=98=E8=AE=A4=E9=A2=84=E8=A7=88=E5=8A=A8=E4=BD=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../im/zhaojun/zfile/common/util/RequestHolder.java | 7 +------ .../zfile/home/service/impl/FtpServiceImpl.java | 10 +++++++++- .../zfile/home/service/impl/SftpServiceImpl.java | 11 ++++++++++- 3 files changed, 20 insertions(+), 8 deletions(-) diff --git a/src/main/java/im/zhaojun/zfile/common/util/RequestHolder.java b/src/main/java/im/zhaojun/zfile/common/util/RequestHolder.java index 765fd72..70bd116 100644 --- a/src/main/java/im/zhaojun/zfile/common/util/RequestHolder.java +++ b/src/main/java/im/zhaojun/zfile/common/util/RequestHolder.java @@ -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 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(); diff --git a/src/main/java/im/zhaojun/zfile/home/service/impl/FtpServiceImpl.java b/src/main/java/im/zhaojun/zfile/home/service/impl/FtpServiceImpl.java index 68b497a..c635309 100644 --- a/src/main/java/im/zhaojun/zfile/home/service/impl/FtpServiceImpl.java +++ b/src/main/java/im/zhaojun/zfile/home/service/impl/FtpServiceImpl.java @@ -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 { 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); diff --git a/src/main/java/im/zhaojun/zfile/home/service/impl/SftpServiceImpl.java b/src/main/java/im/zhaojun/zfile/home/service/impl/SftpServiceImpl.java index 4569e03..415a45a 100644 --- a/src/main/java/im/zhaojun/zfile/home/service/impl/SftpServiceImpl.java +++ b/src/main/java/im/zhaojun/zfile/home/service/impl/SftpServiceImpl.java @@ -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 { 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);