JAVA中文件下载和文件批量下载方法
2017-11-14 14:05
417 查看
JAVA中的文件下载:
/**
* 文件下载
* @param request
* @param response
* @param filePath 文件路径
* @param filename 下载时文件名称
*/
public static void downLoadFile(HttpServletRequest request,HttpServletResponse response,String filePath,String filename){
try {
File file=new File(filePath);
// 先去掉文件名称中的空格,然后转换编码格式为utf-8,保证不出现乱码,这个文件名称 用于浏览器的下载框中自动显示的文件名
String userAgent =request.getHeader("User-Agent");
if(userAgent.contains("MSIE")||userAgent.contains("Trident")){
filename= java.net.URLEncoder.encode(filename,"UTF-8");
}else{
filename=new String(filename.getBytes("utf-8"),"iso8859-1");
}
response.addHeader("Content-Disposition", "attachment;filename=" +filename );
//response.setContentType("application/vnd.ms-excel");
response.setContentType("multipart/form-data");
byte[] b = new byte[1024];
int len=0;
FileInputStream fs=new FileInputStream(file);
PrintWriter writer = response.getWriter();
while ((len = fs.read()) != -1) {
writer.write(len);
}
fs.close();
writer.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
JAVA中批量下载文件,将下载多个文件打包成zip文件下载。
//批量文件下载(将多个文件打包成zip包下载)
public static void batchDownLoadFile(HttpServletRequest request,HttpServletResponse response,String filename,String[] filepath,String[] documentname,String loginname){
byte[] buffer = new byte[1024];
Date date=new Date();
//生成zip文件存放位置
String strZipPath = Constant.exportAddress +loginname+date.getTime()+".zip";
File file=new File(Constant.exportAddress);
if(!file.isDirectory() && !file.exists()){
//创建单层目录
// f.mkdir();
// 创建多层目录
file.mkdirs();
}
try {
ZipOutputStream out = new ZipOutputStream(new FileOutputStream(strZipPath));
// 需要同时下载的多个文件
for (int i = 0; i < filepath.length; i++) {
File f=new File(filepath[i]);
FileInputStream fis = new FileInputStream(f);
System.out.println(documentname[i]);
out.putNextEntry(new ZipEntry(documentname[i]));
//设置压缩文件内的字符编码,不然会变成乱码
out.setEncoding("GBK");
int len;
// 读入需要下载的文件的内容,打包到zip文件
while ((len = fis.read(buffer)) > 0) {
out.write(buffer, 0, len);
}
out.closeEntry();
fis.close();
}
out.close();
PublicMethod.downLoadFile(request, response, strZipPath, filename+".zip");
File temp=new File(strZipPath);
if(temp.exists()){
temp.delete();
}
} catch (Exception e) {
System.out.println("文件下载错误");
}
}
/**
* 文件下载
* @param request
* @param response
* @param filePath 文件路径
* @param filename 下载时文件名称
*/
public static void downLoadFile(HttpServletRequest request,HttpServletResponse response,String filePath,String filename){
try {
File file=new File(filePath);
// 先去掉文件名称中的空格,然后转换编码格式为utf-8,保证不出现乱码,这个文件名称 用于浏览器的下载框中自动显示的文件名
String userAgent =request.getHeader("User-Agent");
if(userAgent.contains("MSIE")||userAgent.contains("Trident")){
filename= java.net.URLEncoder.encode(filename,"UTF-8");
}else{
filename=new String(filename.getBytes("utf-8"),"iso8859-1");
}
response.addHeader("Content-Disposition", "attachment;filename=" +filename );
//response.setContentType("application/vnd.ms-excel");
response.setContentType("multipart/form-data");
byte[] b = new byte[1024];
int len=0;
FileInputStream fs=new FileInputStream(file);
PrintWriter writer = response.getWriter();
while ((len = fs.read()) != -1) {
writer.write(len);
}
fs.close();
writer.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
JAVA中批量下载文件,将下载多个文件打包成zip文件下载。
//批量文件下载(将多个文件打包成zip包下载)
public static void batchDownLoadFile(HttpServletRequest request,HttpServletResponse response,String filename,String[] filepath,String[] documentname,String loginname){
byte[] buffer = new byte[1024];
Date date=new Date();
//生成zip文件存放位置
String strZipPath = Constant.exportAddress +loginname+date.getTime()+".zip";
File file=new File(Constant.exportAddress);
if(!file.isDirectory() && !file.exists()){
//创建单层目录
// f.mkdir();
// 创建多层目录
file.mkdirs();
}
try {
ZipOutputStream out = new ZipOutputStream(new FileOutputStream(strZipPath));
// 需要同时下载的多个文件
for (int i = 0; i < filepath.length; i++) {
File f=new File(filepath[i]);
FileInputStream fis = new FileInputStream(f);
System.out.println(documentname[i]);
out.putNextEntry(new ZipEntry(documentname[i]));
//设置压缩文件内的字符编码,不然会变成乱码
out.setEncoding("GBK");
int len;
// 读入需要下载的文件的内容,打包到zip文件
while ((len = fis.read(buffer)) > 0) {
out.write(buffer, 0, len);
}
out.closeEntry();
fis.close();
}
out.close();
PublicMethod.downLoadFile(request, response, strZipPath, filename+".zip");
File temp=new File(strZipPath);
if(temp.exists()){
temp.delete();
}
} catch (Exception e) {
System.out.println("文件下载错误");
}
}
相关文章推荐
- java后台批量下载文件并压缩成zip下载的方法
- java 批量下载图片,批量打包文件并下载
- Java批量下载zip文件
- java 下载文件的方法(2)
- [Java]远程下载文件并读取实例方法
- Java批量下载生成zip文件
- java 批量下载图片,批量打包文件并下载
- tomcat下载文件时,URL含有文字参数与空格参数出错(java.io.FileNotFoundException错误) 解决方法(转)
- java下载文件的几种方法
- 用java实现文件下载的几种方法
- [Java]远程下载文件并读取实例方法
- java提供文件下载的方法
- JAVA上传批量下载文件
- java重命名文件(附道客巴巴文档下载方法)
- 文件上传下载:通过java方法把附件保存到clob字段中
- java 下载文件的方法
- java上传文件跟批量下载文件
- 经过证实的下载文件java实现方法
- java重命名文件(附道客巴巴文档下载方法)
- strtus2 批量下载 中文问题、压缩文件等 ------ 讨论struts2工作流程 (摘自:http://www.blogjava.net/xcp/archive/2009/10/30/downloadlist.html)