• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to implement download a file/pdf file in struts.

 
kswaughs kswaughs
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, I am developing a struts application and one of My requirements is download a pdf file from the server when a link is clicked.

Is it possible with DownloadAction.If So please help me how to use it.
 
RoshaniG Gopal
Ranch Hand
Posts: 180
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I had done the same type of functionality with an excel sheet. I believe the pdf would be more or less on the same pattern. If you require some more info, i will provide you..

I did not use DownloadAction for the above.

Regards,
Roshani
 
Merrill Higginson
Ranch Hand
Posts: 4864
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
While you certainly don't have to use DownloadAction when writing an Action that downloads a file, using it would certainly make the process easier. You just have to override one method and Struts does the rest. Below is an example of an action that downloads the file "c:\temp\myPdfFile.pdf".



Believe it or not, that's it. The abstract DownloadAction class handles all the rest.
 
RoshaniG Gopal
Ranch Hand
Posts: 180
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Merrill,

Thats really useful bit of information. Just wanted to ask you if the same may be used for Excel sheet?

Regards,
Roshani
 
Merrill Higginson
Ranch Hand
Posts: 4864
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Certainly. Just change the file type to "application/vnd.ms-excel".
 
Jaydeep Deshmukh
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can you please give an example on the download file without extending the DownloadAction class?

Thanks
Jaydeep
 
Brent Sterling
Ranch Hand
Posts: 948
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jaydeep: You can always download the Struts source code and take a look at the source for the DownloadAction class. I suspect that it is only 20 or so lines of code.

Also, if your files are saved on the web server's file system in a directory that is accessible from the outside world, you can just put a plain old link in your web page. Of course this would not work if you had to retrieve the file from a database or you wanted to wrap access to the file with some logic.

- Brent
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic