导出PDF和Zip文件的工具类
生活随笔
收集整理的這篇文章主要介紹了
导出PDF和Zip文件的工具类
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
一、導出pdf文件,使用itext框架:
1、引入pom文件:
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();// 清空responseresponse.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();// 清空responseresponse.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.ZipEntryZipEntry 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();}}}總結
以上是生活随笔為你收集整理的导出PDF和Zip文件的工具类的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: EXCEL导入导出使用的框架
- 下一篇: 阿里巴巴发送短信接口的使用