Java批量下载方法解析

 import java.io.File;

import java.io.FileInputStream;

import java.io.FileOutputStream;

import java.util.zip.ZipEntry;

import java.util.zip.ZipOutputStream;

public class ZipOutputStreamDemo {

public static void main(String[] args) throws Exception {

byte[] buffer = new byte[1024];

//生成的ZIP文件名为Demo.zip

String strZipName = "Demo.zip";

ZipOutputStream out = new ZipOutputStream(new FileOutputStream(strZipName));

//需要同时下载的两个文件result.txt ,source.txt

File[] file1 = {new File("result.txt"),new File("source.txt")};

for(int i=0;i
FileInputStream fis = new FileInputStream(file1[i]);

out.putNextEntry(new ZipEntry(file1[i].getName()));

int len;

//读入需要下载的文件的内容,打包到zip文件

while((len = fis.read(buffer))>0) {

out.write(buffer,0,len);

}

out.closeEntry();

fis.close();

}

out.close();

System.out.println("生成Demo.zip成功");

}













2010-09-13 09:06 java批量下载文件这是我自己写的一个批量下载,是写在Action中的,首先是将要下载的文件打包成.zip的文件,在下载。这里供大家参考,望大家找出更好的方法。注:这是从服务器上下载

response.setContentType("text/html;charset=GBK"); //中文乱码处理
String str=request.getParameter("str"); //获取需要下载文件的ID字符串
String[] num=str.split(",");
List listall=(List)request.getSession().getAttribute("listupload");
//存储下载的文件路径
File[] files=new File[num.length];
for(int i=0;iUploadInfo upinfo=(UploadInfo)listall.get(i);
for(int k=0;kif(upinfo.getUpId().equals(new Integer(num[k]))){
String upname=upinfo.getUpName();
String path=servlet.getServletContext().getRealPath("/")+"uploads\\"+upname;
files[k]=new File(path);
}
}
}
byte[] bt=new byte[8192];
//生成zip文件的名称,必须写明保存路径,这里是保存到服务器上,这样你下载下来的才会到你自己的电脑上。如果你是在你自己电脑上下载 那就不需要保存在服务器了。
String strzipName=servlet.getServletContext().getRealPath("/")+"uploads\\"+"resume.zip";
String strs="resume.zip";
try {
ZipOutputStream zout=new ZipOutputStream(new FileOutputStream(strzipName));
//循环下载每个文件并读取内容
for(int j=0;jFileInputStream is=new FileInputStream(files[j]);
ZipEntry ze=new ZipEntry(URLEncoder.encode(files[j].getName(), "UTF-8"));//设置文件编码格式
zout.putNextEntry(ze);
int len;
//读取下载文件内容
while((len=is.read(bt))>0){
zout.write(bt, 0, len

);
}
zout.closeEntry();
is.close();
}
zout.close();
this.toUpload(response, strs);//调用下载方法
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}


//下载公用方法
public void toUpload(HttpServletResponse response,String str){
try {
String path=servlet.getServletContext().getRealPath("/")+"uploads\\"+str;
if(!"".equals(path)){
File file=new File(path);
if(file.exists()){
InputStream ins=new FileInputStream(path);
BufferedInputStream bins=new BufferedInputStream(ins);//放到缓冲流里面
OutputStream outs=response.getOutputStream();//获取文件输出IO流
BufferedOutputStream bouts=new BufferedOutputStream(outs);
response.setContentType("application/x-download");//设置response内容的类型
response.setHeader("Content-disposition","attachment;filename="+ URLEncoder.encode(str, "UTF-8"));//设置头部信息
int bytesRead = 0;
byte[] buffer = new byte[8192];
//开始向网络传输文件流
while ((bytesRead = bins.read(buffer, 0, 8192)) != -1) {
bouts.write(buffer, 0, bytesRead);
}
bouts.flush();//这里一定要调用flush()方法
ins.close();
bins.close();
outs.close();
bouts.close();
}
else{
response.sendRedirect("../error.jsp");
}
}
else{
response.sendRedirect("../error.jsp");

注;这里面不要用到PrintWriter out=response.getWriter();这里调用了response对象,后面下载调用时就会出错。这里要是想都用,希望大家找到解决办法。
}
} catch (IOException e) {
e.printStackTrace();
}
}

相关文档
最新文档