0

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?

Bharat
  • 943
  • 7
  • 9

1 Answers1

1

perhaps I'm not getting you properly. It seems like what you want is to create a servlet with the same mapping that handles different kinds of requests? am I right? if this is correct why don't you just change the content-type depending on your header in the database.

            response.setContentType("image/jpg");
            response.setContentType("text/html");

and then depending on what you want to serve you can use for images or files: for html:

  response.setContentType("text/html");   
  PrintWriter out = res.getWriter();  
  out.println("<html>....</html>");
  out.close();

for images:

    ServletOutputStream stream = null;
    BufferedInputStream buf = null;
    try{

            stream = response.getOutputStream();
            File mp3 = new File("path/tofile ");

            if(request.getSession().getAttribute( "path" )!=null){      
                 mp3 = new File(request.getSession().getAttribute( "path" ).toString());
                 request.getSession().setAttribute("path", null);
            } 

            response.setContentType("image/jpg");        
            response.setContentLength( (int) mp3.length() );

            FileInputStream input = new FileInputStream(mp3);
            buf = new BufferedInputStream(input);
            int readBytes = 0;

            while((readBytes = buf.read()) != -1)
               stream.write(readBytes);

   } catch (IOException ioe){       
      throw new ServletException(ioe.getMessage());           
   } finally {
       if(stream != null)
           stream.close();
       if(buf != null)
           buf.close();
   }   
PbxMan
  • 7,525
  • 1
  • 36
  • 40
  • Thanks for your reply. I am not looking for a servlet which handles different types of content types but rather a mix of content types. Yours example is rightly sets the content type to image/png if I wanted to display only image, but my page is going to contain multiple mime parts say text/html followed by image/png. Below is sample response header the servlet should return – Bharat Oct 09 '12 at 11:01
  • -------_=_NextPart_001_01CDA60B.AB4C0472 Content-Type: multipart/alternative; boundary="----_=_NextPart_002_01CDA60B.AB4C0472" ------_=_NextPart_002_01CDA60B.AB4C0472 Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: quoted-printable the sample contains multipart/alternative; and text/plain; content-type. similarly i am looking for displying multiple text/html and image/png – Bharat Oct 09 '12 at 11:02
  • Just to add..the servlet should return a response similar to the most voted reponse in http://stackoverflow.com/questions/1806228/browser-support-of-multipart-responses – Bharat Oct 09 '12 at 11:04