aspose file tools*
The moose likes Java in General and the fly likes Dowloading a file from a a struts 2 web application 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 » Java » Java in General
Bookmark "Dowloading a file from a a struts 2 web application" Watch "Dowloading a file from a a struts 2 web application" New topic
Author

Dowloading a file from a a struts 2 web application

lakshmanan anand
Greenhorn

Joined: Jan 13, 2009
Posts: 22
Hi
I wish the users of my webapp to download a file in c:\files space. I have written the following code to use stream result type to let the users download the file. But I get surprising errors. please help me resolve them

When I use


The struts xml fragment is


I still get the following error saying that the name inputStream ( i mean a getter with name inputStream does not exists) It is there in the action class as seen above


I am stuck with this problem for 3 hours and I am not able to solve this. How can struts cannot find whats already in there in its action fragment ??

[edit]Insert line breaks in the code, otherwise it is too wide for my screen. CR[/edit]


http://luckydev07.blogspot.com
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

I'm confused; you're talking about downloading in the subject and using a stream result, but talking about uploading later.

Yes, you can set where files are uploaded to, either by configuring the underlying upload library, or simply copying the temporary file in the upload action. It doesn't matter where you download the file from, but if it's outside of the web context (or in WEB-INF) you need to stream it to the client yourself.
lakshmanan anand
Greenhorn

Joined: Jan 13, 2009
Posts: 22
I am sorry, my uploading problem is solved. Where as in download, I get the above mentioned error.. please help
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

Why wouldn't you just use a normal file input stream?
lakshmanan anand
Greenhorn

Joined: Jan 13, 2009
Posts: 22
David Newton wrote:Why wouldn't you just use a normal file input stream?


I am beginner to all this. Is this not the right way ? How else can i enable the download service for my struts2 web app. I thought stream result type would be easier ??



And why, inspite of the getter method written there, struts is not seeing it ??
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

A stream result *is* the right way to do it--I'm asking why not just use a regular file input stream? It's a hint.
lakshmanan anand
Greenhorn

Joined: Jan 13, 2009
Posts: 22
David Newton wrote:A stream result *is* the right way to do it--I'm asking why not just use a regular file input stream? It's a hint.


ya as you said, I have setup the code to




it worked in a wierd manner. the save as window opened at user side but ask the user if it has to download "fileDownload.action" instead of the original file itself. :banghead:
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

http://struts.apache.org/2.x/docs/stream-result.html
lakshmanan anand
Greenhorn

Joined: Jan 13, 2009
Posts: 22
what content types I should give. In my web app users can upload and download any type of file. what content type i should mention in the xml for it to correctly download the file in proper format.

please help
cow clarke
Greenhorn

Joined: Nov 12, 2009
Posts: 15


Here show that You can locat doc information from API

The following is the information from this API, which I think is enough for you.

A custom Result type for sending raw data (via an InputStream) directly to the HttpServletResponse. Very useful for allowing users to download content.

This result type takes the following parameters:

contentType - the stream mime-type as sent to the web browser (default = text/plain).
contentLength - the stream length in bytes (the browser displays a progress bar).
contentDisposition - the content disposition header value for specifing the file name (default = inline, values are typically attachment;filename="document.pdf".
inputName - the name of the InputStream property from the chained action (default = inputStream).
bufferSize - the size of the buffer to copy from input to output (default = 1024).
allowCaching if set to 'false' it will set the headers 'Pragma' and 'Cache-Control' to 'no-cahce', and prevent client from caching the content. (default = true)
contentCharSet if set to a string, ';charset=value' will be added to the content-type header, where value is the string set. If set to an expression, the result of evaluating the expression will be used. If not set, then no charset will be set on the header
These parameters can also be set by exposing a similarly named getter method on your Action. For example, you can provide getContentType() to override that parameter for the current action.

Example:



if you do exactly as this statement, I think the problem will be sovled.
Kumar Ala
Ranch Hand

Joined: Apr 30, 2009
Posts: 53
Hi ,

I am able to download file successfully if it is single file. But the issue comes when i try to download multiple files.
I am zipping the files and trying to downaload after creating the zipfile on the server. I would like to download without saving the zipfile on the server. Has anyone tried the same..?
Hope you guys got my question. Any help is appreciated.

Thanks


kumar
SCEA5 P1
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

You can only download one file at a time.
Kumar Ala
Ranch Hand

Joined: Apr 30, 2009
Posts: 53
Yes David. I am trying to Zip the files and download. But i wanted to send the stream back to Browser rather than storing the Zip file in to the server.
Hope you got the question now.
This i am doing because i can save some time for download file wihtout saving on the server as our client is expected to download big files.
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

Moving to Java, as Zip streams aren't Struts-related.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Dowloading a file from a a struts 2 web application