您的位置:首页 > 其它

点击按钮下载文件

2015-08-31 15:34 288 查看
 


@RequestMapping("/download.do")
public void download(HttpServletRequest request,
HttpServletResponse response)
throws Exception {
String filePath = "文件路径";
FileInputStream fis = null;
OutputStream os = null;
try {
fis = new FileInputStream(filePath);
os = response.getOutputStream();// 取得输出流
response.reset();// 清空输出流
response.setHeader("Content-disposition", "attachment; filename="
+ filePath.substring(filePath.lastIndexOf("/") + 1));// 设定输出文件头
response.setContentType("application/x-download");
byte[] mybyte = new byte[8192];
int len = 0;
while ((len = fis.read(mybyte)) != -1) {
os.write(mybyte, 0, len);
}
os.close();
} catch (IOException e) {
throw e;
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: