文件下载
//下载
@RequestMapping("down")
public void down(String filename, HttpServletRequest request, HttpServletResponse response) throws IOException {
//设置响应头
response.setHeader("Content-Disposition", "attachment;filename="+filename);
//获取文件路径
String path = request.getServletContext().getRealPath("/file");
File file = new File(path,filename);
//获取输出流(字节流)
ServletOutputStream os = response.getOutputStream();
os.write(FileUtils.readFileToByteArray(file));
os.flush();
os.close();
}
本文介绍了一个文件下载API的实现方式,通过使用@RequestMapping注解映射请求路径,设置响应头为Content-Disposition并附带文件名,从服务器获取指定文件路径,然后通过ServletOutputStream将文件内容输出到客户端,实现了文件的下载功能。
510

被折叠的 条评论
为什么被折叠?



