java mht 转换 html_Word单网页mht文件,汉字被html转义解决办法

做项目要生成Word文档时,一般都是先用Word把里面的内容写好,然后另存为mht文件,得到一个网页文件,这样程序就可以自动替换网页中得文件,然后让大家下载。

下载这里就不说了,很简单,如下处理一下就可以了。

String filename = new String("中文汉字.doc".getBytes(), "ISO8859-1");

response.setContentType("application/octet-stream");

response.setHeader("Content-Disposition", "attachment; filename=\"" + filename + "\"");

response.setCharacterEncoding("GB2312");

在把word另存为mht时,打开mht发现里面的汉字全部变成了XXXX;这样的格式,被html转义了,正常人无法看懂。所以,我的解决办法是先把html转义字符转成汉字,然后把charset从us-ascii全部替换成gb2312,就哦了。

下面一段代码可以把html转义字符转成汉字。然后你打开文件,全局替换一下us-ascii成gb2312。

import java.io.BufferedReader;

import java.io.BufferedWriter;

import java.io.FileNotFoundException;

import java.io.FileReader;

import java.io.FileWriter;

import java.io.IOException;

import java.util.regex.Matcher;

import java.util.regex.Pattern;

public class Main {

public static void main(String[] args) {

try {

FileReader mhtReader = new FileReader("d:\\源文件.mht");

FileWriter mhtWriter = new FileWriter("d:\\生成文件.doc");

BufferedReader bufReader = new BufferedReader(mhtReader);

BufferedWriter bufWriter = new BufferedWriter(mhtWriter);

String line = bufReader.readLine();

String lines = "";

//中

Pattern pattern = Pattern.compile("(\\d+);");

while (line != null) {

if (line.endsWith("=")) {

lines += line.substring(0, line.length()-1);

line = bufReader.readLine();

continue;

} else {

if (!"".equals(lines)) {

lines += line;

line = lines;

lines = "";

}

Matcher matcher = pattern.matcher(line);

while (matcher.find()) {

char c = (char) Integer.parseInt(matcher.group(1));

line = line.replaceFirst(matcher.group(), String.valueOf(c));

}

bufWriter.write(line + "\r\n");

line = bufReader.readLine();

}

}

bufWriter.flush();

bufWriter.close();

bufReader.close();

} catch (FileNotFoundException e) {

e.printStackTrace();

} catch (IOException e) {

e.printStackTrace();

}

}

}

最后,打开转化好的文件,在的下一行,插入Print,这样用word打开时,就是视图模式了。

到这里就完美啦!

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要将MHT文件转换HTML文件,可以使用以下代码: ```java import java.io.*; public class Mht2Html { public static void main(String[] args) throws Exception { String mhtFile = "path/to/mht/file.mht"; String htmlFile = "path/to/html/file.html"; String line; BufferedReader reader = new BufferedReader(new FileReader(mhtFile)); BufferedWriter writer = new BufferedWriter(new FileWriter(htmlFile)); while ((line = reader.readLine()) != null) { if (line.startsWith("Content-Type:")) { String contentType = line.substring("Content-Type:".length()).trim(); if (contentType.equalsIgnoreCase("text/html")) { writer.write("<html>\n"); writer.write("<head>\n"); writer.write("<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\">\n"); writer.write("</head>\n"); writer.write("<body>\n"); } } else if (line.startsWith("Content-Transfer-Encoding:")) { String encoding = line.substring("Content-Transfer-Encoding:".length()).trim(); if (encoding.equalsIgnoreCase("base64")) { reader.readLine(); // skip empty line String base64 = ""; while (!(line = reader.readLine()).equals("==")) { base64 += line; } byte[] bytes = Base64.getDecoder().decode(base64); String text = new String(bytes, "UTF-8"); writer.write(text); } } if (line.equals("") || line.equals("--")) { writer.write("</body>\n"); writer.write("</html>\n"); } } reader.close(); writer.close(); } } ``` 这个代码会解析MHT文件,将其HTML部分提取出来,并转换HTML文件。你需要将代码的`path/to/mht/file.mht`和`path/to/html/file.html`替换为你自己的文件路径。需要注意的是,这个代码使用了`java.util.Base64`类,如果你的Java版本低于8,需要使用其他的Base64库替换。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值