导出PDF和Zip文件的工具类

一、导出pdf文件,使用itext框架:
1、引入pom文件:

<dependency>
			<groupId>com.itextpdf</groupId>
			<artifactId>itextpdf</artifactId>
			<version>5.2.0</version>
		</dependency>

		<dependency>
			<groupId>com.itextpdf</groupId>
			<artifactId>itext-asian</artifactId>
			<version>5.2.0</version>
		</dependency>

2、导出pdf文件:

package com.newtouch.pdf;

import java.io.File;
import java.io.FileOutputStream;

import javax.servlet.http.HttpServletRequest;

import com.itextpdf.text.Document;
import com.itextpdf.text.Element;
import com.itextpdf.text.Font;
import com.itextpdf.text.Image;
import com.itextpdf.text.PageSize;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;
import com.newtouch.common.utils.DateUtils;
import com.newtouch.modules.mba.entity.ApplyOnline;

/**
 * 描述:【JAVA生成PDF】
 * 
 * 
 * @title GeneratePDF
 * @author SYJ
 * @email songyanjun_stars@126.com
 * @date 2013-4-6
 * @version V1.0
 */
public class GeneratePDF {

	/**
	 * 添加含有章节的pdf文件
	 * 
	 * @return
	 * 
	 * @throws Exception
	 */
	public static File writeCharpter(ApplyOnline applyOnline, String realPathPrefix) throws Exception {

		BaseFont bfChinese = BaseFont.createFont(realPathPrefix + "ttf/MSYH.TTF", BaseFont.IDENTITY_H,
				BaseFont.NOT_EMBEDDED);
		Font FontChinese = new Font(bfChinese);
		Font title = new Font(bfChinese, 15, Font.BOLD);
		Font cellTitle = new Font(bfChinese, 11, Font.NORMAL);
		Font cellContent = new Font(bfChinese, 11, Font.NORMAL);
		// 新建document对象 第一个参数是页面大小。接下来的参数分别是左、右、上和下页边距。
		Document document = new Document(PageSize.A4, 20, 20, 15, 15);

		// 建立一个书写器(Writer)与document对象关联,通过书写器(Writer)可以将文档写入到磁盘中。
		String _fileName = applyOnline.getName() + applyOnline.getIdentityCard() + ".pdf";
		PdfWriter.getInstance(document, new FileOutputStream(realPathPrefix + "pdf/" + _fileName));

		// 打开文件
		document.open();

		// 标题
		document.addTitle(applyOnline.getProject() + "提前批面试报名表");

		// 作者
		//document.addAuthor("西工大管理学院" + applyOnline.getProject());

		// 主题
		// document.addSubject("This example explains how to add metadata.");
		// document.addKeywords("iText, Hello mingri");
		// document.addCreator("My program using iText");

		// document.newPage();
		// 向文档中添加内容

		float[] widths = { 20f, 18f, 20f, 25f, 17f };
		PdfPTable table = new PdfPTable(widths);
		table.setSpacingBefore(20f);
		table.setWidthPercentage(100);// 设置表格宽度为100%

		PdfPCell cell = new PdfPCell(); // 基本单元格

		/* 头部logo */
		Image img = Image.getInstance(realPathPrefix + "image/wnpu_logo.jpg");
		cell = new PdfPCell(img);
		cell.setUseAscender(true);
		cell.setUseDescender(true);
		cell.setFixedHeight(55);
		cell.setVerticalAlignment(Element.ALIGN_MIDDLE); // 基本单元格上下居中
		cell.setHorizontalAlignment(Element.ALIGN_CENTER); // 基本单元格左右居中
		cell.setColspan(5);
		table.addCell(cell);

		/* 标题 */
		String project = new String();
		if("mba".equals(applyOnline.getProject())){
			project = "MBA";
		}
		if("emba".equals(applyOnline.getProject())){
			project = "EMBA";
		}
		if("mem&me".equals(applyOnline.getProject())){
			project = "MEM";
		}
		if("edp".equals(applyOnline.getProject())){
			project = "EDP";
		}
		Paragraph pragraph1 = new Paragraph(project + "提前批面试报名表", title);
		cell = new PdfPCell(pragraph1);
		cell.setFixedHeight(30);
		cell.setUseAscender(true);
		cell.setUseDescender(true);
		cell.setVerticalAlignment(Element.ALIGN_MIDDLE); // 基本单元格上下居中
		cell.setHorizontalAlignment(Element.ALIGN_CENTER); // 基本单元格左右居中
		cell.setColspan(5);
		table.addCell(cell);

		/*Paragraph pragraph2 = new Paragraph(" ", FontChinese);
		cell = new PdfPCell(pragraph2);
		cell.setColspan(3);
		table.addCell(cell);*/

		/* 表格内容 */
		Paragraph tab1 = new Paragraph("考生姓名", cellTitle);
		cell = new PdfPCell(tab1);
		cell.setVerticalAlignment(Element.ALIGN_MIDDLE); // 基本单元格上下居中
		cell.setHorizontalAlignment(Element.ALIGN_CENTER); // 基本单元格左右居中
		cell.setFixedHeight(35);
		table.addCell(cell);
		Paragraph pragraph3 = new Paragraph(applyOnline.getName(), cellContent);
		cell = new PdfPCell(pragraph3);
		cell.setVerticalAlignment(Element.ALIGN_MIDDLE); // 基本单元格上下居中
		cell.setHorizontalAlignment(Element.ALIGN_CENTER); // 基本单元格左右居中
		cell.setFixedHeight(35);
		table.addCell(cell);

		Paragraph tab2 = new Paragraph("考生性别", cellTitle);
		cell = new PdfPCell(tab2);
		cell.setVerticalAlignment(Element.ALIGN_MIDDLE); // 基本单元格上下居中
		cell.setHorizontalAlignment(Element.ALIGN_CENTER); // 基本单元格左右居中
		cell.setFixedHeight(35);
		table.addCell(cell);
		Paragraph pragraph4 = new Paragraph(applyOnline.getSex(), cellContent);
		cell = new PdfPCell(pragraph4);
		cell.setVerticalAlignment(Element.ALIGN_MIDDLE); // 基本单元格上下居中
		cell.setHorizontalAlignment(Element.ALIGN_CENTER); // 基本单元格左右居中
		cell.setFixedHeight(35);
		table.addCell(cell);

		/* 照片 */
		//Image photo = Image.getInstance(realPathPrefix + applyOnline.getPhoto());
		Paragraph photo = new Paragraph("照片黏贴处", cellTitle);
		cell = new PdfPCell(photo);
		cell.setUseAscender(true);
		cell.setUseDescender(true);
		cell.setRowspan(4);
		cell.setVerticalAlignment(Element.ALIGN_MIDDLE); // 基本单元格上下居中
		cell.setHorizontalAlignment(Element.ALIGN_CENTER); // 基本单元格左右居中
		table.addCell(cell);

		Paragraph tab3 = new Paragraph("考生民族", cellTitle);
		cell = new PdfPCell(tab3);
		cell.setVerticalAlignment(Element.ALIGN_MIDDLE); // 基本单元格上下居中
		cell.setHorizontalAlignment(Element.ALIGN_CENTER); // 基本单元格左右居中
		cell.setFixedHeight(35);
		table.addCell(cell);
		Paragraph pragraph5 = new Paragraph(applyOnline.getNation(), cellContent);
		cell = new PdfPCell(pragraph5);
		cell.setVerticalAlignment(Element.ALIGN_MIDDLE); // 基本单元格上下居中
		cell.setHorizontalAlignment(Element.ALIGN_CENTER); // 基本单元格左右居中
		cell.setFixedHeight(35);
		table.addCell(cell);

		Paragraph tab4 = new Paragraph("政治面貌", cellTitle);
		cell = new PdfPCell(tab4);
		cell.setVerticalAlignment(Element.ALIGN_MIDDLE); // 基本单元格上下居中
		cell.setHorizontalAlignment(Element.ALIGN_CENTER); // 基本单元格左右居中
		cell.setFixedHeight(35);
		table.addCell(cell);
		Paragraph pragraph6 = new Paragraph(applyOnline.getPoliticsStatus(), cellContent);
		cell = new PdfPCell(pragraph6);
		cell.setVerticalAlignment(Element.ALIGN_MIDDLE); // 基本单元格上下居中
		cell.setHorizontalAlignment(Element.ALIGN_CENTER); // 基本单元格左右居中
		cell.setFixedHeight(35);
		table.addCell(cell);

		Paragraph tab5 = new Paragraph("出生年月", cellTitle);
		cell = new PdfPCell(tab5);
		cell.setVerticalAlignment(Element.ALIGN_MIDDLE); // 基本单元格上下居中
		cell.setHorizontalAlignment(Element.ALIGN_CENTER); // 基本单元格左右居中
		cell.setFixedHeight(35);
		table.addCell(cell);
		String birthdy = DateUtils.formatDate(applyOnline.getBirthday(), DateUtils.YYYY_MM_DD);
		Paragraph pragraph7 = new Paragraph(birthdy, cellContent);
		cell = new PdfPCell(pragraph7);
		cell.setVerticalAlignment(Element.ALIGN_MIDDLE); // 基本单元格上下居中
		cell.setHorizontalAlignment(Element.ALIGN_CENTER); // 基本单元格左右居中
		cell.setFixedHeight(35);
		table.addCell(cell);

		Paragraph tab6 = new Paragraph("身份证号\n护照号", cellTitle);
		cell = new PdfPCell(tab6);
		cell.setVerticalAlignment(Element.ALIGN_MIDDLE); // 基本单元格上下居中
		cell.setHorizontalAlignment(Element.ALIGN_CENTER); // 基本单元格左右居中
		cell.setFixedHeight(35);
		table.addCell(cell);
		Paragraph pragraph8 = new Paragraph(applyOnline.getIdentityCard(), cellContent);
		cell = new PdfPCell(pragraph8);
		cell.setVerticalAlignment(Element.ALIGN_MIDDLE); // 基本单元格上下居中
		cell.setHorizontalAlignment(Element.ALIGN_CENTER); // 基本单元格左右居中
		cell.setFixedHeight(35);
		table.addCell(cell);
		
		Paragraph tab8 = new Paragraph("邮政编码", cellTitle);
		cell = new PdfPCell(tab8);
		cell.setVerticalAlignment(Element.ALIGN_MIDDLE); // 基本单元格上下居中
		cell.setHorizontalAlignment(Element.ALIGN_CENTER); // 基本单元格左右居中
		cell.setFixedHeight(35);
		table.addCell(cell);
		Paragraph pragraph10 = new Paragraph(applyOnline.getPostcode(), cellContent);
		cell = new PdfPCell(pragraph10);
		cell.setVerticalAlignment(Element.ALIGN_MIDDLE); // 基本单元格上下居中
		cell.setHorizontalAlignment(Element.ALIGN_CENTER); // 基本单元格左右居中
		cell.setFixedHeight(35);
		table.addCell(cell);

		Paragraph tab9 = new Paragraph("电子信箱", cellTitle);
		cell = new PdfPCell(tab9);
		cell.setVerticalAlignment(Element.ALIGN_MIDDLE); // 基本单元格上下居中
		cell.setHorizontalAlignment(Element.ALIGN_CENTER); // 基本单元格左右居中
		cell.setFixedHeight(35);
		table.addCell(cell);
		Paragraph pragraph11 = new Paragraph(applyOnline.getEmail(), cellContent);
		cell = new PdfPCell(pragraph11);
		cell.setVerticalAlignment(Element.ALIGN_MIDDLE); // 基本单元格上下居中
		cell.setHorizontalAlignment(Element.ALIGN_CENTER); // 基本单元格左右居中
		cell.setFixedHeight(35);
		table.addCell(cell);

		Paragraph tab7 = new Paragraph("通讯地址", cellTitle);
		cell = new PdfPCell(tab7);
		cell.setVerticalAlignment(Element.ALIGN_MIDDLE); // 基本单元格上下居中
		cell.setHorizontalAlignment(Element.ALIGN_CENTER); // 基本单元格左右居中
		cell.setFixedHeight(35);
		table.addCell(cell);
		Paragraph pragraph9 = new Paragraph(applyOnline.getAddress(), cellContent);
		cell = new PdfPCell(pragraph9);
		cell.setVerticalAlignment(Element.ALIGN_MIDDLE); // 基本单元格上下居中
		cell.setHorizontalAlignment(Element.ALIGN_CENTER); // 基本单元格左右居中
		cell.setFixedHeight(35);
		cell.setColspan(4);
		table.addCell(cell);

		/*Paragraph tab8 = new Paragraph("邮政编码", cellTitle);
		cell = new PdfPCell(tab8);
		cell.setVerticalAlignment(Element.ALIGN_MIDDLE); // 基本单元格上下居中
		cell.setHorizontalAlignment(Element.ALIGN_CENTER); // 基本单元格左右居中
		cell.setFixedHeight(35);
		table.addCell(cell);
		Paragraph pragraph10 = new Paragraph(applyOnline.getPostcode(), cellContent);
		cell = new PdfPCell(pragraph10);
		cell.setVerticalAlignment(Element.ALIGN_MIDDLE); // 基本单元格上下居中
		cell.setHorizontalAlignment(Element.ALIGN_CENTER); // 基本单元格左右居中
		cell.setFixedHeight(35);
		table.addCell(cell);

		Paragraph tab9 = new Paragraph("电子信箱", cellTitle);
		cell = new PdfPCell(tab9);
		cell.setVerticalAlignment(Element.ALIGN_MIDDLE); // 基本单元格上下居中
		cell.setHorizontalAlignment(Element.ALIGN_CENTER); // 基本单元格左右居中
		cell.setFixedHeight(35);
		table.addCell(cell);
		Paragraph pragraph11 = new Paragraph(applyOnline.getEmail(), cellContent);
		cell = new PdfPCell(pragraph11);
		cell.setVerticalAlignment(Element.ALIGN_MIDDLE); // 基本单元格上下居中
		cell.setHorizontalAlignment(Element.ALIGN_CENTER); // 基本单元格左右居中
		cell.setFixedHeight(35);
		cell.setColspan(2);
		table.addCell(cell);*/

		Paragraph tab10 = new Paragraph("移动电话", cellTitle);
		cell = new PdfPCell(tab10);
		cell.setVerticalAlignment(Element.ALIGN_MIDDLE); // 基本单元格上下居中
		cell.setHorizontalAlignment(Element.ALIGN_CENTER); // 基本单元格左右居中
		cell.setFixedHeight(35);
		table.addCell(cell);
		Paragraph pragraph12 = new Paragraph(applyOnline.getMobilephone(), cellContent);
		cell = new PdfPCell(pragraph12);
		cell.setVerticalAlignment(Element.ALIGN_MIDDLE); // 基本单元格上下居中
		cell.setHorizontalAlignment(Element.ALIGN_CENTER); // 基本单元格左右居中
		cell.setFixedHeight(35);
		table.addCell(cell);

		Paragraph tab11 = new Paragraph("固定电话", cellTitle);
		cell = new PdfPCell(tab11);
		cell.setVerticalAlignment(Element.ALIGN_MIDDLE); // 基本单元格上下居中
		cell.setHorizontalAlignment(Element.ALIGN_CENTER); // 基本单元格左右居中
		cell.setFixedHeight(35);
		table.addCell(cell);
		Paragraph pragraph13 = new Paragraph(applyOnline.getTelephone(), cellContent);
		cell = new PdfPCell(pragraph13);
		cell.setVerticalAlignment(Element.ALIGN_MIDDLE); // 基本单元格上下居中
		cell.setHorizontalAlignment(Element.ALIGN_CENTER); // 基本单元格左右居中
		cell.setFixedHeight(35);
		cell.setColspan(2);
		table.addCell(cell);

		Paragraph tab12 = new Paragraph("取得符合报考资格\n的学历的学习形式", cellTitle);
		cell = new PdfPCell(tab12);
		cell.setVerticalAlignment(Element.ALIGN_MIDDLE); // 基本单元格上下居中
		cell.setHorizontalAlignment(Element.ALIGN_CENTER); // 基本单元格左右居中
		cell.setFixedHeight(40);
		table.addCell(cell);
		Paragraph pragraph14 = new Paragraph(applyOnline.getStudyMode(), cellContent);
		cell = new PdfPCell(pragraph14);
		cell.setVerticalAlignment(Element.ALIGN_MIDDLE); // 基本单元格上下居中
		cell.setHorizontalAlignment(Element.ALIGN_CENTER); // 基本单元格左右居中
		cell.setFixedHeight(35);
		table.addCell(cell);

		Paragraph tab13 = new Paragraph("符合报考资格的学历", cellTitle);
		cell = new PdfPCell(tab13);
		cell.setVerticalAlignment(Element.ALIGN_MIDDLE); // 基本单元格上下居中
		cell.setHorizontalAlignment(Element.ALIGN_CENTER); // 基本单元格左右居中
		cell.setFixedHeight(35);
		table.addCell(cell);
		Paragraph pragraph15 = new Paragraph(applyOnline.getDiploma(), cellContent);
		cell = new PdfPCell(pragraph15);
		cell.setVerticalAlignment(Element.ALIGN_MIDDLE); // 基本单元格上下居中
		cell.setHorizontalAlignment(Element.ALIGN_CENTER); // 基本单元格左右居中
		cell.setFixedHeight(35);
		cell.setColspan(2);
		table.addCell(cell);

		Paragraph tab14 = new Paragraph("符合报考资格\n的毕业证书编号", cellTitle);
		cell = new PdfPCell(tab14);
		cell.setVerticalAlignment(Element.ALIGN_MIDDLE); // 基本单元格上下居中
		cell.setHorizontalAlignment(Element.ALIGN_CENTER); // 基本单元格左右居中
		cell.setFixedHeight(35);
		table.addCell(cell);
		Paragraph pragraph16 = new Paragraph(applyOnline.getGraduationCertificateId(), cellContent);
		cell = new PdfPCell(pragraph16);
		cell.setVerticalAlignment(Element.ALIGN_MIDDLE); // 基本单元格上下居中
		cell.setHorizontalAlignment(Element.ALIGN_CENTER); // 基本单元格左右居中
		cell.setFixedHeight(35);
		table.addCell(cell);

		Paragraph tab15 = new Paragraph("获得报考资格\n的毕业年月", cellTitle);
		cell = new PdfPCell(tab15);
		cell.setVerticalAlignment(Element.ALIGN_MIDDLE); // 基本单元格上下居中
		cell.setHorizontalAlignment(Element.ALIGN_CENTER); // 基本单元格左右居中
		cell.setFixedHeight(35);
		table.addCell(cell);
		String graduationdate = DateUtils.formatDate(applyOnline.getGraduationDate(), DateUtils.YYYY_MM_DD);
		Paragraph pragraph17 = new Paragraph(graduationdate, cellContent);
		cell = new PdfPCell(pragraph17);
		cell.setVerticalAlignment(Element.ALIGN_MIDDLE); // 基本单元格上下居中
		cell.setHorizontalAlignment(Element.ALIGN_CENTER); // 基本单元格左右居中
		cell.setFixedHeight(35);
		cell.setColspan(2);
		table.addCell(cell);

		Paragraph tab16 = new Paragraph("毕业院校", cellTitle);
		cell = new PdfPCell(tab16);
		cell.setVerticalAlignment(Element.ALIGN_MIDDLE); // 基本单元格上下居中
		cell.setHorizontalAlignment(Element.ALIGN_CENTER); // 基本单元格左右居中
		cell.setFixedHeight(35);
		table.addCell(cell);
		Paragraph pragraph18 = new Paragraph(applyOnline.getSchool(), cellContent);
		cell = new PdfPCell(pragraph18);
		cell.setVerticalAlignment(Element.ALIGN_MIDDLE); // 基本单元格上下居中
		cell.setHorizontalAlignment(Element.ALIGN_CENTER); // 基本单元格左右居中
		cell.setFixedHeight(35);
		table.addCell(cell);

		Paragraph tab17 = new Paragraph("毕业专业", cellTitle);
		cell = new PdfPCell(tab17);
		cell.setVerticalAlignment(Element.ALIGN_MIDDLE); // 基本单元格上下居中
		cell.setHorizontalAlignment(Element.ALIGN_CENTER); // 基本单元格左右居中
		cell.setFixedHeight(35);
		table.addCell(cell);
		Paragraph pragraph19 = new Paragraph(applyOnline.getMajor(), cellContent);
		cell = new PdfPCell(pragraph19);
		cell.setVerticalAlignment(Element.ALIGN_MIDDLE); // 基本单元格上下居中
		cell.setHorizontalAlignment(Element.ALIGN_CENTER); // 基本单元格左右居中
		cell.setFixedHeight(35);
		cell.setColspan(2);
		table.addCell(cell);

		Paragraph tab18 = new Paragraph("工作单位", cellTitle);
		cell = new PdfPCell(tab18);
		cell.setVerticalAlignment(Element.ALIGN_MIDDLE); // 基本单元格上下居中
		cell.setHorizontalAlignment(Element.ALIGN_CENTER); // 基本单元格左右居中
		cell.setFixedHeight(35);
		table.addCell(cell);
		Paragraph pragraph20 = new Paragraph(applyOnline.getCompany(), cellContent);
		cell = new PdfPCell(pragraph20);
		cell.setVerticalAlignment(Element.ALIGN_MIDDLE); // 基本单元格上下居中
		cell.setHorizontalAlignment(Element.ALIGN_CENTER); // 基本单元格左右居中
		cell.setFixedHeight(35);
		table.addCell(cell);

		Paragraph tab19 = new Paragraph("现任职务", cellTitle);
		cell = new PdfPCell(tab19);
		cell.setVerticalAlignment(Element.ALIGN_MIDDLE); // 基本单元格上下居中
		cell.setHorizontalAlignment(Element.ALIGN_CENTER); // 基本单元格左右居中
		cell.setFixedHeight(35);
		table.addCell(cell);
		Paragraph pragraph21 = new Paragraph(applyOnline.getJob(), cellContent);
		cell = new PdfPCell(pragraph21);
		cell.setVerticalAlignment(Element.ALIGN_MIDDLE); // 基本单元格上下居中
		cell.setHorizontalAlignment(Element.ALIGN_CENTER); // 基本单元格左右居中
		cell.setFixedHeight(35);
		cell.setColspan(2);
		table.addCell(cell);

		Paragraph tab20 = new Paragraph("工作年限", cellTitle);
		cell = new PdfPCell(tab20);
		cell.setVerticalAlignment(Element.ALIGN_MIDDLE); // 基本单元格上下居中
		cell.setHorizontalAlignment(Element.ALIGN_CENTER); // 基本单元格左右居中
		cell.setFixedHeight(35);
		table.addCell(cell);
		Paragraph pragraph22 = new Paragraph(applyOnline.getYearsOfWorking(), cellContent);
		cell = new PdfPCell(pragraph22);
		cell.setVerticalAlignment(Element.ALIGN_MIDDLE); // 基本单元格上下居中
		cell.setHorizontalAlignment(Element.ALIGN_CENTER); // 基本单元格左右居中
		cell.setFixedHeight(35);
		table.addCell(cell);

		Paragraph tab21 = new Paragraph("管理经验年限", cellTitle);
		cell = new PdfPCell(tab21);
		cell.setVerticalAlignment(Element.ALIGN_MIDDLE); // 基本单元格上下居中
		cell.setHorizontalAlignment(Element.ALIGN_CENTER); // 基本单元格左右居中
		cell.setFixedHeight(35);
		table.addCell(cell);
		Paragraph pragraph23 = new Paragraph(applyOnline.getYearsOfManage(), cellContent);
		cell = new PdfPCell(pragraph23);
		cell.setVerticalAlignment(Element.ALIGN_MIDDLE); // 基本单元格上下居中
		cell.setHorizontalAlignment(Element.ALIGN_CENTER); // 基本单元格左右居中
		cell.setFixedHeight(35);
		cell.setColspan(2);
		table.addCell(cell);

		Paragraph tab22 = new Paragraph("工作简历\n及\n主要业绩", cellTitle);
		cell = new PdfPCell(tab22);
		cell.setVerticalAlignment(Element.ALIGN_MIDDLE); // 基本单元格上下居中
		cell.setHorizontalAlignment(Element.ALIGN_CENTER); // 基本单元格左右居中
		cell.setFixedHeight(35);
		table.addCell(cell);
		Paragraph pragraph24 = new Paragraph(applyOnline.getWorkingExperience(), cellContent);
		cell = new PdfPCell(pragraph24);
		cell.setVerticalAlignment(Element.ALIGN_MIDDLE); // 基本单元格上下居中
		cell.setColspan(4);
		cell.setFixedHeight(250);
		table.addCell(cell);

		Paragraph tab23 = new Paragraph("紧急联系人姓名", cellTitle);
		cell = new PdfPCell(tab23);
		cell.setVerticalAlignment(Element.ALIGN_MIDDLE); // 基本单元格上下居中
		cell.setHorizontalAlignment(Element.ALIGN_CENTER); // 基本单元格左右居中
		cell.setFixedHeight(35);
		table.addCell(cell);
		Paragraph pragraph25 = new Paragraph(applyOnline.getEmergencyContactName(), cellContent);
		cell = new PdfPCell(pragraph25);
		cell.setVerticalAlignment(Element.ALIGN_MIDDLE); // 基本单元格上下居中
		cell.setHorizontalAlignment(Element.ALIGN_CENTER); // 基本单元格左右居中
		cell.setFixedHeight(35);
		table.addCell(cell);

		Paragraph tab24 = new Paragraph("紧急联系人电话", cellTitle);
		cell = new PdfPCell(tab24);
		cell.setVerticalAlignment(Element.ALIGN_MIDDLE); // 基本单元格上下居中
		cell.setHorizontalAlignment(Element.ALIGN_CENTER); // 基本单元格左右居中
		cell.setFixedHeight(35);
		table.addCell(cell);
		Paragraph pragraph26 = new Paragraph(applyOnline.getEmergencyContactPhone(), cellContent);
		cell = new PdfPCell(pragraph26);
		cell.setVerticalAlignment(Element.ALIGN_MIDDLE); // 基本单元格上下居中
		cell.setHorizontalAlignment(Element.ALIGN_CENTER); // 基本单元格左右居中
		cell.setFixedHeight(35);
		cell.setColspan(2);
		table.addCell(cell);

		Paragraph tab25 = new Paragraph("就读教学点", cellTitle);
		cell = new PdfPCell(tab25);
		cell.setVerticalAlignment(Element.ALIGN_MIDDLE); // 基本单元格上下居中
		cell.setHorizontalAlignment(Element.ALIGN_CENTER); // 基本单元格左右居中
		cell.setFixedHeight(35);
		table.addCell(cell);
		Paragraph pragraph27 = new Paragraph(applyOnline.getStudyLocation(), cellContent);
		cell = new PdfPCell(pragraph27);
		cell.setVerticalAlignment(Element.ALIGN_MIDDLE); // 基本单元格上下居中
		cell.setHorizontalAlignment(Element.ALIGN_CENTER); // 基本单元格左右居中
		cell.setFixedHeight(35);
		table.addCell(cell);

		Paragraph tab26 = new Paragraph("考生签名", cellTitle);
		cell = new PdfPCell(tab26);
		cell.setVerticalAlignment(Element.ALIGN_MIDDLE); // 基本单元格上下居中
		cell.setHorizontalAlignment(Element.ALIGN_CENTER); // 基本单元格左右居中
		cell.setFixedHeight(35);
		table.addCell(cell);
		
		cell = new PdfPCell();
		cell.setFixedHeight(35);
		cell.setColspan(2);
		table.addCell(cell);
		document.add(table);

		// 关闭文档
		document.close();
		File file = new File(realPathPrefix + "pdf/" + _fileName);
		return file;
	}

}

