/**
*下载多个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();
}
}
}