Win a copy of The Journey To Enterprise Agility this week in the Agile and Other Processes forum! And see the welcome thread for 20% off.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Junilu Lacar
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Ganesh Patekar
  • Tim Moores
  • Pete Letkeman
  • Stephan van Hulst
Bartenders:
  • Carey Brown
  • Tim Holloway
  • Joe Ess

multipart/form-data  RSS feed

 
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
 
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();
}
}
}
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!