1.導出word
添加依賴
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>3.14</version>
</dependency>
實現層代碼
private void downloadWord(HttpServletResponse response, Resource resource) {
String title = resource.getTitle();
String text = resource.getContent();
try {
//word內容
String content="<html><body>" +
"<p style=\"text-align: center;\"><span style=\"font-family: 黑體, SimHei; font-size: 24px;\">"
+ title + "</span></p>" + text + "</body></html>";
byte b[] = content.getBytes("GBK"); //這里是必須要設置編碼的,不然導出中文就會亂碼。
ByteArrayInputStream bais = new ByteArrayInputStream(b);//將字節數組包裝到流中
/*
* 關鍵地方
* 生成word格式 */
POIFSFileSystem poifs = new POIFSFileSystem();
DirectoryEntry directory = poifs.getRoot();
DocumentEntry documentEntry = directory.createDocument("WordDocument", bais);
//輸出文件
// request.setCharacterEncoding("utf-8");
response.setContentType("application/msword");//導出word格式
response.addHeader("Content-Disposition", "attachment;filename=" +
new String(title.getBytes("GB2312"),"iso8859-1") + ".doc");
ServletOutputStream ostream = response.getOutputStream();
poifs.writeFilesystem(ostream);
bais.close();
ostream.close();
poifs.close();
}catch(Exception e){
e.printStackTrace();
}
}
2.導出pdf
添加依賴
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>itextpdf</artifactId>
<version>5.5.11</version>
</dependency>
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>itext-asian</artifactId>
<version>5.2.0</version>
</dependency>
<dependency>
<groupId>com.itextpdf.tool</groupId>
<artifactId>xmlworker</artifactId>
<version>5.5.8</version>
</dependency>
壓縮並導出實現代碼
private void downloadPdf(HttpServletResponse response, Resource resource) {
String content = resource.getContent();
content = MyUtil.handleConvertPdfContent(content);
String rootDir = System.getProperty("user.dir")+"/";
String pdfDir = rootDir+"temp/pdf/";
String pdfFileName = resource.getTitle()+".pdf";
try {
String path = pdfDir + pdfFileName;
File pdfFileDir = new File(pdfDir);
if (!pdfFileDir.exists()) {
pdfFileDir.mkdirs();
}
FileOutputStream pdfOut = new FileOutputStream(path);
// 第一步 創建文檔實例 文章pdf導出+壓縮包下載
Document document = new Document();
// 第二步 獲取PdfWriter實例 寫入文件到目錄 pdfDir + responseFileName
PdfWriter writer = PdfWriter.getInstance(document, pdfOut);
// 第三步 打開文檔
document.open();
//獲取 XMLWorkerHelper實例
XMLWorkerHelper work = XMLWorkerHelper.getInstance();
//解析html文件,創建pdf文檔
work.parseXHtml(writer, document, new ByteArrayInputStream(content.getBytes()));
// 第五部 操作完成后必須執行文檔關閉操作。
document.close();
writer.close();
pdfOut.close();
// 壓縮pdf文件
String zipDir = rootDir + "temp/zip/";
String zipPath = zipDir + resource.getTitle() + ".zip";
File zipFile = new File(zipPath);
File pdfFile1 = new File(path);
ZipUtil.zip(zipFile, false, pdfFile1);
// 將zip file寫入response流
//設置response內容
String zipFileName = resource.getTitle() + ".zip";
// 這里中文名字導致了下載報錯 暫不解決
String responseFileName = new String("download.zip".getBytes(), "utf-8");
response.reset(); // 非常重要
ServletOutputStream responseOutputStream = response.getOutputStream();
response.setContentType("application/octet-stream; charset=utf-8");
response.setHeader("Content-Disposition", "attachment; filename=" + responseFileName);
InputStream in = new FileInputStream(zipPath);
int len = 0;
byte[] buffer = new byte[1024];
while ((len = in.read(buffer)) > 0) {
responseOutputStream.write(buffer, 0, len);
}
in.close();
responseOutputStream.close();
} catch (Exception e) {
e.printStackTrace();
} finally {
FileUtil.delAllFile(rootDir + "/temp");
}
}
只做保存
源地址:https://www.cnblogs.com/guanxiaohe/p/14596592.html
