how to get string parameter from request when using multipart/form-data?
24,326
Solution 1
Its because when you have form enctype="multipart/form-data"
. You can not get other form fields by using request.getParameter("paramnName");
. It will always give you NULL.
You have to use FormItem
's isFormField()
to check if its regular field or file.
Example:
try {
ServletFileUpload upload = new ServletFileUpload();
response.setContentType("text/plain");
FileItemIterator iterator = upload.getItemIterator(request);
while (iterator.hasNext()) {
FileItemStream item = iterator.next();
InputStream stream = item.openStream();
if (item.isFormField()) {
System.out.println("Got a form field: " + item.getFieldName() + " " +item);
} else{
System.out.println("Got an uploaded file: " + item.getFieldName() +
", name = " + item.getName());
int len;
byte[] buffer = new byte[8192];
while ((len = stream.read(buffer, 0, buffer.length)) != -1) {
response.getOutputStream().write(buffer, 0, len);
}
}
}
} catch (FileUploadException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Solution 2
Adding onto the answer and addressing the question @Sayo Oladeji
To get the value of an input field you can use the following:
System.out.println("Got a form field: " + item.getFieldName() + " " + Streams.asString(stream));
Author by
Hanaa
Updated on August 14, 2020Comments
-
Hanaa over 3 years
i make html page to upload image with text box which has description form it . i have use multipart/form-data ; in the dopost at servlet i get file using ServletFileUpload upload = new ServletFileUpload();
to get string parameter i used request.getparameter(); but it always give me NULL ??? HOw can i get it ??
html ::
<form name="filesForm" action="/upload" method="post" enctype="multipart/form-data"> File : <input type="file" name="file"> <textarea name = "description" rows = "4" cols = "30">Enter comments here.</textarea> <input type="submit" name="Submit" value="Upload File">
at servlet :
ServletFileUpload upload = new ServletFileUpload(); upload.setSizeMax(500000000); FileItemIterator iterator = null; try { iterator = upload.getItemIterator(req); } catch (FileUploadException e) { // TODO Auto-generated catch block e.printStackTrace(); } // to handle contents or // instances of request. FileItemStream item = null; try { item = iterator.next(); } catch (FileUploadException e) { // TODO Auto-generated catch block e.printStackTrace(); } // get access each item on iterator. java.io.InputStream in = item.openStream(); // allows to read the items contents. Blob imageBlob = new Blob(IOUtils.toByteArray(in)); // store each item on Blob // object after converting them to Bytes. /*………. Note: we are concerned on uploading images files. So the type of files is either (.jpg or gif) ………. */ PersistenceManager pm = PMF.get().getPersistenceManager(); String counter="1"; ServletOutputStream outt = resp.getOutputStream(); //match incoming request Content type and invoke appropriate method for handel request
-
Hanaa almost 12 yearsHOW ??? can you give me an example ???
-
Hanaa almost 12 yearsi try it but now it give me exception
-
Hanaa almost 12 yearsjava.lang.NoClassDefFoundError: java.rmi.server.UID is a restricted class. Please see the Google App Engine developer's guide for more details. at com.google.appengine.tools.development.agent.runtime.Runtime.reject(Runtime.java:51) at org.apache.commons.fileupload.disk.DiskFileItem.<clinit>(DiskFileItem.java:103) at org.apache.commons.fileupload.disk.DiskFileItemFactory.createItem(DiskFileItemFactory.java:196) at org.apache.commons.fileupload.FileUploadBase.parseRequest(FileUploadBase.java:358)
-
Hardik Mishra almost 12 yearsI guess you can not use
DiskFileItemFactory
withGAE
. Check this link stackoverflow.com/questions/9501527/… and also edited example -
Sayo Oladeji about 11 yearsThe line
System.out.println("Got a form field: " + item.getFieldName() + " " +item);
does not print the value of a corresponding form field. Please how do I get the value of a form field? -
Usman Mutawakil about 11 years@HardikMishra Your absolutely right. Just saved me several hours.
-
Hardik Mishra about 11 years@UsmanMutawakil: You are wel-come.