照片打包下载_JAVA_编程开发_程序员俱乐部

中国优秀的程序员网站程序员频道CXYCLUB技术地图
热搜:
更多>>
 
您所在的位置: 程序员俱乐部 > 编程开发 > JAVA > 照片打包下载

照片打包下载

 2014/5/22 12:57:28  duanfei  程序员俱乐部  我要评论(0)
  • 摘要:HOME_PATH=D\:\\soft\\Tomcat6\\virtualdir\\photoDOWNPHOTO_PATH=/photozipDOWN_TYPE_ZIP=/zip/packagecom.jsict.httptransfer.comm;importjava.io.File;importjava.io.FileInputStream;importjava.io.FileOutputStream;importjava.io.IOException;importjava.net
  • 标签:下载
class="properties">
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.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.jsict.ictmap.common.PropertiesManager;
import com.jsict.ictmap.web.UserSession;
import com.jsict.ictmapmodule.custom.entiry.AttachmentBean;
import com.jsict.util.StringUtil;

/**
 * 打包下载图片
 */
public class DownPhotoUtil {
	protected final Log logger = LogFactory.getLog(getClass());
	public String zipPath = "";	//压缩路径
	public String zipFile = "";	//压缩文件
	public String downLoad = "";	//下载路径
	
	/**
	 * 下载图片
	 * @param userSession	用户session
	 * @param list			AttachmentBeanList
	 * @param zipTitle		zip名称
	 * @return
	 * @throws Exception
	 */
	public String downPhoto(UserSession userSession,List list,String zipTitle) throws Exception{
		String photoPath = PropertiesManager.getProperty("common.properties", "HOME_PATH");
		createFile(userSession,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;	//压缩文件路径
		deleteFile(downPhotoPath);
		zip(zipPath, downPhotoPath);
deleteFile(zipPath);
		return downLoad + "/" + zipTitle;
	}
	
	/**
	 * 保存文件
	 * @param userSession	用户session
	 * @param photoPath		图片绝对路径
	 * @throws IOException
	 */
	public void createFile(UserSession userSession,String photoPath) throws IOException{
		String savePath =  "";
		
		//年月日
        String saveDate=DateTools.parseDate2Str(new Date(), "yyyyMMdd");
        savePath = savePath + "/"  + saveDate;
        
        if(!StringUtil.isEmpty(userSession.getAreaId())){	//区码ID
        	savePath = "/" + userSession.getAreaId();
        }
        if (userSession.getIndependentDept() != null && 
        		!StringUtil.isEmpty(userSession.getIndependentDept().getDepartmentId())){			//企业ID
        	if(!StringUtil.isEmpty(savePath)){
        		savePath = savePath + "/" + userSession.getIndependentDept().getDepartmentId();
        	}
        	else
        	{
        		savePath = "/" + userSession.getIndependentDept().getDepartmentId();
        	}
        }
        if(userSession.getUserId() != null ){
        	savePath = savePath + "/" + userSession.getUserId();
        }
        
        int ronNum = (int)(Math.random()*100000);
        savePath = savePath + "/"  + ronNum;
        zipFile = photoPath + PropertiesManager.getProperty("common.properties", "DOWNPHOTO_PATH")
        		+savePath;
        zipPath = zipFile +PropertiesManager.getProperty("common.properties", "DOWN_TYPE_ZIP");
        downLoad = PropertiesManager.getProperty("common.properties", "DOWNPHOTO_PATH")
        		+savePath;
        deleteFile(zipPath);	//
		File file = new File(zipPath);
		if (!file.exists()) {
			file.mkdirs();
		}
	}
	
	/**
	 * 
	 * 方法说明:删除指定文件路径下的文件
	 * 
	 * @param targetPath
	 *            :目标文件路径
	 * @return 返回值类型
	 * @Exception 异常对象
	 * 
	 */
	public static 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 static void zip(String inputFileName, String zipFileName) throws Exception {
		zip(zipFileName, new File(inputFileName));
	}

	public static void zip(String zipFileName, File inputFile) throws Exception {
		ZipOutputStream out = new ZipOutputStream(new FileOutputStream(zipFileName));
		zip(out, inputFile, "");
		out.close();
	}

	public static void zip(ZipOutputStream out, File file, String base) throws Exception {
		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 url
	 *            远程文件路径
	 * @param clientPath
	 *            本地路径
	 * @return
	 */
	public static void copyFileToLocal(String fileFrom, String fileTo) {
		try {
			if(fileTo.contains("jpg")){
				
			}else{
				
				//有问题,暂时还原
				fileTo=fileTo+".jpg";
			}
			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 zipTitle = "photo.zip";
		DownPhotoUtil dp= new DownPhotoUtil();
		try {
			downPhotoPath = "图片下载失败";
			if (list != null && list.size() > 0) {
				downPhotoPath = dp.downPhoto(this.getUserSession(), list, zipTitle);
			}
			logger.info("压缩包下载路径:" + downPhotoPath);
		} catch (Exception e) {
			downPhotoPath = "图片下载失败";
		}
		return "downPhoto";
	}

  • ant.jar (1.4 MB)
  • 下载次数: 0
发表评论
用户名: 匿名