|
|
在linux环境下,要用java写doc格式,poi有很多功能缺陷。使用RTF格式来代替doc格式,确实也是个不错的解决办法,毕竟MS Word可以打开RTF格式文件
package com.qiuxy.servlet;import java.io.File;import java.io.IOException;import java.io.OutputStream;import javax.servlet.ServletException;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import com.lowagie.text.Document;import com.lowagie.text.DocumentException;import com.lowagie.text.Image;import com.lowagie.text.Paragraph;import com.lowagie.text.rtf.RtfWriter2;/** * 使用itext写RTF格式简单例子 * */public class ExportRtfServlet extends HttpServlet {public void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {//设置浏览器显示的内容类型为Zipresponse.setContentType("application/rtf");//设置内容作为附件下载,并且名字为:export.zip response.setHeader("Content-Disposition", "attachment; filename= export.rtf"); //客户端发过来的图片名称String[] picNames = request.getParameterValues("pics");//创建文档 Document doc = new Document(); //获得输出流 OutputStream os = response.getOutputStream(); //打开文档 RtfWriter2.getInstance(doc, os); doc.open(); for (int i = 0; i < picNames.length; ++i) { String imageName = picNames[i]; String imagePath = getServletContext() .getRealPath("images" + File.separator + imageName);try {//创建一个段落Paragraph p = new Paragraph();//段落中添加图片p.add(Image.getInstance(imagePath));//段落中添加文字p.add(imageName);//将段落添加到文档中doc.add(p);doc.add(new Paragraph(""));} catch (DocumentException e) {e.printStackTrace();} } //关闭文档 doc.close(); os.flush(); os.close();}public ExportRtfServlet() {super();}public void destroy() {super.destroy(); }public void doPost(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {doGet(request, response);}public void init() throws ServletException {}} |
|