java 下载文件 例:excel
2013-05-10 17:32
405 查看
button提交
String filepath = request.getSession().getServletContext().getRealPath("");
String fileName;
fileName = "expenditure.xls";
File file= new File(filepath + "/" + fileName);
try {
//输出流用来向response输出数据
BufferedOutputStream output = new BufferedOutputStream(response.getOutputStream());
// 输入流用来从文件读入数据
BufferedInputStream input = new BufferedInputStream(new FileInputStream(file));
String filename = file.getName();
long fileLength = file.length();
int iFileContent = 0;
byte buffer[] = new byte[2048];
response.reset();
response.setContentType("application/ms-excel"); //文件的保存类型为MS-excel类型
response.setHeader("Content-Disposition", "attachment;filename=\""+URLEncoder.encode(filename, "UTF-8")+"\"");
//URLEncoder.encode(filename, "UTF-8")可以解决中文名乱码问题
response.setContentLength((int)fileLength); //设置文件长度
while((iFileContent = input.read(buffer, 0, 2048)) !=-1){
response.getOutputStream().write(buffer, 0, iFileContent);
}
response.flushBuffer();
input.close();
output.close();
} catch (IOException e1) {
e1.printStackTrace();
}
String filepath = request.getSession().getServletContext().getRealPath("");
String fileName;
fileName = "expenditure.xls";
File file= new File(filepath + "/" + fileName);
try {
//输出流用来向response输出数据
BufferedOutputStream output = new BufferedOutputStream(response.getOutputStream());
// 输入流用来从文件读入数据
BufferedInputStream input = new BufferedInputStream(new FileInputStream(file));
String filename = file.getName();
long fileLength = file.length();
int iFileContent = 0;
byte buffer[] = new byte[2048];
response.reset();
response.setContentType("application/ms-excel"); //文件的保存类型为MS-excel类型
response.setHeader("Content-Disposition", "attachment;filename=\""+URLEncoder.encode(filename, "UTF-8")+"\"");
//URLEncoder.encode(filename, "UTF-8")可以解决中文名乱码问题
response.setContentLength((int)fileLength); //设置文件长度
while((iFileContent = input.read(buffer, 0, 2048)) !=-1){
response.getOutputStream().write(buffer, 0, iFileContent);
}
response.flushBuffer();
input.close();
output.close();
} catch (IOException e1) {
e1.printStackTrace();
}
相关文章推荐
- java excel 文件下载
- Java操作Excel之Excel文件的下载
- java实时向zip中写excel文件并通过浏览器实现下载
- java已知下载链接将Excel文件利用httpclient下载到本地
- java实现赋值excel模板,并在新文件中写入数据,并且下载
- Java Excel文件下载
- JAVA JXL -----excel文件生成、在线打开和下载
- Java代码生成excel文件下载
- java基于jxl.jar包生成Excel文件到指定目录和服务器下载两种模式的使用方法
- java 多excel下载 打成zip压缩包 程序中创建文件 响应到浏览器(三)
- java 动态生成多个excel文件打成zip包供用户下载
- Java通用的Excel文件生成工具类,支持生成文件和浏览器直接下载
- java 通用文件下载 excel,pdf,word,jpg
- java生成excel和下载导出文件的工具类
- 【小坑】java下载excel文件
- java 用边读边下载文件,可用于大数据excel下载
- web导出excel--------java导出文件弹出下载框让用户选择路径
- java 多excel下载 打成zip压缩包 程序中创建文件 响应到浏览器(二)
- java 通用文件下载 excel,pdf,word,jpg,exe,rar
- Java文件下载(excel)