文件下载第三种方式(压缩包里边还有文件夹)
来源:互联网 发布:流程优化是什么 编辑:程序博客网 时间:2024/05/11 20:31
/**
*下载多个id下的多个文件(zip)
*/
publicObject downloadByIds(HttpServletRequest req, HttpServletResponsersq,BeanContext bc){
if(magazineIds.length>0){
List<File> files= newArrayList<File>();
List<FileList> fileLists= newArrayList<FileList>();
FileList fileList = new FileList();
String tempory_path = System.currentTimeMillis()+"";
for (int j = 0; j <magazineIds.length; j++) {
List<FileList> lists= magazineSearchMagazineExt.findFileListById(magazineIds[j]);
if(lists!=null){
for(inti=0;i<lists.size();i++){
File file = new File("c:\\"+tempory_path+File.separator+lists.get(0).getDraftId()+File.separator+lists.get(i).getFileName());
if(!file.getParentFile().exists()){
file.getParentFile().mkdirs();
}
try {
FileUtils.copyFile(new File(constantBean.propertiesMap.get("file_path")+File.separator+lists.get(i).getFilePath()),file);
}catch(IOException e) {
e.printStackTrace();
}
}
}else{
message = "下载失败,稿件下没有文件!";
return toDefault(req,rsq);
}
}
try {
ZipPath t = new ZipPath();
t.zip("d:\\"+tempory_path+".zip","c:\\"+tempory_path);
message = "下载成功!文件路径为:D盘下的"+tempory_path+".zip文件";
return toDefault(req,rsq);
} catch (Exception e) {
e.printStackTrace();
message = "下载失败!";
return"maganize_show.jsp";
}
}
message = "下载失败!";
return"maganize_show.jsp";
}
package com.allyroot.bjjoeocms.webapp.util;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import org.apache.tools.zip.ZipOutputStream;
import org.nestframework.commons.utils.StringUtil;
public classZipPath {
static final int BUFFER = 2048000;// 200KB
@SuppressWarnings("unchecked")
public static void unzip(StringzipFileName, String outputDirectory)
throws Exception {
org.apache.tools.zip.ZipFilezipFile = new org.apache.tools.zip.ZipFile(new File(zipFileName));
java.util.Enumeratione = zipFile.getEntries();
org.apache.tools.zip.ZipEntryz = null;
InputStreamin = null;
FileoutFile = newFile(outputDirectory);
if (!outFile.exists())
{
outFile.mkdirs();
}
while (e.hasMoreElements()) {
z= (org.apache.tools.zip.ZipEntry)e.nextElement();
if (z.isDirectory()) {
Stringname = z.getName();
name= name.substring(0, name.length() - 1);
Filef = newFile(outputDirectory + File.separator + name);
f.mkdir();
}else{
Stringname = z.getName();
int position =name.lastIndexOf("/");
if (position >= 0)
{
name= name.substring(position+1);
}
Filef = newFile(outputDirectory + File.separator
+name.toLowerCase());
f.createNewFile();
in= zipFile.getInputStream(z);
FileOutputStreamout = newFileOutputStream(f);
int count;
byte[] data =new byte[BUFFER];
while ((count = in.read(data,0,BUFFER))!= -1) {
out.write(data,0, count);
}
out.close();
}
}
in.close();
zipFile.close();
}
public void zip(String zipFileName,String inputFile) throws Exception {
if (zipFileName ==null ||zipFileName.trim().length() == 0) {
zipFileName= "test.zip";
}
zip(zipFileName,newFile(inputFile));
}
private void zip(String zipFileName,File inputFile) throwsException {
ZipOutputStreamout = newZipOutputStream(newFileOutputStream(
zipFileName));
out.setMethod(ZipOutputStream.DEFLATED);
// zip(out, inputFile, "test");//目录
zip(out,inputFile, "");
out.close();
}
private void zip(ZipOutputStreamout, File f, String base) throws Exception {
if (f.isDirectory()) {
File[]fl = f.listFiles();
if (!StringUtil.isEmpty(base))
{
out.putNextEntry(neworg.apache.tools.zip.ZipEntry(base +"/"));
}
base= base.length() == 0 ? "" : base +"/";
for (int i = 0; i < fl.length; i++) {
StringfileName = fl[i].getName();
int position =fileName.lastIndexOf(".");
//扩展名
StringextName = fileName.substring(position + 1);
if ("zip".equals(extName))
{
continue;
}
zip(out,fl[i], base + fl[i].getName());
}
}else{
out.putNextEntry(neworg.apache.tools.zip.ZipEntry(base));
FileInputStreamin = newFileInputStream(f);
int count;
byte[] data =new byte[BUFFER];
while ((count = in.read(data,0,BUFFER))!= -1) {
out.write(data,0, count);
}
// int b;
// while ((b = in.read()) != -1)
// {
// out.write(b);
// }
in.close();
}
}
}
- 文件下载第三种方式(压缩包里边还有文件夹)
- 判断文件夹下面是否还有文件(递归方式)
- 批量下载多个文件,把它们压缩成压缩包的方式下载
- 将文件包压缩下载
- Java Ext Struts1的下载文件(多重文件夹压缩)
- java实现压缩指定文件夹(文件夹包含子文件夹或文件)为zip格式压缩包
- 读取两个压缩包,比较文件夹及文件内容的不同之处并添加到另一个文件夹打包下载
- 批量下载_Zip压缩包的方式
- WEB下载指定文件(文本文件、压缩包等)
- 压缩文件夹文件到一个压缩包里面
- php 多文件下载 生成压缩包
- java下载压缩包文件zip
- 以压缩包形式下载文件
- 如何使用java压缩文件夹成为zip包(最简单的方式 已测试)
- C#压缩文件夹方式
- java后台创建压缩包&使用浏览器下载文件(批量下载实现)
- java后台创建压缩包&使用浏览器下载文件(批量下载实现)
- 多个文件和文件夹生成压缩包
- 一小时了解数据挖掘①:解析常见的大数据应用案例
- Gradel文件配置(多渠道打包)
- Appium环境的安装与配置_Python版
- ZZULIOJ 1879: 蛤玮买好了礼物
- android webkit 内核
- 文件下载第三种方式(压缩包里边还有文件夹)
- Android中深入理解 LayoutInflater.inflate()
- php要解决的问题
- RabbitMQ:安装
- 一小时了解数据挖掘②:分类算法的应用和成熟案例解析
- android 在一个应用中启动另一个应用
- Access violation reading location 0xccccccc0运行异常的解决方法
- 一小时了解数据挖掘③:详解大数据挖掘の分类技术
- Java多线程系列--“基础篇”03之 Thread中start()和run()的区别