gpt4 book ai didi

Java REST WS下载docx

转载 作者:行者123 更新时间:2023-11-30 02:58:23 24 4
gpt4 key购买 nike

我正在开发一个基于 Springboot Rest 的 Web 应用程序。其中一个 WS 必须返回 .docx 文档。代码是:

@RequestMapping(value = "/get-doc",method = RequestMethod.GET, produces="application/vnd.openxmlformats-officedocument.wordprocessingml.document")
public @ResponseBody HttpEntity<File> getDoc() {
File file = userService.getDocx();
HttpHeaders header = new HttpHeaders();
header.set("Content-Disposition", "attachment; filename=DocxProject.docx");
header.setContentLength(file.length());

return new HttpEntity<File>(file,header);
}

但我遇到了这个错误:

org.springframework.web.HttpMediaTypeNotAcceptableException: Could not find acceptable representation

我搜索了其他问题,但没有一个给我解决方案,主要是因为他们使用 javax.ws.rs 但我不想依赖它。

我正在寻找的是我遇到的错误的解决方案或我的代码的替代方案(不依赖 javax.ws.rs)。

提前致谢。

最佳答案

尝试返回字节数组。简化您的代码:

@RequestMapping(value = "/get-doc",method = RequestMethod.GET, produces="application/vnd.openxmlformats-officedocument.wordprocessingml.document")
public @ResponseBody byte[] getDoc() {
File file = userService.getDocx();
FileInputStream fis = new FileInputStream(file);
byte[] doc = IOUtils.toByteArray(fis);
return doc;
}

IOUtils 来自org.apache.commons.io.IOUtils。我没有测试过,但我有一个类似的方法来返回图像。我希望这对您有帮助。

关于Java REST WS下载docx,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36620290/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com