5

如何从mht文件中提取图片?图片src=“cid:3f352854-a9d9-4ce4-8a18-4db64479347d“,最...

 2 years ago
source link: https://www.oschina.net/question/3830635_2324767
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
如何从mht文件中提取图片?图片src=“cid:3f352854-a9d9-4ce4-8a18-4db64479347d“,最好用python或者java - OSCHINA - 中文开源技术交流社区 ArchitectureMaster
昨天 11:48

mht是一种类似邮件.eml格式的文件存储方式。mht文件是把html打包成一个文件包含Html里的资源,而这个base64文本的位置 是有规则的。

规则很简单,你看到img里src如果是cid开头的那后面的---MultipartBoundary段中一定会有这个cid对应的guid,也就是Content-ID,而这个Content-ID对应的Content-Type多数一定是Image类型。

如果题主只是解析出图片那非常简单,你有正则表达式把MultipartBoundary截取出来,然后再把里面Content-Type为Image图片,再用正则表达式把尾部的base64截出来就好了。

------MultipartBoundary与下一个空行之间就是代表了一个图片的全部内容。首先你要做的是把这一段之间的内容用正则截取出来,当然你也可以用其它字符串查找截取的类库这个很容易。

第二步就是把这个字符串转为byte[]字节数组,再然后使用Stream写入到文件就可以了。


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK