`

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

 
阅读更多
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;
	}
}

 

分享到:
评论

相关推荐

    JAVA上百实例源码以及开源项目源代码

     Java zip压缩包查看程序,应用弹出文件选择框,选择ZIP格式的压缩文件,可以像Winrar软件一样查看压缩文件内部的文件及文件夹,源码截图如上所示。 Java 数字签名、数字证书生成源码 2个目标文件 摘要:JAVA源码,...

    JAVA上百实例源码以及开源项目

     Java zip压缩包查看程序,应用弹出文件选择框,选择ZIP格式的压缩文件,可以像Winrar软件一样查看压缩文件内部的文件及文件夹,源码截图如上所示。 Java 数字签名、数字证书生成源码 2个目标文件 摘要:JAVA源码,...

    入门学习Linux常用必会60个命令实例详解doc/txt

    halt执行时,杀死应用进程,执行sync(将存于buffer中的资料强制写入硬盘中)系统调用,文件系统写操作完成后就会停止内核。若系统的运行级别为0或6,则关闭系统;否则以shutdown指令(加上-h参数)来取代。  ...

    citrix 中文管理操作手册

    限制应用程序实例 109 为已发布应用程序或桌面指定限制 110 记录连接拒绝事件 110 启用记录连接拒绝事件 111 使用终端服务配置控制连接 111 使用 TS 配置控制服务器上的连接 111 阻止用户在场维护期间进行连接 112 ...

    vijun-1.0-release.rar

    "Vijun-1.0-release.rar" 是一个压缩文件,其中包含了针对计算机专业的JSP(Java Server Pages)源代码资料集合。该资料包旨在为学习者或开发者提供一套全面、实用的JSP编程资源,帮助用户深入理解JSP技术,并能够...

    精通windows server 2008 命令行与powershell电子书PDF版(第一卷)

    《精通Windows Server 2008命令行与PowerShell》的内容简介回到顶部↑ 本书全面地介绍了windows server 2008命令行、powershell和脚本的使用,包括文件和文件夹的管理、磁盘管理、系统管理、活动目录管理、网络管理...

    精通windows server 2008 命令行与powershell 电子书PDF版(第四卷)

    《精通Windows Server 2008命令行与PowerShell》的内容简介回到顶部↑ 本书全面地介绍了windows server 2008命令行、powershell和脚本的使用,包括文件和文件夹的管理、磁盘管理、系统管理、活动目录管理、网络管理...

    精通windows server 2008 命令行与powershell电子书PDF版(第三卷)

    《精通Windows Server 2008命令行与PowerShell》的内容简介回到顶部↑ 本书全面地介绍了windows server 2008命令行、powershell和脚本的使用,包括文件和文件夹的管理、磁盘管理、系统管理、活动目录管理、网络管理...

    精通windows server 2008 命令行与powershell电子书PDF版(第二卷)

    《精通Windows Server 2008命令行与PowerShell》的内容简介回到顶部↑ 本书全面地介绍了windows server 2008命令行、powershell和脚本的使用,包括文件和文件夹的管理、磁盘管理、系统管理、活动目录管理、网络管理...

    Access+2000中文版高级编程

    10.5.2 在多重选择列表框中使用VBA来操作选中项 285 10.6 使用子窗体、子报表向导 291 10.7 对控件提供电子表格的光标移动方式 292 10.7.1 研究问题 292 10.7.2 解决问题 293 10.8 用代码操纵控件 297 ...

    Access 2000中文版高级编程(part1)

    10.5.2 在多重选择列表框中使用VBA来操作选中项 285 10.6 使用子窗体、子报表向导 291 10.7 对控件提供电子表格的光标移动方式 292 10.7.1 研究问题 292 10.7.2 解决问题 293 10.8 用代码操纵控件 297 10.8.1 ...

    sphinx中文语音训练手册

    2)解压Sphinx中除sphinx4-1.0beta6-bin.zip外的压缩文件到d:\sphinxtrain下 3)安装audacity-win-2.0.3rc1.zip和FairStars.zip 4)安装UltraEdit-32.rar(绿色,解压即可) 5.2 准备文本语料 这里需要使用我们提供的...

    史上最强-Kettle-培训教程.pdf

    Kettle可以在 http://kettle.pentaho.org/网站下载 下载kettle压缩包,因kettle为绿色软 件,解压缩到任意本地路径即可 Kettle 运行步骤 双击运行 kettle 文件夹下的 Kettle 文件,出 现 kettle 欢迎界面。...

    新版Android开发教程.rar

    Android 是一个专门针对移动设备的软件集,它包括一个操作系统,中间件和一些重要的应用程序。 Beta 版 的 Android SDK 提供了在 Android 平台上使用 JaVa 语言进行 Android 应用开发必须的工具和 API 接口。 特性 ...

    亮剑.NET深入体验与实战精要2

    9.3 文件夹目录操作 375 9.4 读写INI文件 376 9.5 读写注册表 379 本章常见技术面试题 381 常见面试技巧之经典问题巧回答 381 本章小结 382 第10章 网络应用开发 383 10.1 Socket基本编程 384 10.1.1 Socket基本知识...

    亮剑.NET深入体验与实战精要3

    9.3 文件夹目录操作 375 9.4 读写INI文件 376 9.5 读写注册表 379 本章常见技术面试题 381 常见面试技巧之经典问题巧回答 381 本章小结 382 第10章 网络应用开发 383 10.1 Socket基本编程 384 10.1.1 Socket基本知识...

    ASP.NET4高级程序设计第4版 带目录PDF 分卷压缩包 part1

    16.4.6 通过配置文件应用母版页 16.5 高级母版页 16.5.1 和母版页类交互 16.5.2 动态设置母版页 16.5.3 嵌套母版页 16.6 总结 第17章 网站导航 17.1 多视图页面 17.1.1 MultiView控件 17.1.2 ...

    BB1407openwrt-RG100A_DB120-squashfs-cfe.bin

    绑定完设备后就会显示“Get device token success"的信息,并开始同步操作了,如果已经设置了同步文件夹的话。 4、绑定完设备后,先来测试下你的设备上是否能支持中文,执行命令(perl版几乎可以跳过此步): ...

Global site tag (gtag.js) - Google Analytics