wood burning stoves 2.0*
The moose likes Struts and the fly likes How to Download a FIle in struts Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "How to Download a FIle in struts " Watch "How to Download a FIle in struts " New topic
Author

How to Download a FIle in struts

Parameswaran Thangavel
Ranch Hand

Joined: Mar 01, 2005
Posts: 485
hi all
i am displaying certain content to the user(say result.jsp). And in that page there is option for the user to download those contents.
My question is how we can get the teh download dialog box, where user will have option to choose the file name. The file format is fixed.

And also i need to retain the result page (result.jsp ).

i am trying to acheive above requirements using LookupDispatchAction servlet.
Merrill Higginson
Ranch Hand

Joined: Feb 15, 2005
Posts: 4864
It may be sufficient just to provide a link with the name of the file. If the file suffix is something other than a common Internet file type(e.g. html, jpg, gif) the browser will generally produce a prompt asking the user what to do with the file.

If you need more control over the download than this, you will have to create an action that will read the file and then output it to the Output stream of the HttpServletResponse object.


Merrill
Consultant, Sima Solutions
Parameswaran Thangavel
Ranch Hand

Joined: Mar 01, 2005
Posts: 485
Hi
The data which i am sending to the client is byte array.
To make the client save the data i am doing the following

1) Create a Temp file. (Donno where it will got created.)
2) set the response content type.
3) Pass the created temp file as attachment to the client. which he will save to his local disk.




is there any better way to pass the content to save directly to the file the client specifies, instead of creating the temp file and sending it as attachment.

If possible give me the link where i refer for my requirement.

Thanks
Merrill Higginson
Ranch Hand

Joined: Feb 15, 2005
Posts: 4864
You don't need to write a temporary file. You can just write your Byte array directly to the response. There is an example of how to do this in the struts-examples.war file that comes with the Struts download.

Examine the source code for org.apache.struts.webapp.exercise.ImageAction and you'll see how to do it.
Parameswaran Thangavel
Ranch Hand

Joined: Mar 01, 2005
Posts: 485
hi
i went through the example you mentioned.
when i tried to reuse the code, i am getting the inputstream as null.
i pasted my code snippet below.



I am having a XML file as byte array, which i display to user by converting to string object. User had the option to download that xml file. so i Created the xml String object and pass it to the getResouceAsStream but it returns null. i donno why. but the below code snippet prints the whole xml file as it is.

But when i pass the new string()... to get resouce stream, input stream object is returned as null.

Any idea.
Merrill Higginson
Ranch Hand

Joined: Feb 15, 2005
Posts: 4864
In the example, they had to read a file, turn it into an InputStream, and then turn the InputStream into a Byte array. Your situation is much simpler. You've already got a Byte array. If arryByteXmlPayLoad is a Byte array, you don't need to turn it into an InputStream. Just write the Byte array directly to the OutputStream with no looping like this:

out.write(arryByteXmlPayLoad);
out.close();

You will also want to set the content type and declare it as an attachment like this:

response.setContentType("text/xml");
response.setHeader("Content-Disposition", "attachment; filename=\"myFile.xml\"";

Also, make sure you return null from your execute() method. This tells Struts that you've already handled the response and don't need to forward to a JSP.
Parameswaran Thangavel
Ranch Hand

Joined: Mar 01, 2005
Posts: 485
Hi Merill
Thanks. i got it.

Still out of curiosity, why the input stream returns null.

We are passing the string object which contains some data. but why it returns null. infact if it didn't return null the logic will do what i am intended to do so?
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to Download a FIle in struts
 
Similar Threads
force download of page
jsp
Save /Open option for file download
download file option
Jnlp in a J2ee application