Java 在 Spring MVC REST 中返回文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/32641231/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 12:53:43  来源:igfitidea点击:

Return file in Spring MVC REST

javaspringresthttpspring-mvc

提问by arpit joshi

I have the REST Service Code Below code which returns a File ,now the problem is in the response body on the PostMan Client I get a raw response ,how can I can convert it so that it displays the contents of the file to the client ,Goal is to return a file to the user .File Name is "File1.jpeg"

我有 REST 服务代码下面的代码返回一个文件,现在问题出在 PostMan 客户端上的响应正文中我得到一个原始响应,我该如何转换它以便它向客户端显示文件的内容,目标是向用户返回一个文件。文件名是“File1.jpeg”

Code:

代码:

@RequestMapping(value = URIConstansts.GET_FILE, produces = { "application/json" }, method = RequestMethod.GET)
public @ResponseBody ResponseEntity getFile(@RequestParam(value="fileName", required=false) String fileName,HttpServletRequest request) throws IOException{

    ResponseEntity respEntity = null;

    byte[] reportBytes = null;
    File result=new File("/home/arpit/Documents/PCAP/dummyPath/"+fileName);

    if(result.exists()){
        InputStream inputStream = new FileInputStream("/home/arpit/Documents/PCAP/dummyPath/"+fileName); 


        byte[]out=org.apache.commons.io.IOUtils.toByteArray(inputStream);

        HttpHeaders responseHeaders = new HttpHeaders();
        responseHeaders.add("content-disposition", "attachment; filename=" + fileName);

        respEntity = new ResponseEntity(out, responseHeaders,HttpStatus.OK);


    }else{

        respEntity = new ResponseEntity ("File Not Found", HttpStatus.OK);
    }


    return respEntity;

}

采纳答案by arpit joshi

The code below solved my problem:

下面的代码解决了我的问题:

@RequestMapping(value = URIConstansts.GET_FILE, produces = { "application/json" }, method = RequestMethod.GET)
public @ResponseBody ResponseEntity getFile(@RequestParam(value="fileName", required=false) String fileName,HttpServletRequest request) throws IOException{

    ResponseEntity respEntity = null;

    byte[] reportBytes = null;
    File result=new File("/home/arpit/Documents/PCAP/dummyPath/"+fileName);

    if(result.exists()){
        InputStream inputStream = new FileInputStream("/home/arpit/Documents/PCAP/dummyPath/"+fileName);
        String type=result.toURL().openConnection().guessContentTypeFromName(fileName);

        byte[]out=org.apache.commons.io.IOUtils.toByteArray(inputStream);

        HttpHeaders responseHeaders = new HttpHeaders();
        responseHeaders.add("content-disposition", "attachment; filename=" + fileName);
        responseHeaders.add("Content-Type",type);

        respEntity = new ResponseEntity(out, responseHeaders,HttpStatus.OK);
    }else{
        respEntity = new ResponseEntity ("File Not Found", HttpStatus.OK);
    }
    return respEntity;
}

回答by DhruvG

You need to use different content type instead of produces = { application/json" }

您需要使用不同的内容类型而不是产生 = { application/json" }

Content-types

内容类型

http://silk.nih.gov/public/[email protected]

http://silk.nih.gov/public/[email protected]

If it still dont work then try to get HttpServletResponse and write your file data to Stream with response.setContentType();

如果它仍然不起作用,那么尝试获取 HttpServletResponse 并使用 response.setContentType() 将您的文件数据写入 Stream ;

Note :: Recently I have use response.getOutputStream to write a Excel file. Due to some reasons setting produces wasnt working for me.

注意 :: 最近我用 response.getOutputStream 写了一个 Excel 文件。由于某些原因,设置产品对我不起作用。

Also you can use Firebug in firefox to see Response headers.

您也可以在 firefox 中使用 Firebug 来查看响应头。