aspose file tools*
The moose likes Struts and the fly likes how to implement file upload and download in struts2 Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "how to implement file upload and download in struts2" Watch "how to implement file upload and download in struts2" New topic
Author

how to implement file upload and download in struts2

sunil gupta
Greenhorn

Joined: May 07, 2008
Posts: 21
hello,

i want to implement file upload and download functioality in my application using struts2. i tried to use the examples from showcase and it seems to be working but they are static and i m not able to find some good example. if any body has implemented this functionality please share with me or post some good example.

I have few specific question.

1. when i m uploading a file what should be the path to store so that it can be downloaded also. should it be under tomcat or anywhere it can be saved.

2. when i am downloading a file it seems it should be under tomcat/webapp/project. that means it should be saved here only. i m not sure how will it work.

If any body have done it please help. it is very urjent.

thanks in advance.

Sunil
Shweta Baranwal
Greenhorn

Joined: Jun 19, 2008
Posts: 18
Hi Sunil,

To download file, the file should be present at the location where server is running because when any user will click on download button , the action present in server will be called and it will do whole processing .
I have done the same thing. In my application first of all i fetched the file from database and stored it in the location where tomcat server is running and then click of download button this this file will be downloaded.
Here is my code to save a file at server location :-

File file=new File("abc.jpg");
file.createNewFile();

this code will automatically create the file where server is running


hopefully this will help U.

Bilal A. Siddiqui
Ranch Hand

Joined: Sep 14, 2008
Posts: 53

Hi Sunil

I am using this code for file uploading.




You can find this code on the official web site of Struts 2 in action by manning publication.

and yes you have to save the file in tomcat itself, as per my knowledge.


Cheers!!! Bilal
Handsome Android||@bilal_speed||LinkedIn
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

File uploading is handled by the framework; there's no need to read an input stream:

http://struts.apache.org/2.x/docs/file-upload.html

Uploaded files can be put anywhere.

Downloading files may be handled by the "stream" result:

http://struts.apache.org/2.0.14/docs/stream-result.html

or written directly to the response.
Bilal A. Siddiqui
Ranch Hand

Joined: Sep 14, 2008
Posts: 53

thanks David.
 
 
subject: how to implement file upload and download in struts2