文件下载
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();
}
}
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();
}
}
相关文章推荐
- struts2 文件下载
- dnw下载文件时can not open /dev/secbulk0的解决方法
- JAVA文件上传和下载
- 解决从tomcat和Resin服务器下载apk,ipa文件自动转化为zip的问题
- 为什么深信服设备配置文件不可以右键另存为下载
- c# 下载文件xmlhttp
- 通过Web Services上传和下载文件
- linux下载github中的文件
- Android中AsyncTask进行后台下载文件并在下拉菜单显示下载进度
- jsp实现文件下载
- php 下载保存文件保存到本地的两种实现方法
- 与省局网站备案管理系统接口规范试行版文件下载地址
- 使用HTTP协议 下载文件
- 从网页下载文件
- Struts2下载及其配置文件编辑
- Linux中使用SecureCRT上传、下载文件命令sz与rz用法实例
- 文件的上传和下载(一)
- struts2下载文件功能(边下载边打包)
- smartUpload上传下载文件以及中文乱码
- 【Django】Django 文件下载最佳实践