从application/octet-stream
InputStream
RestController
返回HttpResponse
的最佳方法是什么?
我是否必须在"标准"中将其写入{{1}} Java方式还是Spring为此提供了一些机制?
答案 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();
}