File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Struts and the fly likes Problem download file. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "Problem download file." Watch "Problem download file." New topic
Author

Problem download file.

Naveen Mishra
Ranch Hand

Joined: Nov 20, 2005
Posts: 75
Hi.
Here is the code snipet from one of the actions wherein i'm creating a tem file and setting its path to request attribute . In jsp getting the attribute and forwarding the response .

This is working fine with local and remote machine both when I run it in WSAD , but same is not working for Tomcat5.0 and JBoss ..
Help pls.

================
And the jsp part is




Thanks.

Naveen
Naveen Mishra
Ranch Hand

Joined: Nov 20, 2005
Posts: 75
ohh ..
This is also there in action after getting the forwardURL string.


Thanks..
Naveen Mishra
Ranch Hand

Joined: Nov 20, 2005
Posts: 75
Hi...
Seems I could not explain my problem proerly.
Trying to redefine it ...
main target is to "let the user download (open or save ) the file just created in the server".

The code sent in prev post is able to do this on WSAD (5.1) but causing problem( requseted resorce is not found...) on Tomcat and Jboss .

I'm running the full application on tomcat but deploying only the .war file on JBoss..

Hope , I've made it more clear this time..

Thanks.
Merrill Higginson
Ranch Hand

Joined: Feb 15, 2005
Posts: 4864
A URL that begins with "file://" only works on the local file system. It will not work on a server. This prefix should never be used in a web application. The only reason it worked with WSAD is that your server and browser were both on the same system.

Here are two possible solutions:

1- (Preferred) Don't output to a file at all. Simply retrieve the HttpServletResponse object's OutputStream using its getOutputStream() method and then write to it instead of the FileOutputStream. You would then set the content type using response.setContentType() method. Make sure you return null from the execute() method of your Action class to let struts know that you've handled the response and don't need it to forward to a JSP.

2- Create a temporary file in the web application's context so that it is available to be downloaded. You can find the actual file path of the web context with the following statement:

String path = getServlet().getServletContext().getRealPath("/");

Once you have this path, you can write your file to it. The user can then access it the same as any other file in your context:

http://myserver.com/myApp/myfile.xls

The advantage of option 1 over option 2 is that you're not cluttering up the disk of your server with files that you need to keep track of and clean up periodically.


Merrill
Consultant, Sima Solutions
Naveen Mishra
Ranch Hand

Joined: Nov 20, 2005
Posts: 75
Thanks a lot Merrill for ur response.



The only reason it worked with WSAD is that your server and browser were both on the same system.

but , I was able to download the file from remote system as well. that is what to my surprise , how come it worked (and still working ) with WSAD ?

thanks , anyway, for your support.
Merrill Higginson
Ranch Hand

Joined: Feb 15, 2005
Posts: 4864
All I can tell you is that a URL of file:///xyz/abc/myfile refers to a file on the same file system as your browser. If you developed the app in WSAD and then ported it to a server, what you're getting when you think you're downloading the file from the server is the file on your local system that you created while you were testing on the local system. If you delete this file from your local file system, it will stop working.
 
 
subject: Problem download file.