• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

File Upload Question

 
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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>.
 
Ranch Hand
Posts: 4864
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Merrill,

Thanks for the help! DownloadAction was exactly what I needed!
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic