发送压缩文件春季 [英] Sending zipped file Spring

查看:81
本文介绍了发送压缩文件春季的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I would like to send an already existing zipped file through my spring controller but I keep getting these error messages org.springframework.web.HttpMediaTypeNotAcceptableException: Could not find acceptable representation or a NoHandlerFoundException which results in a 404 response. Is there something that I am missing? This is my controller code

@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);
    }
}

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

Break points in my method are not even being reached.

推荐答案

您需要这样的内容:

@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();
}

这篇关于发送压缩文件春季的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