二、导出zip文件

package com.newtouch.zip;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.List;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.newtouch.common.utils.DateUtils;

public class makezip {
	// 文件打包下载
	public static HttpServletResponse downLoadFiles(List<File> files, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		try {
			/**
			 * 这个有集合就是你想要打包的所文件, 这里假设已经准备好了所要打包的文件
			 */
			// List<File> files = new ArrayList<File>();

			/**
			 * 创建一个临时压缩文件, 我们会把文件流全部注入到这个文件中 这里的文件你可以自定义是.rar还是.zip
			 */
			String pathname = request.getRealPath("/")+"glxy.nwpu.edu.cn_PersonInfomation_" + DateUtils.getDate("yyyyMMddHHmmss") + ".rar";
			File file = new File(pathname);
			if (!file.exists()) {
				file.createNewFile();
			}
			response.reset();
			// response.getWriter()
			// 创建文件输出流
			FileOutputStream fous = new FileOutputStream(file);
			/**
			 * 打包的方法我们会用到ZipOutputStream这样一个输出流, 所以这里我们把输出流转换一下
			 */
			// org.apache.tools.zip.ZipOutputStream zipOut
			// = new org.apache.tools.zip.ZipOutputStream(fous);
			ZipOutputStream zipOut = new ZipOutputStream(fous);
			/**
			 * 这个方法接受的就是一个所要打包文件的集合, 还有一个ZipOutputStream
			 */
			zipFile(files, zipOut);
			zipOut.close();
			fous.close();
			return downloadZip(file, response);
		} catch (Exception e) {
			e.printStackTrace();
		}
		/**
		 * 直到文件的打包已经成功了, 文件的打包过程被我封装在FileUtil.zipFile这个静态方法中,
		 * 稍后会呈现出来,接下来的就是往客户端写数据了
		 */
		// OutputStream out = response.getOutputStream();

		return response;
	}

