支持htm,html转换成mht

/// <param name="srcpath">源路径</param>
/// <param name="aimPath">转换后的路径</param>

 /// <returns></returns>

private   string   mht_show( string  srcpath, string  aimPath)  
{
    
try
     
{
          
if(!Directory.Exists(srcpath))
         
{
              
string txtname=srcpath.Substring(srcpath.LastIndexOf(@""));
              
string[] pos=txtname.Split('.');
              aimPath
=aimPath.Substring(0,aimPath.LastIndexOf("/"));
    CDO.Message msg 
= new CDO.MessageClass();
    CDO.Configuration cfg 
= new CDO.ConfigurationClass();

    msg.Configuration 
= cfg;
    msg.CreateMHTMLBody(srcpath, CDO.CdoMHTMLFlags.cdoSuppressAll, 
"""");

    msg.GetStream().SaveToFile(aimPath
+"/"+pos[0]+".mht", ADODB.SaveOptionsEnum.adSaveCreateOverWrite);
    
return aimPath+pos[0]+".mht";
    }

        }

   
catch (Exception e)
  
{
     MessageBox.Show (e.ToString());
  }

  
return null;
}
 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 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、付费专栏及课程。

余额充值