下载文件
来源:互联网 发布:听打测试软件 编辑:程序博客网 时间:2024/06/07 03:46
public void downFile() throws Exception{//前台调用方法
String type = getParameter("type");//服务器文件名称
String path = ServletActionContext.getServletContext().getRealPath("/resources/"+type+"");
try {
downFile1(path, "");
} catch (IOException e) {
e.printStackTrace();
setAjaxData("0");
}
}
public String downFile(String path, String ContentType)
throws IOException {if (ContentType == null || ContentType.equals(""))
ContentType = "application/octet-stream";
File file = new File(path);
// 取得文件名。
String filename = file.getName();
// 取得文件的后缀名。
// String ext = filename.substring(filename.lastIndexOf(".") +
// 1).toUpperCase();
// 获取文件读取刘
InputStream fis = new FileInputStream(path);
// 获取response;
HttpServletResponse response = ServletActionContext.getResponse();
// 响应类型
response.setContentType(ContentType);
// 获取写流
ServletOutputStream os = ServletActionContext.getResponse()
.getOutputStream();
;
// 设置下载文件名
/*
* response.addHeader("Content-Disposition", "attachment;filename=" +
* new String(filename.getBytes()));
*/
response.addHeader("Content-Disposition", "attachment;filename="
+ java.net.URLEncoder.encode(filename, "utf-8"));
// 设置下载文件大小
response.addHeader("Content-Length", "" + file.length());
int i = 0;
byte[] b = new byte[1024 * 8];
while ((i = fis.read(b)) > 0) {
os.write(b, 0, i);
}
fis.close();
os.flush();
os.close();
return null;
}
0 0
- 下载图片,下载文件
- 文件下载
- 文件下载
- 文件下载
- 文件下载
- 文件下载
- 文件下载
- 下载文件
- 文件下载
- 下载文件
- 文件下载
- 文件下载
- 下载文件
- 文件下载
- 下载文件
- 下载文件
- 文件下载
- 下载文件
- 数据库笔记 4
- 欢迎使用CSDN-markdown编辑器
- 局域网计算机间进行通信都经历了什么
- java并发编程之volatile关键字解析
- OpenCV学习之编译OpenCV
- 下载文件
- UNIX再学习 -- 函数 system
- Linux同一个环境使用2个jdk
- android studio 手把手教你搭建百度地图
- 解决listview层层嵌套的另外一种思路
- gcc 优化选项 -O1 -O2 -O3 -Os 优先级,-fomit-frame-pointer
- SQLSERVER 批量处理
- 数据库笔记 5
- SpringMVC提高篇(一):常用注解标签