现在就说说文件批量下载的流程吧:先是创建一个临时的压缩包,把你想要的东西放在压缩包了,然后在下载,最后再把临时压缩包删除。
看代码,我做的是批量下载图片的,先根据时间读取的图片名称,然后再项目web目录下找到对应的图片的
public void download1(HttpServletRequest req,HttpServletResponse response) {
List<String> list=new ArrayList<String>();//这是需要下载的文件名称,我是下载的图片这个是从数据库读取出来的图片名称
String relativelyPath=req.getSession().getServletContext().getRealPath("/");
String filePath = relativelyPath+"\\zip\\DownLoad.zip";//所要下载的文件路径,从数据库中查询得到,当然也可以直接写文件路径,如:C:\\Users\\Administrator\\Desktop\\csv\\号码_utf8_100.csv(这里我写的是相对路径)
FileUpload.filedocument(relativelyPath,list);
try {
File file = new File(filePath);
String fileName = filePath.substring(filePath.lastIndexOf(File.separator)+1);//得到文件名
fileName = new String(fileName.getBytes("UTF-8"),"ISO8859-1");//把文件名按UTF-8取出并按ISO8859-1编码,保证弹出窗口中的文件名中文不乱码,中文不要太多,最多支持17个中文,因为header有150个字节限制。
response.setContentType("application/octet-stream");//告诉浏览器输出内容为流;设置了头文件才会有弹框;
response.addHeader("Content-Disposition", "attachment;filename="+fileName);//Content-Disposition中指定的类型是文件的扩展名,并且弹出的下载对话框中的文件类型图片是按照文件的扩展名显示的,点保存后,文件以filename的值命名,保存类型以Content中设置的为准。注意:在设置Content-Disposition头字段之前,一定要设置Content-Type头字段。
String len = String.valueOf(file.length());
response.setHeader("Content-Length", len);//设置内容长度
OutputStream out = response.getOutputStream();
FileInputStream in = new FileInputStream(file);
byte[] b = new byte[1024];
int n;
while((n=in.read(b))!=-1){
out.write(b, 0, n);
}
in.close();
out.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally {
FileUpload.deleteFile(filePath);
}
}
现在是帮助类
public class FileUpload {
/**
*
* @param path1相对路径
* @param path要下载的图片名字集合
*/
public static void filedocument(String path1,List<String> path) {
String relativelyPath=System.getProperty("user.dir");
System.out.println(relativelyPath+"\\src\\main\\webapp\\zip");
// 要生成的压缩文件地址和文件名称
String desPath = path1+"\\zip\\DownLoad.zip";//我是生成在web目录下的
File zipFile = new File(path1+"\\zip\\DownLoad.zip");
ZipOutputStream zipStream = null;
FileInputStream zipSource = null;
BufferedInputStream bufferStream = null;
try {
//构造最终压缩包的输出流
zipStream = new ZipOutputStream(new FileOutputStream(zipFile));
for(int i =0;i<path.size();i++){
File file = new File(path1+"QRCode/"+path.get(i));//这是将图片的路径写下来,刚刚只是图片的名称
//将需要压缩的文件格式化为输入流
zipSource = new FileInputStream(file);
//压缩条目不是具体独立的文件,而是压缩包文件列表中的列表项,称为条目,就像索引一样
ZipEntry zipEntry = new ZipEntry(file.getName());
//定位该压缩条目位置,开始写入文件到压缩包中
zipStream.putNextEntry(zipEntry);
//输入缓冲流
bufferStream = new BufferedInputStream(zipSource, 1024 * 10);
int read = 0;
//创建读写缓冲区
byte[] buf = new byte[1024 * 10];
while((read = bufferStream.read(buf, 0, 1024 * 10)) != -1)
{
zipStream.write(buf, 0, read);
}
}
} catch (Exception e) {
e.printStackTrace();
} finally {
//关闭流
try {
if(null != bufferStream) bufferStream.close();
if(null != zipStream) zipStream.close();
if(null != zipSource) zipSource.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
/**
* 删除单个文件
*
* @param fileName
* 要删除的文件的文件名
* @return 单个文件删除成功返回true,否则返回false
*/
public static boolean deleteFile(String fileName) {
File file = new File(fileName);
// 如果文件路径所对应的文件存在,并且是一个文件,则直接删除
if (file.exists() && file.isFile()) {
if (file.delete()) {
System.out.println("删除单个文件" + fileName + "成功!");
return true;
} else {
System.out.println("删除单个文件" + fileName + "失败!");
return false;
}
} else {
System.out.println("删除单个文件失败:" + fileName + "不存在!");
return false;
}
}
}
我是在服务器上测试过了是可以实现下载的。