Mik*_*has 6 action controller spring-mvc download
我有一个使用Spring Framework 3构建的系统,现在我必须实现文件下载.要执行"donwload操作",我通常会获取HttpServletReponse对象,设置标头并从中获取用户输出流.
它运作良好,但我想知道是否有更容易/更聪明的方法吗?
非常感谢!
你可以使用@ResponseBody
并返回一个byte[]
@RequestMapping("/getImage")
@ResponseBody
public byte[] getImage(HttpServletResponse response) throws IOException {
File imageFile = new File("image.jpg");
byte[] bytes = org.springframework.util.FileCopyUtils.copyToByteArray(imageFile);
response.setHeader("Content-Disposition", "attachment; filename=\"" + imageFile.getName() + "\"");
response.setContentLength(bytes.length);
response.setContentType("image/jpeg");
return bytes;
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
6953 次 |
最近记录: |