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?

HaveNoDisplayName
  • 8,291
  • 106
  • 37
  • 47
Syed Rafi
  • 825
  • 2
  • 12
  • 35
  • 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. – Cedric Reichenbach Oct 05 '15 at 11:21

1 Answers1

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.
Community
  • 1
  • 1
David Turner
  • 316
  • 2
  • 8