通过业务表中照片编号获得需要下载的照片列表
把用户session、照片列表与zip名称 传入照片下载公共方法中。获得下载路径。
下载地址设计:
/photozip/时间(yyyyMMdd)/HHmmss_5位随机码zipType
HOME_PATH=D\:\\soft\\Tomcat6\\virtualdir\\photo DOWNPHOTO_PATH=/photozip DOWN_TYPE_ZIP=/zip/
package com.jsict.httptransfer.comm; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.net.MalformedURLException; import java.util.Date; import java.util.List; import org.apache.commons.io.FileUtils; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.apache.tools.zip.ZipEntry; import org.apache.tools.zip.ZipOutputStream; import com.jshx.ictjs.httptransfer.utils.DateTools; import com.jsict.ictmap.common.PropertiesManager; import com.jsict.ictmap.util.StringUtil; import com.jsict.ictmapmodule.custom.entiry.AttachmentBean; /** * 打包下载图片 * @author duanpf */ public class DownPhotoUtil { protected final Log logger = LogFactory.getLog(getClass()); public String zipPath = ""; //压缩路径+文件 public String zipFile = ""; //压缩路径 public String downLoad = ""; //下载路径 /** * 下载图片 * @param list AttachmentBeanList * @param zipType .zip * @return 图片下载地址 * @throws Exception */ public String downPhoto(List list,String zipType) throws IOException{ if(StringUtil.isEmpty(zipType)){ zipType = ".zip"; } String photoPath = PropertiesManager.getProperty("common.properties", "HOME_PATH"); String zipTitle = createFile(photoPath); for (int i = 0; i < list.size(); i++) { AttachmentBean photo = (AttachmentBean) list.get(i); String path = photoPath + photo.getFilePath(); logger.info("=============path["+i+"]="+path); copyFileToLocal(path, zipPath + "/" + photo.getName()); } // 判断目录是否存在,不存在创建 logger.info("压缩包存放路径:" + zipPath + "\n"); String downPhotoPath = zipFile + "/" + zipTitle + zipType; //压缩文件路径 zip(zipPath, downPhotoPath); //deleteFile(zipPath); return downLoad + "/" + zipTitle + zipType; } /** * 保存文件 * @param userSession 用户session * @param photoPath 图片绝对路径 * @return * @throws IOException */ public String createFile(String photoPath) throws IOException{ String savePath = ""; //年月日 String saveDate=DateTools.parseDate2Str(new Date(), "yyyyMMdd"); savePath = savePath + "/" + saveDate; String fileDate=DateTools.parseDate2Str(new Date(), "HHmmss"); int ronNum = (int)(Math.random()*100000); downLoad = PropertiesManager.getProperty("common.properties", "DOWNPHOTO_PATH") +savePath; zipFile = photoPath + downLoad; zipPath = zipFile + "/" + (fileDate + "_" + ronNum); File file = new File(zipPath); if (!file.exists()) { file.mkdirs(); } return fileDate + "_" + ronNum; } /** * * 方法说明:删除指定文件路径下的文件 * * @param targetPath * :目标文件路径 * @return 返回值类型 * @Exception 异常对象 * */ public void deleteFile(String targetPath) throws IOException { File targetFile = new File(targetPath); if (targetFile.isDirectory()) { FileUtils.deleteDirectory(targetFile); } else if (targetFile.isFile()) { targetFile.delete(); } } /** * 把inputFileName下的文件压缩到zipFileName对应路径 * * @param inputFileName * 需压缩的文件(夹)路径 * @param zipFileName * 压缩后生成压缩文件名 * @throws Exception */ public void zip(String inputFileName, String zipFileName) throws IOException { zip(zipFileName, new File(inputFileName)); } public void zip(String zipFileName, File inputFile) throws IOException { ZipOutputStream out = new ZipOutputStream(new FileOutputStream(zipFileName)); zip(out, inputFile, ""); out.close(); } public void zip(ZipOutputStream out, File file, String base) throws IOException { if (file.isDirectory()) { File[] fl = file.listFiles(); out.putNextEntry(new ZipEntry(base + "/")); out.setEncoding("gbk"); base = base.length() == 0 ? "" : base + "/"; for (int i = 0; i < fl.length; i++) { zip(out, fl[i], base + fl[i].getName()); } } else { out.putNextEntry(new ZipEntry(base)); out.setEncoding("gbk"); FileInputStream in = new FileInputStream(file); int b; while ((b = in.read()) != -1) { out.write(b); } in.close(); } } /** * 将文件拷到clientPath指定文件夹下 * * @param fileFrom * 远程文件路径 * @param fileTo * 本地路径 * @return */ public void copyFileToLocal(String fileFrom, String fileTo) { try { if (fileTo.indexOf('.')<0){ //不包含 String[] fileFromSplit = fileFrom.split("\\."); String fileFromSplitStr = fileFromSplit[fileFromSplit.length-1]; fileTo=fileTo+"."+fileFromSplitStr; } FileInputStream in = new FileInputStream(fileFrom); FileOutputStream out = new FileOutputStream(fileTo); byte[] bt = new byte[1024]; int count; while ((count = in.read(bt)) > 0) { out.write(bt, 0, count); } in.close(); out.close(); } catch (MalformedURLException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } }
业务调用例子:
public String downPhoto()throws Exception { List<AttachmentBean> list = null; String zipType = ".zip"; DownPhotoUtil dp= new DownPhotoUtil(); try { downPhotoPath = "图片下载失败"; if (list != null && list.size() > 0) { downPhotoPath = dp.downPhoto(list, zipType); } logger.info("压缩包下载路径:" + downPhotoPath); } catch (Exception e) { downPhotoPath = "图片下载失败"; } return "downPhoto"; }