Per, The servlet specification does not require support for so called multipart/form-data formatted requests and most servlet engines I know of do not provide support for it out of the box. You need to use a server side library capable of parsing multipart/form-data requests in order to be able to access individual parameters (parts)
http://jakarta.apache.org/commons/fileupload/ Hope this helps Oleg On Thu, 2005-02-24 at 17:48 +0100, Per Nyfelt wrote: > I have a servlet running in Tomcat 5 that i want to handle file transfers > (upload and download) of big files (>32 MB). However, I am unable to find a > way for HttpClient to set parameters properly for the servlet to read them. > > The servlet has a service() method defined with the following logic: > > public void service(HttpServletRequest request, HttpServletResponse response) > { > String requestType = request.getParameter("TYPE"); > String fileName= request.getParameter("FILE_NAME"); > > if ("upload".equals(requestType)) { > uploadDocument(fileName, request, response); > } else if ("download".equals(requestType)) { > downloadDocument(fileName, response); > } else { > sendParameterError(requestType, fileName); > } > } > > The issue is that requestType and fileName end up as null for all my attempts > to set parameters. > > Here's some examples of code I've tried for the upload part (I am using 3.0 > rc1): > > 1. This is the only one that works (i.e. putting the request parameters in > the > url): > > public void testUpload() { > File file = new File("test.zip"); > HttpClient client = new HttpClient(); > PostMethod httppost = new > PostMethod("http://localhost:8080/test/FiletServlet?TYPE=download&FILE_NAME=" > + file.getName()); > httppost.setRequestEntity(new InputStreamRequestEntity( > new FileInputStream(file), file.length())); > try { > client.executeMethod(httppost); > if (httppost.getStatusCode() == HttpStatus.SC_OK) { > System.out.println(httppost.getResponseBodyAsString()); > } else { > System.out.println("Unexpected failure: " + > httppost.getStatusLine().toString()); > } > } finally { > httppost.releaseConnection(); > } > } > > 2. This is what I would like to do: > public void testUpload2() { > File file = new File("test.zip"); > HttpClient client = new HttpClient(); > PostMethod httppost = new > PostMethod("http://localhost:8080/test/FileServlet"); > httppost.addParameter("TYPE", "upload"); > httppost.addParameter("FILE_NAME", file.getName()); > httppost.setRequestEntity(new InputStreamRequestEntity( > new FileInputStream(file), file.length())); > > try { > client.executeMethod(httppost); > > if (httppost.getStatusCode() == HttpStatus.SC_OK) { > System.out.println(httppost.getResponseBodyAsString()); > } else { > System.out.println("Unexpected failure: " + > httppost.getStatusLine().toString()); > } > } finally { > httppost.releaseConnection(); > } > } > > 3. I have also tried setting parameters in the HttpClient: > File file = new File("test.zip"); > HttpClient client = new HttpClient(); > client.getParams().setParameter("TYPE", "upload"); > client.getParams().setParameter("FILE_NAME", file.getName()); > > PostMethod httppost = new > PostMethod("http://localhost:8080/test/FileServlet"); > httppost.setRequestEntity(new InputStreamRequestEntity( > new FileInputStream(file), file.length())); > > try { > client.executeMethod(httppost); > ....etc... > > 4. and also tried using NameValuePair: > File file = new File("test.zip"); > HttpClient client = new HttpClient(); > PostMethod httppost = new > PostMethod("http://localhost:8080/test/FileServlet"); > NameValuePair type = new NameValuePair("TYPE", "upload"); > NameValuePair fileName = new NameValuePair("FILE_NAME", > file.getName()); > httppost.setRequestBody(new NameValuePair[] {type, fileName}); > > httppost.setRequestEntity(new InputStreamRequestEntity( > new FileInputStream(file), file.length())); > > try { > client.executeMethod(httppost); > ...etc..... > > But all variations on this theme given me null on both parameters on the > servlet side. Maybe I have misunderstood something but is there no way to set > request parameters one by one and sending them in the POST instead of > building up a GET-type string with all parameters passed in the constructor > for the PostMethod? > > Please advice. > > Best regards, > Per > > --------------------------------------------------------------------- > To unsubscribe, e-mail: [EMAIL PROTECTED] > For additional commands, e-mail: [EMAIL PROTECTED] > --------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
