poi根據模版導出多頁word,帶插入圖片,並壓縮下載

工具類代碼

package cn.edu.nfu.jw.srs.utils;

import org.apache.poi.xwpf.usermodel.*;
import org.apache.xmlbeans.XmlOptions;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTBody;

import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

public class WordUtils {
    /**
     * 根據模板生成word
     *
     * @param path      模板的路徑
     * @param params    需要替換的參數
     * @param tableList 需要插入的參數
     * @param fileName  生成word文件的文件名
     * @param response
     */
    public void getWord(String path, Map<String, Object> params, List<String[]> tableList, String fileName, HttpServletResponse response) throws Exception {
        File file = new File(path);
        InputStream is = new FileInputStream(file);
        CustomXWPFDocument doc = new CustomXWPFDocument(is);
        this.replaceInPara(doc, doc, params);    //替換文本里面的變量
        this.replaceInTable(doc, doc, params, tableList); //替換表格裏面的變量
        OutputStream os = response.getOutputStream();
        response.setHeader("Content-disposition", "attachment; filename=" + fileName);
        doc.write(os);
        this.close(os);
        this.close(is);
    }

    /**
     * 替換段落裏面的變量
     *
     * @param doc    要替換的文檔
     * @param params 參數
     */
    public void replaceInPara(CustomXWPFDocument firstDoc, CustomXWPFDocument doc, Map<String, Object> params) {
        Iterator<XWPFParagraph> iterator = doc.getParagraphsIterator();
        XWPFParagraph para;
        while (iterator.hasNext()) {
            para = iterator.next();
            this.replaceInPara(firstDoc, para, params, doc);
        }
    }

    /**
     * 替換段落裏面的變量
     *
     * @param para   要替換的段落
     * @param params 參數
     */
    private void replaceInPara(CustomXWPFDocument firstDoc, XWPFParagraph para, Map<String, Object> params, CustomXWPFDocument doc) {
        List<XWPFRun> runs;
        Matcher matcher;
        if (this.matcher(para.getParagraphText()).find()) {
            runs = para.getRuns();
            int start = -1;
            int end = -1;
            String str = "";
            for (int i = 0; i < runs.size(); i++) {
                XWPFRun run = runs.get(i);
                String runText = run.toString();
                if ('$' == runText.charAt(0) && '{' == runText.charAt(1)) {
                    start = i;
                }
                if ((start != -1)) {
                    str += runText;
                }
                if ('}' == runText.charAt(runText.length() - 1)) {
                    if (start != -1) {
                        end = i;
                        break;
                    }
                }
            }

            for (int i = start; i <= end; i++) {
                para.removeRun(i);
                i--;
                end--;
            }

            for (Map.Entry<String, Object> entry : params.entrySet()) {
                String key = entry.getKey();
                if (str.indexOf(key) != -1) {
                    Object value = entry.getValue();
                    if (value instanceof String) {
                        str = str.replace(key, value.toString());
                        para.createRun().setText(str, 0);
                        break;
                    } else if (value instanceof Map) {
                        str = str.replace(key, "");
                        Map pic = (Map) value;
                        int width = Integer.parseInt(pic.get("width").toString());
                        int height = Integer.parseInt(pic.get("height").toString());
                        int picType = getPictureType(pic.get("type").toString());
                        byte[] byteArray = (byte[]) pic.get("content");
                        ByteArrayInputStream byteInputStream = new ByteArrayInputStream(byteArray);
                        try {
                            String ind = firstDoc.addPictureData(byteInputStream, picType);
                            int id = firstDoc.getNextPicNameNumber(picType);
                            firstDoc.createPicture(ind, id, width, height, para);
                            // 由於圖片重複則顯示錯誤,重寫create方法
                            // firstDoc.addPictureData(byteInputStream, picType);
                            // firstDoc.createPicture(firstDoc.getAllPictures().size() - 1, width, height, para, firstDoc);
                            para.createRun().setText(str, 0);
                            break;
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
                }
            }
        }
    }


    /**
     * 爲表格插入數據,行數不夠添加新行
     *
     * @param table     需要插入數據的表格
     * @param tableList 插入數據集合
     */
    private static void insertTable(XWPFTable table, List<String[]> tableList) {
        //創建行,根據需要插入的數據添加新行,不處理表頭
        for (int i = 0; i < tableList.size(); i++) {
            XWPFTableRow row = table.createRow();
        }
        //遍歷表格插入數據
        List<XWPFTableRow> rows = table.getRows();
        int length = table.getRows().size();
        for (int i = 1; i < length - 1; i++) {
            XWPFTableRow newRow = table.getRow(i);
            List<XWPFTableCell> cells = newRow.getTableCells();
            for (int j = 0; j < cells.size(); j++) {
                XWPFTableCell cell = cells.get(j);
                String s = tableList.get(i - 1)[j];
                cell.setText(s);
            }
        }
    }

    /**
     * 替換表格裏面的變量
     *
     * @param doc    要替換的文檔
     * @param params 參數
     */
    public void replaceInTable(CustomXWPFDocument firstDoc, CustomXWPFDocument doc, Map<String, Object> params, List<String[]> tableList) {
        Iterator<XWPFTable> iterator = doc.getTablesIterator();
        XWPFTable table;
        List<XWPFTableRow> rows;
        List<XWPFTableCell> cells;
        List<XWPFParagraph> paras;
        while (iterator.hasNext()) {
            table = iterator.next();
            if (table.getRows().size() > 1) {
                //判斷表格是需要替換還是需要插入,判斷邏輯有$爲替換,表格無$爲插入
                if (this.matcher(table.getText()).find()) {
                    rows = table.getRows();
                    for (XWPFTableRow row : rows) {
                        cells = row.getTableCells();
                        for (XWPFTableCell cell : cells) {
                            paras = cell.getParagraphs();
                            for (XWPFParagraph para : paras) {
                                this.replaceInPara(firstDoc, para, params, doc);
                            }
                        }
                    }
                } else {
                    insertTable(table, tableList);  //插入數據
                }
            }
        }
    }

    /**
     * 正則匹配字符串
     *
     * @param str
     * @return
     */
    private Matcher matcher(String str) {
        Pattern pattern = Pattern.compile("\\$\\{(.+?)\\}", Pattern.CASE_INSENSITIVE);
        Matcher matcher = pattern.matcher(str);
        return matcher;
    }

    /**
     * 根據圖片類型,取得對應的圖片類型代碼
     *
     * @param picType
     * @return int
     */
    public int getPictureType(String picType) {
        int res = CustomXWPFDocument.PICTURE_TYPE_PICT;
        if (picType != null) {
            if (picType.equalsIgnoreCase("png")) {
                res = CustomXWPFDocument.PICTURE_TYPE_PNG;
            } else if (picType.equalsIgnoreCase("dib")) {
                res = CustomXWPFDocument.PICTURE_TYPE_DIB;
            } else if (picType.equalsIgnoreCase("emf")) {
                res = CustomXWPFDocument.PICTURE_TYPE_EMF;
            } else if (picType.equalsIgnoreCase("jpg") || picType.equalsIgnoreCase("jpeg")) {
                res = CustomXWPFDocument.PICTURE_TYPE_JPEG;
            } else if (picType.equalsIgnoreCase("wmf")) {
                res = CustomXWPFDocument.PICTURE_TYPE_WMF;
            }
        }
        return res;
    }

    public  byte[] input2byte(InputStream inStream) throws IOException {
        ByteArrayOutputStream swapStream = new ByteArrayOutputStream();
        byte[] buff = new byte[inStream.available()];
        int rc = 0;
        while ((rc = inStream.read(buff, 0, 100)) > 0) {
            swapStream.write(buff, 0, rc);
        }
        byte[] in2b = swapStream.toByteArray();
        swapStream.close();
        return in2b;
    }

    /**
     * 將輸入流中的數據寫入字節數組
     *
     * @param in
     * @return
     */
    public static byte[] inputStream2ByteArray(InputStream in, boolean isClose) {
        byte[] byteArray = null;
        try {
            int total = in.available();
            byteArray = new byte[total];
            in.read(byteArray);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (isClose) {
                try {
                    in.close();
                } catch (Exception e2) {
                    e2.getStackTrace();
                }
            }
        }
        return byteArray;
    }

    /**
     * 關閉輸入流
     *
     * @param is
     */
    public void close(InputStream is) {
        if (is != null) {
            try {
                is.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * 關閉輸出流
     *
     * @param os
     */
    public void close(OutputStream os) {
        if (os != null) {
            try {
                os.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * @Description: 保存圖片(用來插入)
     * @Param:
     * @return:
     * @Author: lgc
     * @Date: 2019/4/24
     */
    public static byte[] readInputStream(InputStream inStream) throws Exception {
        ByteArrayOutputStream outStream = new ByteArrayOutputStream();
        //創建一個Buffer字符串
        byte[] buffer = new byte[1024];
        //每次讀取的字符串長度,如果爲-1,代表全部讀取完畢
        int len = 0;
        //使用一個輸入流從buffer裏把數據讀取出來
        while ((len = inStream.read(buffer)) != -1) {
            //用輸出流往buffer裏寫入數據,中間參數代表從哪個位置開始讀,len代表讀取的長度
            outStream.write(buffer, 0, len);
        }
        //關閉輸入流
        inStream.close();
        //把outStream裏的數據寫入內存
        return outStream.toByteArray();
    }

    //兩個對象進行追加
    public static CustomXWPFDocument mergeWord(CustomXWPFDocument document, CustomXWPFDocument doucDocument2) throws Exception {
        CustomXWPFDocument src1Document = document;
        XWPFParagraph p = src1Document.createParagraph();
        //設置分頁符
        p.setPageBreak(true);
        CTBody src1Body = src1Document.getDocument().getBody();
        CustomXWPFDocument src2Document = doucDocument2;
        CTBody src2Body = src2Document.getDocument().getBody();
        XWPFParagraph p2 = src2Document.createParagraph();
        XmlOptions optionsOuter = new XmlOptions();
        optionsOuter.setSaveOuter();
        String appendString = src2Body.xmlText(optionsOuter);
        String srcString = src1Body.xmlText();
        String prefix = srcString.substring(0, srcString.indexOf(">") + 1);
        String mainPart = srcString.substring(srcString.indexOf(">") + 1, srcString.lastIndexOf("<"));
        String sufix = srcString.substring(srcString.lastIndexOf("<"));
        String addPart = appendString.substring(appendString.indexOf(">") + 1, appendString.lastIndexOf("<"));
        CTBody makeBody = CTBody.Factory.parse(prefix + mainPart + addPart + sufix);
        src1Body.set(makeBody);
        return src1Document;
    }

    //壓縮輸出
    public static void createZipPath(String path, HttpServletResponse response, String zipName) throws IOException {
        ZipOutputStream zipOutputStream = null;
        OutputStream output = response.getOutputStream();
        response.reset();
        String fileNameZip = new String((zipName + ".zip").getBytes("UTF-8"), "iso-8859-1");
        response.setHeader("Content-disposition", "attachment; filename=" + fileNameZip);
        response.setContentType("application/msword");
        zipOutputStream = new ZipOutputStream(output);
        File[] files = new File(path).listFiles();
        FileInputStream fileInputStream = null;
        byte[] buf = new byte[1024];
        int len = 0;
        if (files != null && files.length > 0) {
            for (File wordFile : files) {
                String fileName = wordFile.getName();
                fileInputStream = new FileInputStream(wordFile);
                //放入壓縮zip包中;
                zipOutputStream.putNextEntry(new ZipEntry(fileName));
                //讀取文件;
                while ((len = fileInputStream.read(buf)) > 0) {
                    zipOutputStream.write(buf, 0, len);
                }
                //關閉;
                zipOutputStream.closeEntry();
                if (fileInputStream != null) {
                    fileInputStream.close();
                }
            }
        }

        if (zipOutputStream != null) {
            zipOutputStream.close();
        }
    }
    /**
     * 將InputStream寫入本地文件
     * @param destination 寫入本地目錄
     * @param input 輸入流
     * @throws IOException IOException
     */
    public void writeToLocal(String destination, InputStream input)
            throws IOException {
        int index;
        byte[] bytes = new byte[1024];
        FileOutputStream downloadFile = new FileOutputStream(destination);
        while ((index = input.read(bytes)) != -1) {
            downloadFile.write(bytes, 0, index);
            downloadFile.flush();
        }
        input.close();
        downloadFile.close();

    }
    /***
     * 刪除文件夾
     *
     * @param folderPath 文件夾完整絕對路徑
     */
    public static void delFolder(String folderPath) {
        try {
            delAllFile(folderPath); // 刪除完裏面所有內容
            String filePath = folderPath;
            filePath = filePath.toString();
            java.io.File myFilePath = new java.io.File(filePath);
            myFilePath.delete(); // 刪除空文件夾
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /***
     * 刪除指定文件夾下所有文件
     *
     * @param path 文件夾完整絕對路徑
     * @return
     */
    public static boolean delAllFile(String path) {
        boolean flag = false;
        File file = new File(path);
        if (!file.exists()) {
            return flag;
        }
        if (!file.isDirectory()) {
            return flag;
        }
        String[] tempList = file.list();
        File temp = null;
        for (int i = 0; i < tempList.length; i++) {
            if (path.endsWith(File.separator)) {
                temp = new File(path + tempList[i]);
            } else {
                temp = new File(path + File.separator + tempList[i]);
            }
            if (temp.isFile()) {
                temp.delete();
            }
            if (temp.isDirectory()) {
                delAllFile(path + "/" + tempList[i]);// 先刪除文件夾裏面的文件
                delFolder(path + "/" + tempList[i]);// 再刪除空文件夾
                flag = true;
            }
        }
        return flag;
    }
}

創建圖片工具類

package cn.edu.nfu.jw.srs.utils;

import org.apache.poi.openxml4j.opc.OPCPackage;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFParagraph;
import org.apache.xmlbeans.XmlException;
import org.apache.xmlbeans.XmlToken;
import org.openxmlformats.schemas.drawingml.x2006.main.CTNonVisualDrawingProps;
import org.openxmlformats.schemas.drawingml.x2006.main.CTPositiveSize2D;
import org.openxmlformats.schemas.drawingml.x2006.wordprocessingDrawing.CTInline;

import java.io.IOException;
import java.io.InputStream;

public class CustomXWPFDocument extends XWPFDocument {

    public CustomXWPFDocument(InputStream in) throws IOException {
        super(in);
    }

    public CustomXWPFDocument() {
        super();
    }

    public CustomXWPFDocument(OPCPackage pkg) throws IOException {
        super(pkg);
    }

    /**
     * @param id
     * @param width     寬
     * @param height    高
     * @param paragraph 段落
     */
    public void createPicture(int id, int width, int height, XWPFParagraph paragraph, CustomXWPFDocument doc) {
        final int EMU = 9525;
        width *= EMU;
        height *= EMU;
        String blipId = doc.getAllPictures().get(id).getPackageRelationship().getId();
        CTInline inline = paragraph.createRun().getCTR().addNewDrawing().addNewInline();
        String picXml = ""
                + "<a:graphic xmlns:a=\"http://schemas.openxmlformats.org/drawingml/2006/main\">"
                + "   <a:graphicData uri=\"http://schemas.openxmlformats.org/drawingml/2006/picture\">"
                + "      <pic:pic xmlns:pic=\"http://schemas.openxmlformats.org/drawingml/2006/picture\">"
                + "         <pic:nvPicPr>" + "            <pic:cNvPr id=\""
                + id
                + "\" name=\"Generated\"/>"
                + "            <pic:cNvPicPr/>"
                + "         </pic:nvPicPr>"
                + "         <pic:blipFill>"
                + "            <a:blip r:embed=\""
                + blipId
                + "\" xmlns:r=\"http://schemas.openxmlformats.org/officeDocument/2006/relationships\"/>"
                + "            <a:stretch>"
                + "               <a:fillRect/>"
                + "            </a:stretch>"
                + "         </pic:blipFill>"
                + "         <pic:spPr>"
                + "            <a:xfrm>"
                + "               <a:off x=\"0\" y=\"0\"/>"
                + "               <a:ext cx=\""
                + width
                + "\" cy=\""
                + height
                + "\"/>"
                + "            </a:xfrm>"
                + "            <a:prstGeom prst=\"rect\">"
                + "               <a:avLst/>"
                + "            </a:prstGeom>"
                + "         </pic:spPr>"
                + "      </pic:pic>"
                + "   </a:graphicData>" + "</a:graphic>";

        inline.addNewGraphic().addNewGraphicData();
        XmlToken xmlToken = null;
        try {
            xmlToken = XmlToken.Factory.parse(picXml);
        } catch (XmlException xe) {
            xe.printStackTrace();
        }
        inline.set(xmlToken);

        CTPositiveSize2D extent = inline.addNewExtent();
        extent.setCx(width);
        extent.setCy(height);

        CTNonVisualDrawingProps docPr = inline.addNewDocPr();
        docPr.setId(id);
        docPr.setName("圖片" + blipId);
        docPr.setDescr("頭像");
    }

    public void createPicture(String blipId, int id, int width, int height, XWPFParagraph paragraph) {
        final int EMU = 9525;
        width *= EMU;
        height *= EMU;
        //String blipId = getAllPictures().get(id).getPackageRelationship().getId();
        CTInline inline = paragraph.createRun().getCTR().addNewDrawing().addNewInline();

        String picXml = "" +
                "<a:graphic xmlns:a=\"http://schemas.openxmlformats.org/drawingml/2006/main\">" +
                "   <a:graphicData uri=\"http://schemas.openxmlformats.org/drawingml/2006/picture\">" +
                "      <pic:pic xmlns:pic=\"http://schemas.openxmlformats.org/drawingml/2006/picture\">" +
                "         <pic:nvPicPr>" +
                "            <pic:cNvPr id=\"" + id + "\" name=\"Generated\"/>" +
                "            <pic:cNvPicPr/>" +
                "         </pic:nvPicPr>" +
                "         <pic:blipFill>" +
                "            <a:blip r:embed=\"" + blipId + "\" xmlns:r=\"http://schemas.openxmlformats.org/officeDocument/2006/relationships\"/>" +
                "            <a:stretch>" +
                "               <a:fillRect/>" +
                "            </a:stretch>" +
                "         </pic:blipFill>" +
                "         <pic:spPr>" +
                "            <a:xfrm>" +
                "               <a:off x=\"0\" y=\"0\"/>" +
                "               <a:ext cx=\"" + width + "\" cy=\"" + height + "\"/>" +
                "            </a:xfrm>" +
                "            <a:prstGeom prst=\"rect\">" +
                "               <a:avLst/>" +
                "            </a:prstGeom>" +
                "         </pic:spPr>" +
                "      </pic:pic>" +
                "   </a:graphicData>" +
                "</a:graphic>";

        inline.addNewGraphic().addNewGraphicData();
        XmlToken xmlToken = null;
        try {
            xmlToken = XmlToken.Factory.parse(picXml);
        } catch (XmlException xe) {
            xe.printStackTrace();
        }
        inline.set(xmlToken);

        CTPositiveSize2D extent = inline.addNewExtent();
        extent.setCx(width);
        extent.setCy(height);

        CTNonVisualDrawingProps docPr = inline.addNewDocPr();
        docPr.setId(id);
        docPr.setName("圖片" + id);
        docPr.setDescr("頭像");
    }
}

操作ftp文件工具類

package cn.edu.nfu.jw.srs.utils;

import java.io.*;
import java.net.URLEncoder;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

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

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.poi.hwpf.HWPFDocument;
import org.apache.poi.hwpf.usermodel.Range;

import cn.edu.nfu.jw.exception.ServiceException;
import cn.edu.nfu.jw.srs.domain.SrsJbzl;
import cn.edu.nfu.jw.srs.domain.SrsPunishment;

/**
 * 創建word文檔
 * 
 * @author manolin
 *
 */
public class CommonUtil {
	private static final Log log = LogFactory.getLog(CommonUtil.class);
	public static void createDocumentByTemplate(String realname, HttpServletResponse response,
			String templeteName, SrsPunishment info) {
		
		OutputStream outp = null;
		InputStream in = null;
		templeteName=File.separator+"doc"+File.separator+templeteName; // 模板文件名字
		String fileName = realname; //輸出文件名稱
		try {
			fileName = URLEncoder.encode(fileName, "UTF-8") ;
			if(!fileName.endsWith(".doc")){
				fileName=fileName+".doc";
			}
			response.reset();
			response.setContentType("application/doc");
			response.addHeader("Content-Disposition", "attachment; filename="+fileName);
			response.setContentType("application/octet-stream;charset=UTF-8");
			outp = response.getOutputStream();
			in = CommonUtil.class.getClassLoader().getResourceAsStream(templeteName);
			//替換變量
			HWPFDocument doc = new HWPFDocument(in);
			Range range = doc.getRange();
			range.replaceText("${name}", info.getSrsJbzl().getXm());
			range.replaceText("${punishment}", info.getPunishment());
			range.replaceText("${gender}",getGender(info.getSrsJbzl()));
			range.replaceText("${xh}",info.getSrsJbzl().getXh());
			range.replaceText("${yxmc}",info.getSrsJbzl().getFesYx().getYxmc());
			range.replaceText("${zymc}",info.getSrsJbzl().getFesZy().getZymc());
			range.replaceText("${rxnj}",info.getSrsJbzl().getRxnj().toString());
			range.replaceText("${cheatTime}",formatDate(info.getCheatTime()));
			range.replaceText("${cheatSubject}",info.getCheatSubject());
			range.replaceText("${cheatDetail}",info.getCheatDetail());
			range.replaceText("${rule}",info.getRules());
			range.replaceText("${cTime}",formatDate(info.getcTime()));
			ByteArrayOutputStream ostream = new ByteArrayOutputStream();
			doc.write(ostream);
			outp.write(ostream.toByteArray());
			outp.flush();
		} catch (Exception e) {
			e.printStackTrace();
			log.error("occur exception on get file template: "  + e.getLocalizedMessage());
		} finally {
			try {
				if (in != null) {
					in.close();
				}
				if (outp != null) {
					outp.close();
				}
			} catch (Exception e) {
				log.error("occur exception on closing stream: "  + e.getLocalizedMessage());
			}
		}
	}
	private static String getGender(SrsJbzl srsJbzl){
		String gender="";
		if(srsJbzl.getXbdm().equals("1")){
			gender="男";
		}else if(srsJbzl.getXbdm().equals("2")){
			gender="女";
		}else{
			log.error("gender data error! student id="+srsJbzl.getId()); 
			throw new ServiceException(-1,"學生性別數據異常!");
		}
		return gender;
	}
	private static String formatDate(Date date){
		DateFormat dft=new SimpleDateFormat("yyyy年MM月dd日");
		return dft.format(date);
	}

	/**
	 * 上傳文件
	 * @param file
	 * @param filePath
	 * @param fileName
	 */
	public static void uploadFile(byte[] file, String filePath, String fileName) {
		File targetFile = new File(filePath);
		if(!targetFile.exists()){
			targetFile.mkdirs();
		}
		FileOutputStream out = null;
		try {
			out = new FileOutputStream(filePath + fileName);
			out.write(file);
			out.flush();
		} catch (Exception e) {

		} finally {
			try {
				if (out != null)
					out.close();
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}

	/**
	 * 上傳文件到ftp服務器
	 * @param host
	 * @param port
	 * @param username
	 * @param pwd
	 * @param path
	 * @param local
	 * @param filename
	 */
	public static boolean uploadFile(String host, int port, String username, String pwd,
									 String path, InputStream local, String filename){
		boolean result = false;
		//創建客戶端對象
		FTPClient ftp = new FTPClient();
		try {
			//連接ftp服務器
			ftp.connect(host, port);
			//登錄
			ftp.login(username, pwd);
			//檢查上傳路徑是否存在 如果不存在返回false
			boolean flag = ftp.changeWorkingDirectory(path);
			if(!flag){
				//創建上傳的路徑  該方法只能創建一級目錄,在這裏如果/home/ftpuser存在則可創建image
				ftp.makeDirectory(path);
			}
			//指定編碼
			//ftp.setControlEncoding("GBK");
			//防止數據鏈路不通
			ftp.enterLocalPassiveMode();
			//指定上傳路徑
			ftp.changeWorkingDirectory(path);
			//指定上傳文件的類型  二進制文件
			ftp.setFileType(FTPClient.BINARY_FILE_TYPE);
			//第一個參數是文件名
			ftp.storeFile(new String(filename.getBytes(),"iso-8859-1"), local);
			result = true;
		} catch (IOException e) {
			log.error("occur exception on uploading green channel file to ftp server : " + e.getLocalizedMessage());
			e.printStackTrace();
		} finally {
			try {
				//關閉文件流
				local.close();
				//退出
				ftp.logout();
				//斷開連接
				ftp.disconnect();
			} catch (IOException e) {
				log.error("occur exception on close stream on uploading green channel file to ftp server : " + e.getLocalizedMessage());
				e.printStackTrace();
			}
		}
		return result;
	}

	/**
	 * 返回流
	 * @param host
	 * @param port
	 * @param username
	 * @param pwd
	 * @param path
	 * @param response
	 * @param ftpfilename
	 * @return
	 */
	public static boolean getFileIo(String host, int port, String username, String pwd,
									String path, HttpServletResponse response, String ftpfilename){
		boolean result = false;
		//創建客戶端對象
		FTPClient ftp = new FTPClient();
		try {
			//連接ftp服務器
			ftp.connect(host, port);
			//登錄
			ftp.login(username, pwd);
			//檢查上傳路徑是否存在 如果不存在返回false
			boolean flag = ftp.changeWorkingDirectory(path);
			if (!flag) {
				//創建上傳的路徑  該方法只能創建一級目錄,在這裏如果/home/ftpuser存在則可創建image
				ftp.makeDirectory(path);
			}
			//指定編碼
			//ftp.setControlEncoding("GBK");
			//防止數據鏈路不通
			ftp.enterLocalPassiveMode();
			//切換目錄
			ftp.changeWorkingDirectory(path);
			//指定上傳文件的類型  二進制文件
			ftp.setFileType(FTPClient.BINARY_FILE_TYPE);
			//設置文件返回類型
			response.setContentType("image/*");
			response.addHeader("Content-Disposition", "attachment; filename="+ftpfilename+".png");
			result = ftp.retrieveFile(new String(ftpfilename.getBytes(),"iso-8859-1"), response.getOutputStream());
		} catch (IOException e) {
			log.error("occur exception on get ftp file io from ftp server : " + e.getLocalizedMessage());
			e.printStackTrace();
		} finally {
			try {
				//退出
				ftp.logout();
				//斷開連接
				ftp.disconnect();
			} catch (IOException e) {
				log.error("occur exception on close stream on get ftp file io from ftp server : " + e.getLocalizedMessage());
				e.printStackTrace();
			}
		}
		return result;
	}

	/**
	 * 獲取文件輸入流
	 * @param host
	 * @param port
	 * @param username
	 * @param pwd
	 * @param path
	 * @param ftpfilename
	 * @return
	 */
	public static InputStream  getFileInputStream(String host, int port, String username, String pwd,
									String path, String ftpfilename){
		//創建客戶端對象
		FTPClient ftp = new FTPClient();
		InputStream inputStream = null;
		try {
			//連接ftp服務器
			ftp.connect(host, port);
			//登錄
			ftp.login(username, pwd);
			//檢查上傳路徑是否存在 如果不存在返回false
			boolean flag = ftp.changeWorkingDirectory(path);
			if (!flag) {
				//創建上傳的路徑  該方法只能創建一級目錄,在這裏如果/home/ftpuser存在則可創建image
				ftp.makeDirectory(path);
			}
			//指定編碼
			//ftp.setControlEncoding("GBK");
			//防止數據鏈路不通
			ftp.enterLocalPassiveMode();
			//切換目錄
			ftp.changeWorkingDirectory(path);
			//指定上傳文件的類型  二進制文件
			ftp.setFileType(FTPClient.BINARY_FILE_TYPE);
			//設置文件返回類型
			inputStream = ftp.retrieveFileStream(new String(ftpfilename.getBytes(), "iso-8859-1"));
		} catch (IOException e) {
			log.error("occur exception on get ftp file io from ftp server : " + e.getLocalizedMessage());
			e.printStackTrace();
		} finally {
			try {
				//退出
				ftp.logout();
				//斷開連接
				ftp.disconnect();
			} catch (IOException e) {
				log.error("occur exception on close stream on get ftp file io from ftp server : " + e.getLocalizedMessage());
				e.printStackTrace();
			}
		}
		return inputStream;
	}

	/**
	 * 從ftp服務器上刪除文件
	 * @param host
	 * @param port
	 * @param username
	 * @param pwd
	 * @param path
	 * @param ftpfilename
	 * @return
	 */
	public static boolean delFile(String host, int port, String username, String pwd,
								  String path, String ftpfilename){
		boolean result = false;
		//創建客戶端對象
		FTPClient ftp = new FTPClient();
		try {
			//連接ftp服務器
			ftp.connect(host, port);
			//登錄
			ftp.login(username, pwd);
			//檢查上傳路徑是否存在 如果不存在返回false
			boolean flag = ftp.changeWorkingDirectory(path);
			if (!flag) {
				//創建上傳的路徑  該方法只能創建一級目錄,在這裏如果/home/ftpuser存在則可創建image
				ftp.makeDirectory(path);
			}
			//指定編碼
			//ftp.setControlEncoding("GBK");
			//防止數據鏈路不通
			ftp.enterLocalPassiveMode();
			//指定上傳路徑
			ftp.changeWorkingDirectory(path);
			//指定上傳文件的類型  二進制文件
			ftp.deleteFile(ftpfilename);
			result = true;
		} catch (IOException e) {
			log.error("occur exception on deletting green channel file from ftp server : " + e.getLocalizedMessage());
			e.printStackTrace();
		} finally {
			try {
				//退出
				ftp.logout();
				//斷開連接
				ftp.disconnect();
			} catch (IOException e) {
				log.error("occur exception on close stream on deletting green channel file from ftp server : " + e.getLocalizedMessage());
				e.printStackTrace();
			}
		}
		return result;
	}

	/**
	 * 下載模板信息
	 * 適用於windows和linux
	 * @param response
	 * @param request
	 * @param templeteName
	 * @throws IOException
	 */
	public static void downloadTemplate(String realname, HttpServletResponse response, HttpServletRequest request, String templeteName) {
		OutputStream outp = null;
		InputStream in = null;
		try {
			String fileName = templeteName; //要下載的模板文件
			if(templeteName!=null){
				if(!templeteName.endsWith(".doc")){
					fileName = templeteName + ".doc";
				}
			}
			//String filedownload = CommonUtil.class.getClassLoader().getResource("").getPath();
			//filedownload += fileName;
			fileName = URLEncoder.encode(fileName, "UTF-8") ;
			// 要下載的模板所在的絕對路徑
			response.reset();
			response.addHeader("Content-Disposition", "attachment; filename="+fileName);
			response.setContentType("application/octet-stream;charset=UTF-8");
			outp = response.getOutputStream();
			in = cn.edu.nfu.jw.srs.utils.CommonUtil.class.getClassLoader().getResourceAsStream(realname);
			//new File(ResourceUtils.getURL("classpath:").getPath());
			//in = new FileInputStream(filedownload);
			byte[] b = new byte[1024];
			int i;
			while ((i = in.read(b)) > 0) {
				outp.write(b, 0, i);
			}
			outp.flush();
		} catch (Exception e) {
			e.printStackTrace();
			log.error("occur exception on get file template: "  + e.getLocalizedMessage());
		} finally {
			try {
				if (in != null) {
					in.close();
				}
				if (outp != null) {
					outp.close();
				}
			} catch (Exception e) {
				log.error("occur exception on closing stream: "  + e.getLocalizedMessage());
			}
		}
	}
}

自己實現的業務代碼

public void batchPrintXjxx(HttpServletResponse response, SrsJbzl obj) {
        obj.setPageSize(Integer.MAX_VALUE);
        obj.setStart(0);
        List<SrsJbzl> userList = dao.listPage(obj);

        //創建壓縮包位置
        File fileZip = new File("/wordZip");
        if (!fileZip.exists()) {
            fileZip.mkdirs();
        }
        //到處的登記表名稱
        String wordName = "中山大學南方學院本科生學籍登記表";
        String filesPath = "/wordZip/" + new Date().getTime();
        //合併模板的個數就是每次要解析多少個當前的模板 ,那麼就需要首先定義一個集合
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        //我用的是2007版本以後poi對word進行解析的docx
        //首先定義一個XWPFDocument 集合 這個對象可以進行word 解析 合併 還有下載都離不開這個對象
        List<CustomXWPFDocument> xwpfDocuments = new ArrayList<>();
        //由於合併時將所有圖片都存在第一個doc裏面,所以定義方便圖片傳值
        CustomXWPFDocument firstDoc = null;
        //判斷需要的參數
        Integer sex;
        Date playStartTime;
        List<Map<String, Object>> allZzmm = xsjbxxDao.getAllZzmm();
        String filePath = ResourceUtils.CLASSPATH_URL_PREFIX + "中山大學南方學院本科生學籍登記表" + ".docx";
        //期次我們先解析word模板 替換其中的佔位符
        int index = 0;
        Integer handleTime = 1;
        try {
            for (SrsJbzl srsJbzl : userList) {
                WordUtils wordUtils = new WordUtils();
                Map<String, Object> params = new HashMap<>();
                //從ftp下載圖片
                InputStream fileInputStream = CommonUtil.getFileInputStream(FTP_ADDRESS, FTP_PORT, FTP_USERNAME, FTP_PASSWORD, FTP_BASEPATH, srsJbzl.getXh());
                if (fileInputStream != null) {
                    Map<String, Object> header = new HashMap<>();
                    header.put("width", 100);
                    header.put("height", 150);
                    header.put("type", "jpg");
                    byte[] bytes = wordUtils.input2byte(fileInputStream);
//                    byte[] bytes1 = WordUtils.inputStream2ByteArray(fileInputStream, true);
                    header.put("content", bytes);
                    params.put("${headImage}", header);
                } else {
                    params.put("${headImage}", "");
                }
                params.put("${xm}", srsJbzl.getXm());
                params.put("${cym}", srsJbzl.getCym());
                params.put("${ywxm}", srsJbzl.getYwxm());
                params.put("${xh}", srsJbzl.getXh());
                params.put("${xb}", "1".equals(srsJbzl.getXbdm()) ? "男" : "2".equals(srsJbzl.getXbdm()) ? "女" : "");
               

# params.put("${jkzk}", srsJbzl.getSrsJkzkdm()==null?"":srsJbzl.getSrsJkzkdm().getJkzkmc());//健康狀況 params.put("${sznj}", srsJbzl.getSznj() == null ? "" : srsJbzl.getSznj().toString());//年級 params.put("${csrq}", srsJbzl.getCsrq() ==null ?"":sdf.format(srsJbzl.getCsrq()));//出生時間 params.put("${gj}", srsJbzl.getFesDqGjdm() == null ? "":srsJbzl.getFesDqGjdm().getGjmc());//國籍 params.put("${mz}", srsJbzl.getFesMzdm() == null ? "":srsJbzl.getFesMzdm().getMzmc());//民族 params.put("${hf}", srsJbzl.getFesHyzkdm() == null?"":srsJbzl.getFesHyzkdm().getHyzkmc());//婚否 params.put("${cc}", srsJbzl.getCc());//層次 //to String zzmmc = ""; for (Map<String, Object> stringObjectMap : allZzmm) { if((Long)stringObjectMap.get("ID")==srsJbzl.getZzmmdm().getId().longValue()){ zzmmc = (String)stringObjectMap.get("ZZMMMC"); } } params.put("${zzmm}", zzmmc);//政治面貌 params.put("${jtcs}", srsJbzl.getSrsJtcsdm()==null?"":srsJbzl.getSrsJtcsdm().getJtcsMc());//家庭出身 params.put("${jg}", srsJbzl.getJgFesDqXsdm()==null?"":srsJbzl.getJgFesDqXsdm().getXsmc());//籍貫 params.put("${csd}", srsJbzl.getHkszd());//出生地 params.put("${lxdh}", srsJbzl.getYxlxdh());//聯繫電話 params.put("${sfzh}", srsJbzl.getSfzjh());//身份證號 params.put("${email}", srsJbzl.getEmail());//E-mail params.put("${yxmc}", srsJbzl.getFesYx()==null?"":srsJbzl.getFesYx().getYxmc());//系別 params.put("${zymc}", srsJbzl.getFesZy()==null?"":srsJbzl.getFesZy().getZymc());//專業 params.put("${rtsjdd}", srsJbzl.getRtsjdd());//何時何地加入共青團 params.put("${rdsjdd}", srsJbzl.getRdsjdd());//何時何地加入何黨派 params.put("${txmxdz}", srsJbzl.getTxmxdz());//通訊地址 params.put("${hjdqdm1}", srsJbzl.getHjdqdm());//郵編 params.put("${hjdqdm2}", srsJbzl.getHjdqdm());//郵編 params.put("${jtzz}", srsJbzl.getJtzz());//家庭地址 params.put("${jtdh}", srsJbzl.getJtdh());//家庭電話 params.put("${chcddz}", srsJbzl.getChcddz());//乘火車到達站 params.put("${rxsj}", srsJbzl.getRxny()==null?"":sdf.format(srsJbzl.getRxny()));//入學時間 params.put("${rxfs}",srsJbzl.getSrsRxfsdm()==null?"":srsJbzl.getSrsRxfsdm().getRxfsmc());//入學方式 params.put("${rxqxl}", srsJbzl.getFesRxsfdm()==null?"":srsJbzl.getFesRxsfdm().getRxsfmc());//入學前學歷 //查宿舍信息 String shByXsid = xsjbxxDao.getShByXsid(srsJbzl.getId()); params.put("${xxss}", shByXsid);//學校宿舍 params.put("${hq}", srsJbzl.getYnHq()!=null&&srsJbzl.getYnHq() ? "是" : "否");//是否華僑 params.put("${ga}", srsJbzl.getYnHq()!=null&&srsJbzl.getYnGas() ? "是" : "否");//是否港澳生 params.put("${qj}", srsJbzl.getYnHq()!=null&&srsJbzl.getYnQj() ? "是" : "否");//是否僑眷 List<SrsGzjlzl> srsGzjlzls = gzjlzlDao.listByMySelf(srsJbzl); List<SrsJtzl> srsJtzls = jtzlDao.listByMySelf(srsJbzl); if(srsGzjlzls!=null && srsGzjlzls.size()>0){ for (int i=0; i<srsGzjlzls.size() ;i++) { if (i==3) break;//個人簡歷不超過3條 String qsrq = sdf.format(srsGzjlzls.get(i).getQsrq()); String jsrq = sdf.format(srsGzjlzls.get(i).getQsrq()); params.put("${qzny"+(i+1)+"}", qsrq+"到"+jsrq); params.put("${szdw"+(i+1)+"}", srsGzjlzls.get(i).getSzdw()); params.put("${zw"+(i+1)+"}", srsGzjlzls.get(i).getZw()); params.put("${zmr"+(i+1)+"}",srsGzjlzls.get(i).getZmr()); } } if(srsJtzls!=null && srsJtzls.size()>0){ for (int i=0; i<srsJtzls.size() ;i++) { if (i==2) break;//監護人不超過2條 params.put("${cyxm"+(i+1)+"}", srsJtzls.get(i).getCyxm()); params.put("${ybrgx"+(i+1)+"}", srsJtzls.get(i).getYbrgx()); params.put("${sflx"+(i+1)+"}", srsJtzls.get(i).getSflx()); params.put("${cysrzh"+(i+1)+"}",srsJtzls.get(i).getCysrzh()); params.put("${lxdh"+(i+1)+"}",srsJtzls.get(i).getLxdh()); } } //學籍異動日誌 Page page = new Page(); page.setCurrent(0); page.setPageSize(100); SrsRecord srsRecord = new SrsRecord(); srsRecord.setJbzl(srsJbzl); List<SrsRecord> srsRecordlist = srsRecordService.list(srsRecord, page); if(srsRecordlist!=null && srsRecordlist.size()>0){ for (int i=0; i<srsRecordlist.size() ;i++) { if (i==3) break;//最多隻顯示三條 params.put("${ctime"+(i+1)+"}", srsRecordlist.get(i).getCtime()!=null?sdf.format(srsRecordlist.get(i).getCtime()):""); params.put("${ydlxName"+(i+1)+"}", srsRecordlist.get(i).getYdlxName()); params.put("${reason"+(i+1)+"}", srsRecordlist.get(i).getReason()); } } params.put("${jcqk}", srsJbzl.getJcqk());//何時何地何原因受過何種獎勵或處分 params.put("${ahtc}", srsJbzl.getAhtc());//有何愛好與特長 params.put("${syxwrq}", srsJbzl.getSyxwrq()!=null?sdf.format(srsJbzl.getSyxwrq()):"");//學位證書時間 params.put("${sjbysj}",srsJbzl.getSjbysj()!=null?sdf.format(srsJbzl.getSjbysj()):"");//畢業證書時間 params.put("${xwzh}", srsJbzl.getXwzh());//學位證書編號 params.put("${byzsh}", srsJbzl.getByzsh());//畢業證書編號

                //返回一個新的xwpfDocument對象
                File file = ResourceUtils.getFile(filePath);
                InputStream is = new FileInputStream(file);
                CustomXWPFDocument doc = new CustomXWPFDocument(is);
                if (index == 0 || (index != 1 && index % 49 == 1)) {
                    firstDoc = doc;
                }
                wordUtils.replaceInPara(firstDoc, doc, params);
                wordUtils.replaceInTable(firstDoc, doc, params, null);
                xwpfDocuments.add(doc);
                is.close();
                //每50條輸出一次
                if (userList.size() > 50 && index != 0 && (index == userList.size() - 1 || index % 49 == 0)) {
                    File files = new File(filesPath);
                    if (!files.exists()) {
                        files.mkdirs();
                    }
                    //這樣第一步將所有word內容替換之後生成多個   xwpfDocument
                    //現在將多個xwpfDocument 進行合併 追加 生成word文件
                    CustomXWPFDocument xwpfDocument = xwpfDocuments.get(0);
                    for (int i = 0; i < xwpfDocuments.size(); i++) {
                        //每次的追加爲了避免樣式和格式混亂 加上分頁符
                        //當是只有一條數據的時候 直接輸出
                        if (i == 0) {
                            xwpfDocument = xwpfDocuments.get(0);
                            continue;
                        } else {
                            //當存在多條時候
                            xwpfDocument = WordUtils.mergeWord(xwpfDocument, xwpfDocuments.get(i));
                        }
                    }
                    //合併之後返回XWPFDocument對象 寫出就可以了
                    String fileName = wordName + handleTime + ".docx";
                    FileOutputStream fout = new FileOutputStream(filesPath + "/" + new File(fileName));
                    xwpfDocument.write(fout);
                    fout.close();
                    handleTime++;
                    xwpfDocuments.clear();
                }
                index++;
            }
            if (userList.size() > 50) {
                createZipPath(filesPath, response, "中山大學南方學院本科生學籍登記表");
                File files = new File(filesPath);
                if (files.exists()) {
                    delFolder(files.getPath());
                }
            } else {
                if (xwpfDocuments.size() > 0) {
                    //這樣第一步將所有word內容替換之後生成多個   xwpfDocument
                    //現在將多個xwpfDocument 進行合併 追加 生成word文件
                    CustomXWPFDocument xwpfDocument = xwpfDocuments.get(0);
                    for (int i = 0; i < xwpfDocuments.size(); i++) {
                        //每次的追加爲了避免樣式和格式混亂 加上分頁符
                        //當是只有一條數據的時候 直接輸出
                        if (i == 0) {
                            xwpfDocument = xwpfDocuments.get(0);
                            continue;
                        } else {
                            //當存在多條時候
                            xwpfDocument = WordUtils.mergeWord(xwpfDocument, xwpfDocuments.get(i));
                        }
                    }
                    //合併之後返回XWPFDocument對象 寫出就可以了
                    OutputStream os = response.getOutputStream();
                    String fileName = new String((wordName + ".docx").getBytes("UTF-8"), "iso-8859-1");
                    response.setHeader("Content-disposition", "attachment; filename=" + fileName);
                    xwpfDocument.write(os);
                    os.close();
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

最後查看效果圖

在這裏插入圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章