java文件打包下载

package com.abchina.swdx.os.contactdept.web.action;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts2.ServletActionContext;
import org.apache.tools.zip.ZipEntry;
import org.apache.tools.zip.ZipOutputStream;

import com.abchina.swdx.os.contactdept.service.IInfocService;
import com.abchina.swdx.os.contactdept.service.IInfocsbryService;
import com.abchina.swdx.os.contactdept.web.vo.InfocVO;
import com.abchina.swdx.os.contactdept.web.vo.InfocsbryVO;
import com.abchina.swdx.sysset.comm.web.vo.SysSetVO;

public class WjxzAction {
	
	private IInfocService infocService;
	
	private IInfocsbryService infocsbryService;
	
	private SysSetVO sysSetVO;
	
	private InfocVO infocVO;
	
	private InfocsbryVO infocsbryVO;
	
	private List<InfocsbryVO> infocsbryVOList = null;
	
	public List<InfocsbryVO> getInfocsbryVOList() {
		return infocsbryVOList;
	}

	public void setInfocsbryVOList(List<InfocsbryVO> infocsbryVOList) {
		this.infocsbryVOList = infocsbryVOList;
	}

	public IInfocService getInfocService() {
		return infocService;
	}

	public void setInfocService(IInfocService infocService) {
		this.infocService = infocService;
	}

	public IInfocsbryService getInfocsbryService() {
		return infocsbryService;
	}

	public void setInfocsbryService(IInfocsbryService infocsbryService) {
		this.infocsbryService = infocsbryService;
	}

	public SysSetVO getSysSetVO() {
		return sysSetVO;
	}

	public void setSysSetVO(SysSetVO sysSetVO) {
		this.sysSetVO = sysSetVO;
	}

	public InfocVO getInfocVO() {
		return infocVO;
	}

	public void setInfocVO(InfocVO infocVO) {
		this.infocVO = infocVO;
	}

	public InfocsbryVO getInfocsbryVO() {
		return infocsbryVO;
	}

	public void setInfocsbryVO(InfocsbryVO infocsbryVO) {
		this.infocsbryVO = infocsbryVO;
	}

	/***
	 * 下载上报附件
	 */
	public void getInofcsbryfiles(){
		HttpServletResponse response  = ServletActionContext.getResponse();
    	HttpServletRequest request = ServletActionContext.getRequest();
    	response.setCharacterEncoding("UTF-8");
    	response.setContentType("text/html;charest=UTF-8");
    	String bdpath = request.getSession().getServletContext().getRealPath("/"); //项目本地路径
    	if(null==infocsbryVOList){
    		infocsbryVOList = new ArrayList<InfocsbryVO>();
    	}
    	infocsbryVO.setFjname("1");
    	infocsbryVOList=infocsbryService.findInfocsbryByWhereSB(infocsbryVO);
    	if(null==infocVO){
    		infocVO = new InfocVO();
    	}
    	infocVO.setId(infocsbryVO.getInfocid());
    	infocVO = this.infocService.findInfocByKey(infocVO);
    	String title = infocVO.getTitle();
    	try {
	    	File filexxsb = new File(bdpath+"/"+title);//根目录+title
	    	if(!filexxsb.exists()){
	    		filexxsb.mkdirs();
	    	}
			for(int j=0;j<infocsbryVOList.size();j++){
				String orgname = infocsbryVOList.get(j).getOrgname().toString();
				orgname=orgname.replaceAll("/", "-");
				File filesbFolder = new File(filexxsb+"/"+orgname+"("+infocsbryVOList.get(j).getName().toString()+")");
				if(!filesbFolder.exists()){
					filesbFolder.mkdirs();
				}
				//将班级活动文件拷贝到新的目录中   //注意 如果是用控件上传方法需要加"/uploads/"
				//String pp =  filesbFolder+"/"+infocsbryVOList.get(j).getFjname().toString();
				//System.out.println(pp+"!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
				copyFile(bdpath+"/uploads/"+infocsbryVOList.get(j).getFjpath().toString(),filesbFolder+"/"+infocsbryVOList.get(j).getFjname().toString());
			}
			WjxzAction book = new WjxzAction();
			book.zip(bdpath+"/"+title,title);
//			//删除创建的临时文件夹
	        WjxzAction wjgl = new WjxzAction();
	        wjgl.DeleteFolder(bdpath+"/"+title);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * 复制单个文件
	 * @param oldPath
	 * @param newPath
	 */
	public void copyFile(String oldPath, String newPath) { 
	       try { 
	           int bytesum = 0; 
	           int byteread = 0; 
	           File oldfile = new File(oldPath); 
	           if (oldfile.exists()) { //文件存在时 
	               InputStream inStream = new FileInputStream(oldPath); //读入原文件 
	               FileOutputStream fs = new FileOutputStream(newPath); 
	               byte[] buffer = new byte[1444]; 
	               while ( (byteread = inStream.read(buffer)) != -1) { 
	                   bytesum += byteread; //字节数 文件大小 
	                   fs.write(buffer, 0, byteread); 
	               } 
	               fs.close();
	               inStream.close(); 
	           } 
	       } 
	       catch (Exception e) { 
	           System.out.println("复制单个文件操作出错"); 
	           e.printStackTrace(); 
	       } 
	   } 
	
	/**   
     *    
     * @param inputFileName 输入一个文件夹   
     * @param zipFileName   输出一个压缩文件夹,打包后文件名字   
     * @throws Exception   
     */    
    public void zip(String inputFileName, String zipFileName) throws Exception {     
        zip(zipFileName, new File(inputFileName));     
    }     
    
    private void zip(String zipFileName, File inputFile) throws Exception {     
    	HttpServletResponse response  = ServletActionContext.getResponse();
    	response.setContentType("APPLICATION/OCTET-STREAM");
		response.setHeader("Content-Disposition", "attachment; filename="
				+new String(zipFileName.getBytes("gb2312"),"iso8859-1") + ".zip");
		ZipOutputStream out = new ZipOutputStream(response.getOutputStream());
        zip(out, inputFile, ""); 
        out.setEncoding("GB2312");
        out.flush();
        out.close();   
    }     
    
    private void zip(ZipOutputStream out, File f, String base) throws Exception {
        if (f.isDirectory()) {//判断是否为目录     
            File[] fl = f.listFiles();     
//            out.putNextEntry(new org.apache.tools.zip.ZipEntry(base + "/"));     
            out.putNextEntry(new ZipEntry(base + "/"));
            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 org.apache.tools.zip.ZipEntry(base));     
            out.putNextEntry(new ZipEntry(base));     
            FileInputStream in = new FileInputStream(f);     
            int b;     
            while((b = in.read()) != -1){     
            	out.write(b);
            }     
            in.close();
        }     
    }  
    
    
    /** 
     *  根据路径删除指定的目录或文件,无论存在与否 
     *@param sPath  要删除的目录或文件 
     *@return 删除成功返回 true,否则返回 false。 
     */  
    public void DeleteFolder(String sPath) {  
        File file = new File(sPath);  
        // 判断目录或文件是否存在  
        if (file.exists()) {  
            // 判断是否为文件  
            if (file.isFile()) {  // 为文件时调用删除文件方法  
                deleteFile(sPath);  
            } else {  // 为目录时调用删除目录方法  
                deleteDirectory(sPath);  
            }  
        }  
    }
    
    /** 
     * 删除单个文件 
     * @param   sPath    被删除文件的文件名 
     * @return 单个文件删除成功返回true,否则返回false 
     */  
    public void deleteFile(String sPath) {  
        File file = new File(sPath);  
        // 路径为文件且不为空则进行删除  
        if (file.isFile() && file.exists()) {  
            file.delete();
        }  
    }
    
    /** 
     * 删除目录(文件夹)以及目录下的文件 
     * @param   sPath 被删除目录的文件路径 
     * @return  目录删除成功返回true,否则返回false 
     */  
    public boolean deleteDirectory(String sPath) {  
        //如果sPath不以文件分隔符结尾,自动添加文件分隔符  
        if (!sPath.endsWith(File.separator)) {  
            sPath = sPath + File.separator;  
        }  
        File dirFile = new File(sPath);  
        //如果dir对应的文件不存在,或者不是一个目录,则退出  
        if (!dirFile.exists() || !dirFile.isDirectory()) {  
            return false;  
        }  
        //删除文件夹下的所有文件(包括子目录)  
        File[] files = dirFile.listFiles();  
        for (int i = 0; i < files.length; i++) {  
            //删除子文件  
            if (files[i].isFile()) {  
                deleteFile(files[i].getAbsolutePath());  
            } //删除子目录  
            else {  
                deleteDirectory(files[i].getAbsolutePath());  
            }  
        }  
        //删除当前目录  
        if (dirFile.delete()) {  
            return true;  
        } else {  
            return false;  
        }  
    } 
	
}

 

猜你喜欢

转载自tom-tomcat.iteye.com/blog/2218644
今日推荐