• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

File upload in struts

 
Ranch Hand
Posts: 155
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello All,

If I put follwing code in JSP , It works fine and uploads the file

boolean isMultipart = FileUpload.isMultipartContent(request);
System.out.println("MultiPart is "+isMultipart);
DiskFileUpload upload = new DiskFileUpload();
// Set upload parameters
upload.setSizeMax(1000000);
List /* FileItem */ items = upload.parseRequest(request);
System.out.println("items.size =" + items.size());
Iterator itr = items.iterator();
while(itr.hasNext()) {
FileItem fi = fi= (FileItem)itr.next();

//Check if not form field so as to only handle the file inputs
//else condition handles the submit button input
if(!fi.isFormField()) {
System.out.println("\nNAME: "+fi.getName());
System.out.println("SIZE: "+fi.getSize());
//System.out.println(fi.getOutputStream().toString());
System.out.println("App.getrealpath() ="+ servlet.getServletContext().getRealPath("/") );
File fNew= new File(servlet.getServletContext().getRealPath("/"), fi.getName());
System.out.println(fNew.getAbsolutePath());
fi.write(fNew);
}
else {
System.out.println("Field ="+fi.getFieldName());
}
}

but If I put that code in Action Class , List /* FileItem */ items is empty set, i.e items.size is ZERO !!

Can anybody enlighten me on this ??

Thanks in Advance.
-Anand.
 
Ranch Hand
Posts: 4982
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
First, I wonder how you put the codes in JSP? When you submit the form in one JSP, you submit to another JSP?


but If I put that code in Action Class , List /* FileItem */ items is empty set, i.e items.size is ZERO !!

Can anybody enlighten me on this ??


In fact, if you use Struts, you dont need to use Jakarta Common FileUpload. Struts has a built-in class FormFilefor file upload:



Hope this help.

Nick
 
Ranch Hand
Posts: 1312
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Struts has a built-in class FormFile for file upload



import org.apache.struts.upload.FormFile;


And for more information about struts-upload, you can see more example in struts-upload.war in struts.zip (downloaded for apache.org)
 
Anand Gondhiya
Ranch Hand
Posts: 155
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for your posts !! They are very userful
 
reply
    Bookmark Topic Watch Topic
  • New Topic