This week's book giveaway is in the Kotlin forum.
We're giving away four copies of Kotlin for Android App Development and have Peter Sommerhoff on-line!
See this thread for details.
Win a copy of Kotlin for Android App Development this week in the Kotlin forum!
  • 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:
  • Campbell Ritchie
  • Liutauras Vilda
  • Devaka Cooray
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Junilu Lacar
  • Paul Clapham
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • salvin francis
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Frits Walraven
  • Ganesh Patekar

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();
}
}
}
 
All of the world's problems can be solved in a garden - Geoff Lawton. Tiny ad:
RavenDB is an Open Source NoSQL Database that’s fully transactional (ACID) across your database
https://coderanch.com/t/704633/RavenDB-Open-Source-NoSQL-Database
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!