Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to return file using Struts ?

 
Vladimir Dejanovic
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 12617
IntelliJ IDE Ruby
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic