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();
and it gives me list of file submitted through client side which absolutely work fine.
ServletFileUpload servletFileUpload = new ServletFileUpload(fileItemFactory);
List fileItems = servletFileUpload.parseRequest(request);
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.