From 5cb28441412402dc716a8a3bbc180b3e02cf3dc7 Mon Sep 17 00:00:00 2001 From: zhaojun <873019219@qq.com> Date: Mon, 16 May 2022 20:50:49 +0800 Subject: [PATCH] =?UTF-8?q?:bug:=20=E4=BF=AE=E5=A4=8D=E9=94=99=E8=AF=AF?= =?UTF-8?q?=E7=9A=84=20content=5Fdisposition=20=E5=92=8C=20contentType=20?= =?UTF-8?q?=E5=AF=BC=E8=87=B4=E4=B8=8B=E8=BD=BD=E6=96=87=E4=BB=B6=E6=A0=BC?= =?UTF-8?q?=E5=BC=8F=E8=A2=AB=E6=B5=8F=E8=A7=88=E5=99=A8=E8=AF=86=E5=88=AB?= =?UTF-8?q?=E9=94=99=E8=AF=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/im/zhaojun/zfile/util/FileUtil.java | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/src/main/java/im/zhaojun/zfile/util/FileUtil.java b/src/main/java/im/zhaojun/zfile/util/FileUtil.java index bef8a48..7e7d324 100644 --- a/src/main/java/im/zhaojun/zfile/util/FileUtil.java +++ b/src/main/java/im/zhaojun/zfile/util/FileUtil.java @@ -137,16 +137,18 @@ public class FileUtil { long contentLength = endByte - startByte + 1; //文件类型 String contentType = request.getServletContext().getMimeType(fileName); - - if (Objects.equals(type, LocalFileResponseTypeConstant.DOWNLOAD) || StrUtil.isEmpty(contentType)) { - contentType = "attachment"; + if (StrUtil.isEmpty(contentType)) { + contentType = "application/octet-stream"; } response.setHeader(HttpHeaders.ACCEPT_RANGES, "bytes"); response.setHeader(HttpHeaders.CONTENT_TYPE, contentType); // 这里文件名换你想要的,inline 表示浏览器可以直接使用 // 参考资料:https://developer.mozilla.org/zh-CN/docs/Web/HTTP/Headers/Content-Disposition - response.setHeader(HttpHeaders.CONTENT_DISPOSITION, contentType + ";filename=" + URLUtil.encode(fileName)); + if (Objects.equals(type, LocalFileResponseTypeConstant.DOWNLOAD) || StrUtil.isEmpty(contentType)) { + String contentDisposition = "attachment;filename=" + URLUtil.encode(fileName); + response.setHeader(HttpHeaders.CONTENT_DISPOSITION, contentDisposition); + } response.setHeader(HttpHeaders.CONTENT_LENGTH, String.valueOf(contentLength)); // [要下载的开始位置]-[结束位置]/[文件总大小] response.setHeader(HttpHeaders.CONTENT_RANGE, "bytes " + startByte + rangeSeparator + endByte + "/" + file.length()); @@ -193,4 +195,4 @@ public class FileUtil { } } } -} +} \ No newline at end of file