强制浏览器在JAVA中下载docx文件会生成损坏的文档

博主遇到一个问题,在使用Java强制浏览器下载docx文件时,文件会被损坏。尝试了多种MIME类型,包括应用了IANA推荐的类型,但问题依然存在。已排除文件上传时的错误,怀疑是下载过程中出现了干扰。寻求解决方案。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

使用JAVA,我试图强制浏览器下载文件。

这是我当前使用的代码:

response.reset();
response.resetBuffer();
response.setContentType(mimeType);
response.setHeader("Content-Disposition", "attachment; filename=\"" + fileName + "\"");

InputStream in = new FileInputStream(file);
OutputStream out = response.getOutputStream();
IOUtils.copy(in, out);

out.flush();
out.close();
in.close();
response.flushBuffer();

它 几乎可以 正常工作,但是当强制下载docx文档(MS Office 2007+)时,下载的文件已损坏(至少,这是MS
Office告诉我的)。如果尝试在存储它们的服务器上直接打开它,则不会出现该错误,这意味着下载时(而不是上传时)确实会干扰该问题。

根据IANA的说法,此类文件的MIME类型应为application/vnd.openxmlformats- officedocument.wordprocessingml.document (1) ,但是指定MIME类型不能解决问题。

Web上有很多 曲目
,但是没有一个对我有用。似乎有一个解决方案ASP.NET,但我没有找到在等效JAVA

我也尝试使用MIME类型application/vnd.ms-word (2),*
就像我在那里看到的那样,但是下载的文件仍然损坏。一个人在这里建议使用MIME类型application/msword
(3) ,并在本论坛上提出的MIME类型 (4)
application/octet- stream 
*

然后,对于这四种MIME类型中的每一种,我试图将下载文件的名称从myfile.docx更改为myfile.doc(不再是 x
),但问题仍然存在。

那么,如何强制下载 未损坏的 docx文件?我的代码正确吗?


阅读 74

06-27
package com.shunyu.yiwu.ai.platform.controller; import io.swagger.v3.oas.annotations.tags.Tag; import jakarta.servlet.http.HttpServletResponse; import org.springframework.core.io.Resource; import org.springframework.core.io.support.PathMatchingResourcePatternResolver; import org.springframework.core.io.support.ResourcePatternResolver; import org.springframework.http.*; import org.springframework.util.StreamUtils; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import java.io.IOException; import java.io.InputStream; import java.net.URLEncoder; import java.nio.charset.StandardCharsets; import java.util.Arrays; import java.util.Comparator; import java.util.List; import java.util.Objects; /** * 文件下载控制器 */ @RestController @RequestMapping("/file/download") @Tag(name = "文件下载", description = "处理文件下载相关接口") public class FileDownloadController { private final ResourcePatternResolver resourcePatternResolver; public FileDownloadController(ResourcePatternResolver resourcePatternResolver) { this.resourcePatternResolver = resourcePatternResolver; } /** * 下载帮助文档(默认返回目录下第一个文件) */ @GetMapping("/help-doc") public ResponseEntity<byte[]> downloadHelpDoc() { try { // 获取help-docs目录下的所有文件 Resource[] resources = resourcePatternResolver.getResources("classpath:help-docs/*"); if (resources.length == 0) { return ResponseEntity.notFound().build(); } // 获取第一个文件(按文件名排序以确保一致性) List<Resource> resourceList = Arrays.asList(resources); resourceList.sort((r1, r2) -> { try { return Objects.requireNonNull(r1.getFilename()).compareTo(Objects.requireNonNull(r2.getFilename())); } catch (Exception e) { return 0; } }); Resource fileResource = resourceList.get(0); String filename = fileResource.getFilename(); // 读取文件内容 byte[] fileContent; try (InputStream inputStream = fileResource.getInputStream()) { fileContent = inputStream.readAllBytes(); // 使用更可靠的读取方式 } // 检测内容类型 String contentType = detectContentTypeFromName(filename); // 对于Office Open XML格式文件,使用通用二进制流类型以避免编码问题 if (filename != null && (filename.toLowerCase().endsWith(".docx") || filename.toLowerCase().endsWith(".xlsx") || filename.toLowerCase().endsWith(".pptx"))) { contentType = MediaType.APPLICATION_OCTET_STREAM_VALUE; } // 对文件名进行UTF-8编码 String encodedFileName = null; if (filename != null) { encodedFileName = URLEncoder.encode(filename, StandardCharsets.UTF_8) .replace("+", "%20"); } // 构建响应 return ResponseEntity.ok() .contentType(MediaType.parseMediaType(contentType)) .header(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename*=UTF-8''" + encodedFileName) .header(HttpHeaders.ACCESS_CONTROL_EXPOSE_HEADERS, HttpHeaders.CONTENT_DISPOSITION) .header(HttpHeaders.CACHE_CONTROL, "no-cache, no-store, must-revalidate") .header(HttpHeaders.PRAGMA, "no-cache") .header(HttpHeaders.EXPIRES, "0") .header(HttpHeaders.CONTENT_LENGTH, String.valueOf(fileContent.length)) .body(fileContent); } catch (Exception e) { e.printStackTrace(); return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build(); } } /** * 根据文件名检测Content-Type */ private String detectContentTypeFromName(String filename) { if (filename == null) { return MediaType.APPLICATION_OCTET_STREAM_VALUE; } String fileName = filename.toLowerCase(); if (fileName.endsWith(".pdf")) { return "application/pdf"; } else if (fileName.endsWith(".doc")) { return "application/msword"; } else if (fileName.endsWith(".docx")) { return "application/vnd.openxmlformats-officedocument.wordprocessingml.document"; } else if (fileName.endsWith(".xls")) { return "application/vnd.ms-excel"; } else if (fileName.endsWith(".xlsx")) { return "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"; } else if (fileName.endsWith(".ppt")) { return "application/vnd.ms-powerpoint"; } else if (fileName.endsWith(".pptx")) { return "application/vnd.openxmlformats-officedocument.presentationml.presentation"; } else if (fileName.endsWith(".txt")) { return "text/plain; charset=UTF-8"; } else if (fileName.endsWith(".html") || fileName.endsWith(".htm")) { return "text/html; charset=UTF-8"; } else if (fileName.endsWith(".css")) { return "text/css; charset=UTF-8"; } else if (fileName.endsWith(".js")) { return "application/javascript"; } else if (fileName.endsWith(".json")) { return "application/json; charset=UTF-8"; } else if (fileName.endsWith(".xml")) { return "application/xml; charset=UTF-8"; } else if (fileName.endsWith(".jpg") || fileName.endsWith(".jpeg")) { return "image/jpeg"; } else if (fileName.endsWith(".png")) { return "image/png"; } else if (fileName.endsWith(".gif")) { return "image/gif"; } else if (fileName.endsWith(".svg")) { return "image/svg+xml"; } else if (fileName.endsWith(".zip")) { return "application/zip"; } else if (fileName.endsWith(".rar")) { return "application/vnd.rar"; } else if (fileName.endsWith(".7z")) { return "application/x-7z-compressed"; } else { return MediaType.APPLICATION_OCTET_STREAM_VALUE; } } }以上的代码经过我的下载测试,我发现了下载文件docx格式会出现乱码现象,下载pdf格式会出现空白页面,下载xlsx格式跟源文件保持一致,下载txt格式跟源文件保持一致. 请进行解决
最新发布
08-27
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值