下载服务器上的文件

@RequestMapping("downLoadExcel")
public String downLoadExcel(HttpServletRequest request,HttpServletResponse response){
try {
//路径
String path = request.getSession().getServletContext().getRealPath("/template/");
     //下面的两行设置响应头,必须设置,以弹框的形式下载 中文乱码记得要转码啊!!!!!!!!!!!!!
response.setContentType("application/octet-stream");
response.setHeader("Content-Disposition", "attachment;fileName="+new String("指数模板.xlsx".getBytes(),"ISO8859-1"));//
ServletOutputStream out;
     //File.separator 文件夹的分割线,不同的操作系统之间可以通用
File file = new File(path+File.separator+ "指数模板.xlsx");
FileInputStream inputStream = new FileInputStream(file);
out = response.getOutputStream();
int len= 0;
byte[] buffer = new byte[1024];
while ((len=inputStream.read(buffer))!=-1){
out.write(buffer,0,len);
}
inputStream.close();
out.close();
out.flush();
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
posted @ 2016-10-14 14:02  小杨羊  阅读(515)  评论(0编辑  收藏  举报