gpt4 book ai didi

java - 发送压缩文件 Spring

转载 作者:可可西里 更新时间:2023-11-01 17:05:39 26 4
gpt4 key购买 nike

我想通过我的 spring Controller 发送一个已经存在的压缩文件,但我不断收到这些错误消息 org.springframework.web.HttpMediaTypeNotAcceptableException: Could not find acceptable representationNoHandlerFoundException这会导致 404 响应。有什么我想念的吗?这是我的 Controller 代码

@RequestMapping(
method = RequestMethod.GET,
value = BASE + "/download",
produces = "application/zip"
)
@ResponseBody
public void sendZippedFile(HttpServletResponse response) throws IOException {
try{
File file = new File("C:\\Users\\me\\test.zip");
FileInputStream is = new FileInputStream(file);
response.setContentType("application/zip");
response.setHeader("Content-Disposition","inline; filename=" + file.getName());
response.setHeader("Content-Length", String.valueOf(file.length()));
FileCopyUtils.copy(is, response.getOutputStream());
}catch (IOException e){
response.sendError(HttpServletResponse.SC_NOT_FOUND);
}
}

我的方法中的断点甚至都没有达到。

最佳答案

你需要这样的东西:

@RequestMapping("/download")
public void download(HttpServletResponse response) throws IOException {

FileInputStream inputStream = new FileInputStream(new File("C:\\Users\\me\\test.zip"));

response.setHeader("Content-Disposition", "attachment; filename=\"test.zip\"");
response.setContentType(MediaType.APPLICATION_OCTET_STREAM_VALUE);

ServletOutputStream outputStream = response.getOutputStream();
IOUtils.copy(inputStream, outputStream);

outputStream.close();
inputStream.close();
}

关于java - 发送压缩文件 Spring,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45419170/

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