文件下载
来源:互联网 发布:消费者数据分析 编辑:程序博客网 时间:2024/05/19 14:54
//下载
public void downFile() throws IOException{
String fileName = request.getParameter("fileName");
fileName = URLDecoder.decode(fileName, "UTF-8");
response.setCharacterEncoding("UTF-8");
boolean isOnLine = false;
Properties commonPro = new Properties();
String path = "config/common.properties";
InputStream is = AndroidVersionMainupdateAction.class.getClassLoader().getResourceAsStream(path);
commonPro.load(is);
String pt = commonPro.getProperty("filePath");
String filePath =pt+"\\"+fileName;
System.out.println("filePath"+filePath);
File f = new File(filePath);
if (!f.exists()) {
response.sendError(404, "File not found!");
return;
}
BufferedInputStream br = new BufferedInputStream(new FileInputStream(f));
byte[] buf = new byte[1024];
int len = 0;
response.reset(); //非常重要
if(isOnLine){ //在线打开方式
URL u = new URL("file:///"+filePath);
response.setContentType(u.openConnection().getContentType());
response.setHeader("Content-Disposition", "inline; filename="+(f.getName()).getBytes("gbk"));
//文件名应该编码成UTF-8
}
else{ //纯下载方式
response.setContentType("application/x-msdownload");
response.setHeader("Content-Disposition", "attachment; filename=" + java.net.URLEncoder.encode(f.getName(),"UTF-8"));
}
OutputStream out = response.getOutputStream();
while ((len = br.read(buf)) > 0) out.write(buf, 0, len);
br.close();
out.close();
}
public void downFile() throws IOException{
String fileName = request.getParameter("fileName");
fileName = URLDecoder.decode(fileName, "UTF-8");
response.setCharacterEncoding("UTF-8");
boolean isOnLine = false;
Properties commonPro = new Properties();
String path = "config/common.properties";
InputStream is = AndroidVersionMainupdateAction.class.getClassLoader().getResourceAsStream(path);
commonPro.load(is);
String pt = commonPro.getProperty("filePath");
String filePath =pt+"\\"+fileName;
System.out.println("filePath"+filePath);
File f = new File(filePath);
if (!f.exists()) {
response.sendError(404, "File not found!");
return;
}
BufferedInputStream br = new BufferedInputStream(new FileInputStream(f));
byte[] buf = new byte[1024];
int len = 0;
response.reset(); //非常重要
if(isOnLine){ //在线打开方式
URL u = new URL("file:///"+filePath);
response.setContentType(u.openConnection().getContentType());
response.setHeader("Content-Disposition", "inline; filename="+(f.getName()).getBytes("gbk"));
//文件名应该编码成UTF-8
}
else{ //纯下载方式
response.setContentType("application/x-msdownload");
response.setHeader("Content-Disposition", "attachment; filename=" + java.net.URLEncoder.encode(f.getName(),"UTF-8"));
}
OutputStream out = response.getOutputStream();
while ((len = br.read(buf)) > 0) out.write(buf, 0, len);
br.close();
out.close();
}
阅读全文
0 0
- 下载图片,下载文件
- 文件下载
- 文件下载
- 文件下载
- 文件下载
- 文件下载
- 文件下载
- 下载文件
- 文件下载
- 下载文件
- 文件下载
- 文件下载
- 下载文件
- 文件下载
- 下载文件
- 下载文件
- 文件下载
- 下载文件
- STM32使用J-Link的SWD接口调试出现Supply voltage is too low问题的解决办法
- UVALive 3263 That Nice Euler Circuit
- spring学习入门
- 九九乘法表
- Win10+Anaconda+python3.6安装Tensorflow
- 文件下载
- B&G GLOBAL MARKETS全球影响力如何
- 融云通讯简易集成
- Java泛型中E、T、K、V等的含义
- 【实战】学生信息保存到xm文件l中
- Flume 概述架构及部署
- [python]报错汇总【持续更新】
- 三点估算
- Linux 服务器安装MySQL数据库