1

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?

1
  • For images, this will probably get quite complicated and nasty, as they needed to be encoded to base64 (string representation of binaries). Especially for large image files, your resulting html file would become large and probably have performance issues in some browsers. Commented Oct 5, 2015 at 11:21

1 Answer 1

2

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.
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.