wood burning stoves 2.0*
The moose likes Struts and the fly likes How to return file using Struts ? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "How to return file using Struts ?" Watch "How to return file using Struts ?" New topic
Author

How to return file using Struts ?

Vladimir Dejanovic
Greenhorn

Joined: Feb 11, 2009
Posts: 4
Hi ,

I am new to Struts and I am trying to return file using it , but unfortunately with no luck. In my app there are some tables, and I wont that when user click on link export to PDF to return him pdf file, I created code which out of data creates pdf file , but I can't manage to return it to user . So my question is how to return file to user using struts ?

I tried with this code

David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

Normally it's a good idea to say what actually went wrong.

http://faq.javaranch.com/java/TellTheDetails

That aside, I've usually used the "stream" result when returning files rather than doing it manually, but it probably doesn't matter much.
Vladimir Dejanovic
Greenhorn

Joined: Feb 11, 2009
Posts: 4
It went wrong in the fact that it doesn't work , I guessed with upper code , and hoped that it will work but it doesn't , it opened acrobat reader, but it show error not correct pdf file .

So my question is how to return pdf file which I have to user using struts ? Can I give him url of file or something else ... any kind of help or suggestion is welcome .
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

It went wrong in the fact that it doesn't work , I guessed with upper code , and hoped that it will work but it doesn't , it opened acrobat reader, but it show error not correct pdf file .


That is not enough information to help. "It shows an error" provides nothing that can be used to diagnose the problem. The rest of the action (that should, for example, be returning a "null" instead of an ActionForward) isn't displayed, so there's no way to know if that part of the application is correct. Have you verified that the PDF file is being created correctly? If so, that's useful information to provide, as it potentially eliminates another point of failure.
Vladimir Dejanovic
Greenhorn

Joined: Feb 11, 2009
Posts: 4
David Newton wrote:
It went wrong in the fact that it doesn't work , I guessed with upper code , and hoped that it will work but it doesn't , it opened acrobat reader, but it show error not correct pdf file .


That is not enough information to help. "It shows an error" provides nothing that can be used to diagnose the problem. The rest of the action (that should, for example, be returning a "null" instead of an ActionForward) isn't displayed, so there's no way to know if that part of the application is correct. Have you verified that the PDF file is being created correctly? If so, that's useful information to provide, as it potentially eliminates another point of failure.


as far as I saw pdf file is correct , because when I copy it to my computer manually and try to open it is it opened with no errors or problems. Code which I provided in my first post is complete code inside this class , I only have imports at the beginning and that is it.

Like I said I am new to struts , so I am not sure if I set response in correct manner or not.

I am not sure if I understood what You meant by this

for example, be returning a "null" instead of an ActionForward)



should I modify method toPDF in a way that it doesn't return void but ActionForward , and then at the end of method to put return null ?
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

Oh, you're using Struts 1 -- my mistake.

Normal S1 actions return an ActionForward; maybe it's possible to have void action methods, not sure. The BaseAction class may be doing something that makes my comment irrelevant. I'm not sure about the content length and if it should be the exact length of the file; that's an HTTP/non-Struts issue that searching the web might help with.

There are a million references for how to return a PDF from Struts 1--it might be quickest to just search around a bit, because it's been covered in depth all over the place.
Vladimir Dejanovic
Greenhorn

Joined: Feb 11, 2009
Posts: 4
David Newton wrote:Oh, you're using Struts 1 -- my mistake.

Normal S1 actions return an ActionForward; maybe it's possible to have void action methods, not sure. The BaseAction class may be doing something that makes my comment irrelevant. I'm not sure about the content length and if it should be the exact length of the file; that's an HTTP/non-Struts issue that searching the web might help with.

There are a million references for how to return a PDF from Struts 1--it might be quickest to just search around a bit, because it's been covered in depth all over the place.


I googled it , tried to find it , but unfortunately with no luck. I looked at servlet in FOP (I used this in order to make my pdf file), and how it is returning data , and tried to modify it in order to get my pdf back , that is how I came up with my code, but something is wrong there :) . Back to the drawing board , I will look further on the net , Thanks for help .
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

This search had several relevant results.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to return file using Struts ?