	/**
	 * 把接受的全部文件打成压缩包
	 * 
	 * @param List<File>;
	 * @param org.apache.tools.zip.ZipOutputStream
	 */
	public static void zipFile(List<File> files, ZipOutputStream outputStream) {
		int size = files.size();
		for (int i = 0; i < size; i++) {
			File file = (File) files.get(i);
			zipFile(file, outputStream);
		}
	}

	public static HttpServletResponse downloadZip(File file, HttpServletResponse response) {
		try {
			// 以流的形式下载文件。
			InputStream fis = new BufferedInputStream(new FileInputStream(file.getPath()));
			byte[] buffer = new byte[fis.available()];
			fis.read(buffer);
			fis.close();
			// 清空response
			response.reset();

			OutputStream toClient = new BufferedOutputStream(response.getOutputStream());
			response.setContentType("application/octet-stream");
			response.setHeader("Content-Disposition",
					"attachment;filename=" + java.net.URLEncoder.encode(file.getName(), "UTF-8"));
			toClient.write(buffer);
			toClient.flush();
			toClient.close();
		} catch (IOException ex) {
			ex.printStackTrace();
		} finally {
			try {
				File f = new File(file.getPath());
				f.delete();
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return response;
	}
	public static HttpServletResponse download(File file, HttpServletResponse response) {
		try {
			// 以流的形式下载文件。
			InputStream fis = new BufferedInputStream(new FileInputStream(file.getPath()));
			byte[] buffer = new byte[fis.available()];
			fis.read(buffer);
			fis.close();
			// 清空response
			response.reset();
			
			OutputStream toClient = new BufferedOutputStream(response.getOutputStream());
			response.setContentType("application/octet-stream");
			response.setHeader("Content-Disposition",
					"attachment;filename=" + java.net.URLEncoder.encode(file.getName(), "UTF-8"));
			toClient.write(buffer);
			toClient.flush();
			toClient.close();
		} catch (IOException ex) {
			ex.printStackTrace();
		}
		return response;
	}

	/**
	 * 根据输入的文件与输出流对文件进行打包
	 * 
	 * @param File
	 * @param org.apache.tools.zip.ZipOutputStream
	 */
	public static void zipFile(File inputFile, ZipOutputStream ouputStream) {
		try {
			if (inputFile.exists()) {
				/**
				 * 如果是目录的话这里是不采取操作的, 至于目录的打包正在研究中
				 */
				FileInputStream in = null;
				BufferedInputStream bins= null;
				if (inputFile.isFile()) {
					in = new FileInputStream(inputFile);
					bins = new BufferedInputStream(in, 512);
					// org.apache.tools.zip.ZipEntry
					ZipEntry entry = new ZipEntry(inputFile.getName());
					ouputStream.putNextEntry(entry);
					// 向压缩文件中输出数据
					int nNumber;
					byte[] buffer = new byte[512];
					while ((nNumber = bins.read(buffer)) != -1) {
						ouputStream.write(buffer, 0, nNumber);
					}
					// 关闭创建的流对象
					bins.close();
					in.close();
				} else {
					try {
						File[] files = inputFile.listFiles();
						for (int i = 0; i < files.length; i++) {
							zipFile(files[i], ouputStream);
						}
					} catch (Exception e) {
						e.printStackTrace();
					}
					finally {
						if(in != null){
							in.close();
						}
						if(bins != null){
							bins.close();
						}
					}
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值