This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Struts and the fly likes File Upload Question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "File Upload Question" Watch "File Upload Question" New topic
Author

File Upload Question

Ben Hagadorn
Greenhorn

Joined: Mar 28, 2006
Posts: 25
first, my apologies if STRUTS isn't the correct place to post this.

Here's my issue:

I have a web app that allows users to upload files to a directory under my deployment area. Then I have another page that displays links to these files that can be clicked so the uploaded documents can be viewed. The problem is that when I redeploy the app for any reason, the directory with the uploaded files gets blown away (since it resides in my deployment). What I'd like to do is upload these files to a place outside of my deployment so this does not happen. Assume that my web site is called MyApp and the directory where the files are going is called UploadedFiles

So now I have two issues:
1) how to specify the file path for where the uploaded files are saved. I currently derive this by using servletContext.getRealPath("UploadedFiles") but that won't give me what I need for a location outside my deployment.

2) how to derive a valid URL for the links I display on my JSP pages. I currently use "/MyAPP/UploadedFiles/<the file>". When the JSP page is rendered the link becomes http://localhost:8080/MyApp/UploadedFiles/<the file>.
Merrill Higginson
Ranch Hand

Joined: Feb 15, 2005
Posts: 4864
Originally posted by Ben Hagadorn:

1) how to specify the file path for where the uploaded files are saved. I currently derive this by using servletContext.getRealPath("UploadedFiles") but that won't give me what I need for a location outside my deployment.


Since you're no longer saving the files inside the web context, it doesn't really matter where you put them. You will now have to specify a path based on the server's operating system file structure. Just pick a directory and make sure that the directory has public read/write authority.

It's probably not a good idea to hard-code the path, though. I'd suggest specifying it as a context parameter in your web.xml file, and then reading the context parameter in your application code.
Originally posted by Ben Hagadorn:

2) how to derive a valid URL for the links I display on my JSP pages. I currently use "/MyAPP/UploadedFiles/<the file>".

Because the file is no longer in the web context, you can't get to the file directly from a URL any more. You will have to create an Action class that will read the file from the server's file system and then output it to the HTTPServletResponse object's output stream.

So, your new URL will be something like "MyApp/DownloadAction.do?file=<thefile>"

Struts has a DownloadAction class that you can extend. This class does almost all the work of downloading the file. All you have to do is override its getStreamInfo method to tell it where to find the file and what its content type is.


Merrill
Consultant, Sima Solutions
Ben Hagadorn
Greenhorn

Joined: Mar 28, 2006
Posts: 25
Merrill,

Thanks for the help! DownloadAction was exactly what I needed!
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: File Upload Question
 
Similar Threads
Tomcat url redirect problem
page cannot be displayed error
How to see my own home page instead of the Tomcat one?
MVC implementation?
Tomcat Security