rtf乱码。。跪求解决。

a290196712 2012-03-07 06:02:15
package com.eetrust;

import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;




public class OperatorRTFCN {
/**
* 字符串转换为rtf编码
* @param content
* @return
*/
int inext = 0;// 用来判断中文 编码出现 第一次出现为0 第二次出现为1
public String strToRtf(String content) throws IOException {

try {
System.out.println("原始内容:\n"+content);
content=new String(content.getBytes(),"UTF-8");
System.out.println("新内容:\n"+content);
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
char[] digital = "0123456789ABCDEF".toCharArray();
StringBuffer sb = new StringBuffer("");
byte[] bs = null;
bs = content.getBytes();
int bit;
for (int i = 0; i < bs.length; i++) {
bit = (bs[i] & 0x0f0) >> 4;
sb.append("\\'");
sb.append(digital[bit]);
bit = bs[i] & 0x0f;
sb.append(digital[bit]);
}
return sb.toString();
}



/**
* 替换文档的可变部 *
* @param content
* 原来的
* @param markersign
* 标记符号
* @param replacecontent
* 替换的内容用replacecontent替换markersign
* @return
* @throws IOException
*/
public String replaceRTF(String content, String markersign,
String replacecontent) throws IOException {
String rc = strToRtf(replacecontent);
String target = "";
markersign = "$" + markersign + "$";
target = content.replace(markersign, rc);
return target;
}

/*
* 替换模板 *
* @param inputPath
*
* @param outPath
*
* @param data
*
* @return
*/
public void rgModel(String inputPath, String outPath, HashMap data) {
// TODO Auto-generated method stub
/* 字节形式读取模板文件内容,将结果转为字符串 */
String sourname = inputPath + "//" + "模板ZH.rtf";
String sourcecontent = "";
InputStream ins = null;
try {
ins = new FileInputStream(sourname);

byte[] b = new byte[16384]
int bytesRead = 0;
while (true) {
// bytesRead = ins.read(b, 0, 1024); // return final read
// bytes counts
bytesRead = ins.read(b, 0, 1024);
if (bytesRead == -1) {// end of InputStream
System.out.println("读取模板文件结束");
break;
}
sourcecontent += new String(b, 0, bytesRead);
System.out.println("b:\n"+b);

}
} catch (Exception e) {
e.printStackTrace();
}
/* 修改变化部分 */
String targetcontent = "";
String oldText = "";
Object newValue;
/* 结果输出保存到文件*/
try {
Iterator keys = data.keySet().iterator();
int keysfirst = 0;
while (keys.hasNext()) {
oldText = (String) keys.next();
newValue = data.get(oldText);
String newText = (String) newValue;
inext = 0;// add by wde 改为初始
if (keysfirst == 0) {
targetcontent = replaceRTF(sourcecontent, oldText,
newText);
keysfirst = 1;
} else {
targetcontent = replaceRTF(targetcontent, oldText,
newText);
keysfirst = 1;
}
}

FileWriter fw = new FileWriter(outPath, true);
PrintWriter out = new PrintWriter(fw);
if (targetcontent.equals("") || targetcontent == "") {
out.println(sourcecontent);
} else {
out.println(targetcontent);
}
out.close();
fw.close();
System.out.println(outPath + " 生成文件成功");
} catch (IOException e) {

e.printStackTrace();
}
}

public static void main(String[] args) {

SimpleDateFormat sdf = new java.text.SimpleDateFormat(
"yyyy-MM-dd HH:mm:ss");
Date current = new Date();
String targetname = sdf.format(current).substring(0, 4) + "年";
targetname += sdf.format(current).substring(5, 7) + "月";
targetname += sdf.format(current).substring(8, 10) + "日";
targetname += sdf.format(current).substring(11, 13) + "时";
targetname += sdf.format(current).substring(14, 16) + "分";
targetname += sdf.format(current).substring(17, 19) + "秒";
targetname += ".doc";
OperatorRTFCN oRTF = new OperatorRTFCN();

// *****************************************
// 利用HashMap读取数据库中的数据
HashMap map = new HashMap();

map.put("ACCTNAME", "xxx科技股份有限公司");
map.put("SN", "000000000000000000001");



// ******************************************
oRTF.rgModel("E://FileTest", "E://FileTest//" + targetname, map);

}
}


从网上看到的,在本地运行出打开生成的文件就是乱码。。
...全文
361 回复 打赏 收藏 转发到动态 举报
写回复
用AI写文章
回复
切换为时间正序
请发表友善的回复…
发表回复

51,409

社区成员

发帖
与我相关
我的任务
社区描述
Java相关技术讨论
javaspring bootspring cloud 技术论坛(原bbs)
社区管理员
  • Java相关社区
  • 小虚竹
  • 谙忆
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