Win a copy of Practical SVG this week in the HTML/CSS/JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

multipart/form-data

 
himanshu patel
Ranch Hand
Posts: 205
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,

Can anybody give me example how to get parameters when form type is
multipart/form-data?

I am using org.apache.commons.fileupload API for file uploading.

Here is my code for file uploading.

FileUpload fileUploader = new FileUpload();

List fileItems = fileUploader.parseRequest(request);

Iterator i = fileItems.iterator();

while (i.hasNext()) {

FileItem item = (FileItem) i.next();
String fileName = "";
if (!item.isFormField()) {
String ufilePath = item.getName();
if (ufilePath.indexOf(".csv") < 0) {
errorString = "The type of the file being imported is not valid.
Please select a CSV file to import";
}
ufilePath = ufilePath.replace('\\','/');
StringTokenizer st = new StringTokenizer(ufilePath, "/");
while (st.hasMoreTokens()) {
fileName += st.nextToken();
}
item.write(FILE_PATH + "/" + fileName);
}
}

Apart from File uploading i want to get parameters which is a part of
fom data.

How do i achieve String action= request.getParameter("action") ?

I came across third party package which straight away offers getParameter()
method to retrieve form data. But i just need one form parameters so i have to stick with apache fileupload package.

Some where I have read that we can get form data using readHeaders() from org.apache.commons.MultipartStream class but do not know how exactly i should use.

Can anybody help me.

Thanks
 
Rovas Kram
Ranch Hand
Posts: 135
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've used com.oreilly.servlet.multipart.MultipartParser. This produces an iterator with objects of type FilePart or ParamPart.
 
himanshu patel
Ranch Hand
Posts: 205
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I got the solutions. Posting the modified code here so that other can get benefit.

String fileName = "";
String actionVal = "";

FileUpload fileUploader = new FileUpload();

List fileItems = fileUploader.parseRequest(request);

Iterator i = fileItems.iterator();

while (i.hasNext()) {

FileItem item = (FileItem) i.next();

if (!item.isFormField()) {
String ufilePath = item.getName();
if (ufilePath.indexOf(".csv") < 0) {
errorString = "The type of the file being imported is not valid. Please select a CSV file to import";
}
ufilePath = ufilePath.replace('\\','/');
StringTokenizer st = new StringTokenizer(ufilePath, "/");
while (st.hasMoreTokens()) {
fileName += st.nextToken();
}

item.write(FILE_PATH + "/" + fileName);

} else {
/* Get form fields value */
/* Since form type is "multipart/form-data", we can not get
form field value through usual request.getParameter() method.
Hence get it from FileItem.
*/

String name = item.getFieldName();

if ("action".equals(name)) {
actionVal = item.getString();
}
}
}
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!