如何在REST Spring中返回application / octet-stream?

时间:2014-11-28 11:46:28

标签: spring rest mime-types

application/octet-stream InputStream RestController返回HttpResponse的最佳方法是什么? 我是否必须在"标准"中将其写入{{1}} Java方式还是Spring为此提供了一些机制?

1 个答案:

答案 0 :(得分:0)

您可以将输入流写入spring fileInputStream

请参阅此示例:

    File file = new File("File path");
    httpServletResponse.setHeader("Expires", "0");
    httpServletResponse.setHeader("Cache-Control", "must-revalidate, post-check=0, pre-check=0");
    httpServletResponse.setHeader("Pragma", "public");
    httpServletResponse.setHeader("Content-Type", "get content type from file");
    httpServletResponse.setHeader("Content-Length", String.valueOf(file.length()));
    try {
        FileInputStream fileInputStream = new FileInputStream(file);
        FileCopyUtils.copy(fileInputStream, httpServletResponse.getOutputStream());
        fileInputStream.close();
    } catch (Exception e) {
        e.printStackTrace();
    }