您的位置:首页 > 其它

文件下载

2018-03-18 12:54 113 查看
第一步,本地文件配置地址,application.properties
fileUrl=d://ExcelFile//第二步,前端控制器
//引入文件路径
@Value("${fileUrl}")
private String fileUrl;
/**
* 模板文件下载
* @param response
*/
@RequestMapping(value = "/loadxls",method = RequestMethod.GET)
@ResponseBody
public void loadxls(HttpServletResponse response) {
InputStream fis = null;
try {
//引入文件存储地址
String filename = fileUrl+ "/设备信息数据登记表.xlsx";
System.out.println("下载的路径="+filename);
//获得文件
File file = new File(filename);
//获得字符输入缓冲流
fis = new BufferedInputStream(new FileInputStream(file));
//创建字符数组
byte[] buffer = null;
buffer = new byte[fis.available()];
fis.read(buffer);
fis.close();
response.reset();
// 下载文件名
response.addHeader("Content-Disposition",
"attachment;filename=" + new String("设备信息数据登记表.xlsx".getBytes("utf-8"), "iso8859-1"));
response.addHeader("Content-Length", "" + file.length());
//创建输出流
OutputStream os = null;
//创建输出字符缓冲流
os = new BufferedOutputStream(response.getOutputStream());
response.setContentType("application/octet-stream");
//循环输出
os.write(buffer);
os.flush();
os.close();
} catch (FileNotFoundException e) {
e.getMessage();
} catch (IOException e) {
e.getMessage();
}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  文件下载