• 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
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

How to Download a FIle in struts

 
Ranch Hand
Posts: 485
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 4864
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Parameswaran Thangavel
Ranch Hand
Posts: 485
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 4864
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 485
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 4864
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 485
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
You firghten me terribly. I would like to go home now. Here, take this tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic