Syed Rafi
Syed Rafi

Reputation: 915

Convert .html files to .mhtml using Java API

I have some generated .html reports in a folder and want to convert multiple .html, .css, .js and image files into one report.mhtml file so that single file can be accessed as a web service.

Is there any Java API to convert the folder of .html files to a single .mhtml file?

Upvotes: 1

Views: 2880

Answers (1)

David Turner
David Turner

Reputation: 316

I was investigating the reverse (unpacking an MHTML/EML to files) and while there didn't seem to be a simple Java-based utility to do this, I found the Apache Mime4J libraries to be very useful (and easier than JavaMail).

You can find the code I shared here: How to read or parse MHTML (.mht) files in java

For your case, to build an MHTML, if you can't find anything simpler, approach could be:

  1. Create a Message object which has a Multipart body.
  2. Read all files in a folder using Streams, append these as BodyParts of the Multipart with their mime-type (Mime4j includes a Base64 stream encoder/decoder).
  3. Ensure the references in the html page point to the necessary body parts (may be able to embed their original filename as reference?).
  4. Write the Message object to mht file or a response stream.

Upvotes: 2

Related Questions