1

I am facing some weird issue with my code. I am using org.apache.http.entity.mime.MultipartEntity class to submit file entity to server so that I can upload the file. But when I am trying to add another entity/parameter, its value is not been able to capture through HttpServletRequest. Below is my client side working code from where I am sending the successful file upload request:

public class TestClass 
{
    public static void main(String args[]) throws ConfigurationException, ParseException, ClientProtocolException, IOException
    {
        HttpClient client = new DefaultHttpClient();
        client.getParams().setParameter(CoreProtocolPNames.PROTOCOL_VERSION, HttpVersion.HTTP_1_1);     
        HttpPost    post   = new HttpPost("http://localhost:9090/HostImages/ImageUploaderServlet");
        MultipartEntity entity = new MultipartEntity( HttpMultipartMode.BROWSER_COMPATIBLE );
        entity.addPart( "file", new FileBody(new File("D:/tempImage/cat_image.jpg") ));
        post.setEntity(entity);
        String response = EntityUtils.toString( client.execute( post ).getEntity(), "UTF-8" );
        client.getConnectionManager().shutdown();
    }
}

On the server side, i.e. inside ImageUploaderServlet I was parsing the request object of HttpServletRequest as follows:

FileItemFactory fileItemFactory = new DiskFileItemFactory();
ServletFileUpload servletFileUpload = new ServletFileUpload(fileItemFactory); List fileItems = servletFileUpload.parseRequest(request);
and it gives me list of file submitted through client side which absolutely work fine.

Problem arises when I am trying to add another entity either by using MultipartEntity or by HttpParams I am unable to get its value at server end. I have gone through this question already posted but no help. (I tried to add another entity as StringBody as below:)

entity.addPart("flag", new StringBody("true"));

I want to add some additional parameters at client side so that I can use that at server side to serve my purpose. Additional parameter could be either String, int or byte whatsoever. I dont know where is the exact problem lies, client side or server side ! Kindly help.

Community
  • 1
  • 1
Aman Gupta
  • 5,548
  • 10
  • 52
  • 88
  • 1
    Maybe this thread can help out: http://stackoverflow.com/questions/3337056/convenient-way-to-parse-incoming-multipart-form-data-parameters-in-a-servlet – Martin May 02 '13 at 14:32
  • Hi Martin.. thanks for providing useful link. just add your comment as answer so that I can accept this. – Aman Gupta May 06 '13 at 07:01

1 Answers1

0

Multipart Requests need special attention. Here is a useful link to a thread that can help to understand how to treat these cases:

Convenient way to parse incoming multipart/form-data parameters in a Servlet

Regards

Community
  • 1
  • 1
Martin
  • 3,018
  • 1
  • 26
  • 45
  • 1
    At the client side, order of adding entity is important, because it got captured in that order only at server side. – Aman Gupta May 06 '13 at 12:39