六狼论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

新浪微博账号登陆

只需一步,快速开始

搜索
查看: 52|回复: 0

java读写RTF格式文件

[复制链接]

升级  50%

5

主题

5

主题

5

主题

童生

Rank: 1

积分
25
 楼主| 发表于 2013-1-15 02:47:35 | 显示全部楼层 |阅读模式
在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 {}}
您需要登录后才可以回帖 登录 | 立即注册 新浪微博账号登陆

本版积分规则

快速回复 返回顶部 返回列表