I am trying to display mulitple contents types (text/html and image/png inline images) in the same page. I have image content and text/html content stored in our disk/database with all the header information (basically assume that i can read the contents of text/html and image using say InputStream). I would like to create an HttpResponse containing both text/html part and inline images part. I came across some references to HttpClient/MultipartEntity. So i tried a sample code to display an image (saved in my disk) using MultipartEntity but all i see in the page is gibberish. jars i am referencing in my build path are apache-mime4j-0.6.jar, httpcore-4.0.1.jar, httpmime-4.0.jar. I am using apache tomacat server. And below is the sample code.
import java.io.*;
import java.nio.charset.Charset;
import javax.servlet.*;
import javax.servlet.http.*;
import org.apache.http.entity.mime.HttpMultipartMode;
import org.apache.http.entity.mime.MultipartEntity;
import org.apache.http.entity.mime.content.InputStreamBody;
public class MyHelloWorldServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws IOException, ServletException
{
ServletOutputStream out = response.getOutputStream();
MultipartEntity entity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE,null,Charset.forName("UTF-8"));
//File file = new File("C:\\Users\\Public\\Pictures\\Sample Pictures\\test.msg");
File file = new File("C:\\Users\\Public\\Pictures\\Sample Pictures\\Chrysanthemum.jpg");
InputStream in1 = new FileInputStream(file);
InputStreamBody fileBody1 = new InputStreamBody(new FileInputStream(file), "Chrysanthemum.jpg");
entity.addPart("part1", fileBody1);
entity.writeTo(out);
}
}
Also can someone please let me know if in a similar way is it possible to add parts of multiple content type and display?