Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

File Upload Question

 
Ben Hagadorn
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4864
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Ben Hagadorn
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Merrill,

Thanks for the help! DownloadAction was exactly what I needed!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic