借助webcat中的RTF2HTML将RTF转成HTML

使用webcat中的RTF2HTML类。
webcat下载地址:http://webcat.sourceforge.net/

代码


 
import org.apache.commons.lang.StringUtils;
 
import java.io.*;
import java.util.ArrayList;
import java.util.List;
 
public class RTFtoHTML {
 
    public static void main(String[] args) {
        convertRTFToHTML("G:\\RTF", "G:\\HTML");
    }
 
    /**
     * RTF转HTML
     * @param sourceFile 源文件夹(保存需要转换的rft文件)
     * @param targetFile 目标文件夹(保存转换后的html文件)
     */
    public static void convertRTFToHTML(String sourceFile, String targetFile) {
        List<String> fileUrls = getFiles(sourceFile);
        for (String fileUrl : fileUrls) {
            try {
                //调用RTF2HTML
                String rlt = new RTF2HTML().convertRTFToHTML(new File(fileUrl));
                if (StringUtils.isBlank(rlt)) {
                    break; 
                } 
                //解决中文乱码问题
                String context = new String(rlt.getBytes("UTF-8"));
                FileWriter fw;
                String[] fileSplit = fileUrl.split("\\\\");
                //拼接html文件名称
                String targetFileName = targetFile + "\\\\" + fileSplit[fileSplit.length - 1].split("\\.")[0] + ".html";
                File f = new File(targetFileName);
                try {
                    if(!f.exists()){
                        f.createNewFile();
                    }
                    fw = new FileWriter(f); 
                    BufferedWriter out = new BufferedWriter(fw);
                    //将转换好的文件写入
                    out.write(context, 0, context.length()-1); 
                    out.close();
                } catch (IOException e) {
                    e.printStackTrace(); 
                }
                FileWriter w = new FileWriter(targetFile);
                w.write(context);
                w.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
 
    /**
     * 获取某个目录下所有直接下级文件,不包括目录下的子目录的下的文件
     */
    public static List<String> getFiles(String path) {
        List<String> files = new ArrayList<>();
        File file = new File(path);
        File[] tempList = file.listFiles();
        if(tempList!=null) {
        	for (int i = 0; i < tempList.length; i++) {
        		if (tempList[i].isFile()) {
        			files.add(tempList[i].toString());
            }
        }
        	}else {
        		System.out.println("空");
        	}
        
        return files;
    }
}

资源:
https://download.csdn.net/download/qq_46432776/85057047

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

scblue_club

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值