针对操作文件夹、压缩文件的应用实例

package com.spider.reader.business.action;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.spider.reader.business.service.ArticleService;
import com.spider.reader.business.service.PeriodService;
import com.spider.reader.common.action.BaseAction;
import com.spider.reader.common.bean.Article;
import com.spider.reader.common.bean.Period;
import com.spider.reader.common.tool.GetFileSize;
import com.spider.reader.common.tool.ZipUtil;
import com.spider.reader.common.util.Contants;
import com.spider.reader.common.util.HttpDownload;
import com.spider.reader.common.util.StringUtils;

/**
 * @ClassName: DataExportAction
 * @Description: 电信、移动数据导出
 * @author zhough
 * @date 2012-12-17 下午05:38:32
 */
public class DataExportAction extends BaseAction {
	private static final long serialVersionUID = 1L;

	protected final static Log log = LogFactory.getLog(DataExportAction.class);
	
	private String qproductid;
	
	private PeriodService periodService;
	
	private ArticleService articleService;

	/***************************** 电信数据导出 ************************************/
	public String teleDataExport() {
		log.error("teleDataExport start……");
		String jouralId = StringUtils.nvl(this.getRequest().getParameter("jouralId"));
		String issueId = StringUtils.nvl(this.getRequest().getParameter("issueId"));
		qproductid = StringUtils.nvl(jouralId);
		Period periodVo = periodService.queryPeriodById(StringUtils.nvl(issueId), Period.class);//通过id查询刊期
		if(null == periodVo){
			getRequest().setAttribute("message", "没获取到刊期相关数据!");
			return "error";
		}
		FileOutputStream out_0 = null;
		try {
			String periodName = periodVo.getProductname();
			periodName = periodName.replaceAll("<","").replaceAll(">","").replaceAll("\\|","").replaceAll("\\?","").replaceAll("\\*","").replaceAll("\\/","").replaceAll(":","");
			/** 名称:电信--中外文化交流 **/
			String mkdirPath = Contants.getProjectRealPath() + "/exportfile/d" + periodVo.getId() + "/电信--" + periodName;
			String fileTxt =  mkdirPath + "/正文.txt";
			StringBuilder contentTxt = new StringBuilder();
			File zipFile = new File(mkdirPath + ".zip");
			if(zipFile.exists()) {
				zipFile.delete();
			}
			File filepath = new File(mkdirPath);
			if (!filepath.isDirectory()) {
				filepath.mkdirs();
			}
			/** 查询刊期下的上架文章status = 1 **/
			List<Article> articleList = articleService.queryArticleList(StringUtils.nvl(issueId),"1");
			log.error("teleDataExport articleList ==" + articleList.size());
			if(null == articleList || articleList.size() == 0){
				getRequest().setAttribute("message", "没获取到刊期文章相关数据!");
				return "error";
			}
			/** 存储所有图片 * */
			List<String[]> picList = new ArrayList<String[]>();
			String strTemp = "";
			if (null != articleList && articleList.size() > 0) {
				for (int i = 0; i < articleList.size(); i++) {
					Article articleVo = (Article) articleList.get(i);
					String contentId = StringUtils.nvl(articleVo.getId()); // 当前文章ID
					String contentName = StringUtils.nvl(articleVo.getName()); // 当前文章标题
					String[] contentPic = StringUtils.getPicture(articleVo.getLargepicture(), "480"); // 480抬头大图
					picList.add(contentPic); // 抬头大图存入List中
					String columnName = StringUtils.nvl(articleVo.getColumnname()); // 栏目名称
					String articleContent = StringUtils.nvl(articleVo.getContent()); // 当前文章正文
					/****************************** 拼揍正文 ******************************/
					if(!strTemp.contains(columnName)){
						strTemp += columnName+"|";
						contentTxt.append("##"+columnName+"\n\n");
					}
					contentTxt.append("###"+contentName+"\n\n");
					contentTxt.append("  "+StringUtils.getVivaContent(articleContent));
					contentTxt.append("\n\n\n\n");
					/****************************** 组装图片copy到对应目录 ******************************/
					String[] picAs = (String[]) picList.get(i);
					String path=Contants.getUploadImgPath();	//取得服务器上的图片
					if(path.equals("/")){
						path=Contants.getProjectRealPath();
					}
					if (StringUtils.isNotEmpty(picAs[0]) && StringUtils.isNotEmpty(picAs[1]) && !picAs[1].contains(Contants.DEFAULTPIC)) {
						File fileTmp = new File(path + "/pic/bookpic/" + picAs[1]);
						log.error("teleDataExport fileTmp =="+fileTmp);
						if (fileTmp.exists()) {
							ZipUtil.copyStreamTwo(path + "/pic/bookpic/" + picAs[1], mkdirPath + "/" + columnName + "-" + contentName + ".jpg");
						}
					}
				}
				/********************* 生成txt文档 **********************/
				out_0 = new FileOutputStream(fileTxt);
        		out_0.write(contentTxt.toString().getBytes());
        		/***************** 生成zip文档 ******************/
        		mkdirPath = Contants.getProjectRealPath() + "/exportfile/d" + periodVo.getId();
				ZipUtil.zip(mkdirPath, mkdirPath + ".zip");
				GetFileSize.delFolder(mkdirPath);
				String zipPath = mkdirPath + ".zip";
				new HttpDownload().httpDownLoadUtil(this.getRequest(), this.getResponse(), zipPath);
			}
		} catch (Exception e) {
			e.printStackTrace();
			getRequest().setAttribute("message", "电信数据导出失败!");
			return "error";
		}finally{
			try {
				if(null != out_0){
					out_0.close();
				}
			} catch (IOException e) {
				e.printStackTrace();
			}

		}
		getRequest().setAttribute("message", "电信数据导出成功");
		return SUCCESS;
	}
	
	/***************************** 移动数据导出 ************************************/
	public String moveDataExport(){
		log.error("moveDataExport start……");
		String jouralId = StringUtils.nvl(this.getRequest().getParameter("jouralId"));
		String issueId = StringUtils.nvl(this.getRequest().getParameter("issueId"));
		qproductid = StringUtils.nvl(jouralId);
		Period periodVo = periodService.queryPeriodById(StringUtils.nvl(issueId), Period.class);//通过id查询刊期
		if(null == periodVo){
			getRequest().setAttribute("message", "没获取到刊期相关数据!");
			return "error";
		}
		/** 查询刊期下的上架文章status = 1 **/
		List<Article> articleList = articleService.queryArticleList(StringUtils.nvl(issueId),"1");
		log.error("moveDataExport articleList=="+articleList.size());
		if(null == articleList || articleList.size() == 0){
			getRequest().setAttribute("message", "没获取到刊期文章相关数据!");
			return "error";
		}
		FileOutputStream out_0 = null;
		try {
			String periodName = periodVo.getProductname();
			periodName = periodName.replaceAll("<","").replaceAll(">","").replaceAll("\\|","").replaceAll("\\?","").replaceAll("\\*","").replaceAll("\\/","").replaceAll(":","");
			/** 名称:移动--中外文化交流2012年第12期 **/
			String mkdirPath = Contants.getProjectRealPath() + "/exportfile/y" + periodVo.getId() + "/移动--" + periodName + periodVo.getPeriodname();
			log.error("moveDataExport mkdirPath=="+mkdirPath);
			/** 存储所有图片 * */
			List<String[]> picList = new ArrayList<String[]>();
			String fileTxt = "";
			File zipFile = new File(mkdirPath + ".zip");
			log.error("moveDataExport zipFile=="+zipFile);
			if(zipFile.exists()) {
				zipFile.delete();
			}
			File filepath = new File(mkdirPath);
			if (!filepath.isDirectory()){
				filepath.mkdirs();
			}
			if(null != articleList && articleList.size()>0){
				for(int i = 0; i < articleList.size(); i++){
					Article articleVo = (Article) articleList.get(i);
					String contentId = StringUtils.nvl(articleVo.getId()); // 当前文章ID
					String contentName = StringUtils.nvl(articleVo.getName()); // 当前文章标题
					String[] contentPic = StringUtils.getPicture(articleVo.getLargepicture(), "480"); // 480抬头大图
					picList.add(contentPic); // 抬头大图存入List中
					String articleContent = StringUtils.nvl(articleVo.getContent()); // 当前文章正文
					/****************************** 拼揍正文 ******************************/
					StringBuilder contentTxt = new StringBuilder();
					contentTxt.append(contentName+"\n\n");
					contentTxt.append("  "+StringUtils.getVivaContent(articleContent));
					contentTxt.append("\n");
//					contentTxt.append("    本文出自:《" + periodName + "》" + periodVo.getPeriodname());
					/****************************** 组装图片copy到对应目录 ******************************/
					String[] picAs = (String[]) picList.get(i);
					String path=Contants.getUploadImgPath();	//取得服务器上的图片
					if(path.equals("/")){
						path=Contants.getProjectRealPath();
					}
					if (StringUtils.isNotEmpty(picAs[0]) && StringUtils.isNotEmpty(picAs[1]) && !picAs[1].contains(Contants.DEFAULTPIC)) {
						File fileTmp = new File(path + "/pic/bookpic/" + picAs[1]);
						log.error("moveDataExport fileTmp=="+fileTmp);
						if (fileTmp.exists()) {
							ZipUtil.copyStreamTwo(path + "/pic/bookpic/" + picAs[1], mkdirPath + "/"  + String.valueOf(i+1) + contentName + ".jpg");
						}
					}
					fileTxt = mkdirPath + "/" + String.valueOf(i+1) + contentName + ".txt";
					log.error("moveDataExport fileTxt=="+fileTxt);
					/********************* 生成txt文档 **********************/
					out_0 = new FileOutputStream(fileTxt);
	        		out_0.write(contentTxt.toString().getBytes());
				}
				/***************** 生成zip文档 ******************/
				mkdirPath = Contants.getProjectRealPath() + "/exportfile/y" + periodVo.getId();
				ZipUtil.zip(mkdirPath, mkdirPath + ".zip");
				GetFileSize.delFolder(mkdirPath);
				String zipPath = mkdirPath + ".zip";
				new HttpDownload().httpDownLoadUtil(this.getRequest(), this.getResponse(), zipPath);
			}
		} catch (Exception e) {
			e.printStackTrace();
			getRequest().setAttribute("message", "移动数据导出失败!");
			return "error";
		}finally{
			try {
				if(null != out_0){
					out_0.close();
				}
			} catch (IOException e) {
				e.printStackTrace();
			}

		}
		getRequest().setAttribute("message", "移动数据导出成功");
		return SUCCESS;
	}
	
	public String getQproductid() {
		return qproductid;
	}

	public void setQproductid(String qproductid) {
		this.qproductid = qproductid;
	}
	
	public void setPeriodService(PeriodService periodService) {
		this.periodService = periodService;
	}

	public void setArticleService(ArticleService articleService) {
		this.articleService = articleService;
	}
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值