This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes JSP and the fly likes uploading file outside of context Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » JSP
Bookmark "uploading file outside of context" Watch "uploading file outside of context" New topic
Author

uploading file outside of context

prajapatisagar Sagar
Ranch Hand

Joined: Feb 28, 2011
Posts: 35


Above is the code how i am saving files inside uploadedFiles.
But i want to save file outside of context that is uploadedFiles folder and context folder on same directory.

Please help

Thanks in advance

i can save outside of context by setting srcPath


now i have problem while showing that file in jsp page

i am using

to display that file but it give me http 404 mesage please help
Haina Minawa
Ranch Hand

Joined: Oct 13, 2011
Posts: 119
prajapatisagar Sagar wrote:

Above is the code how i am saving files inside uploadedFiles.
But i want to save file outside of context that is uploadedFiles folder and context folder on same directory.

Please help

Thanks in advance


One solution is to split the context path by the delimiter File.separator, then remove the last path element. Finally concatenate the result with "uploadedFiles/"+Name;
Stefan Evans
Bartender

Joined: Jul 06, 2005
Posts: 1016
If you want to save the files outside of the web context (which I agree is the correct way to do this) then you won't be able to directly access them, because they're not in context.
However the problem is easily solved, you provide a file download servlet which reads the file from the directory, and sends it out as a servlet response.

I would set it up such that http://myWebSite/myWebApp/getFile/MyFile.txt would download the file.

A servlet mapping in web.xml would map /getFile/* to the file servlet
The servlet would then look at the rest of the url to know which file to send back to the client.

Googling for "file servlet" gives you some good results.


prajapatisagar Sagar
Ranch Hand

Joined: Feb 28, 2011
Posts: 35
Dear Stefan i think your solution works but i am new to jsp so as requested by you i cannot perform task so please can you give me step by step to perform these task
Pete Nelson
Ranch Hand

Joined: Aug 30, 2010
Posts: 147

A "getFile" servlet, that serves a file outside of the context root, basically follows these steps:

  • Servlet receives request for file
  • Servlet opens file as input stream
  • So far so good? Send the appropriate content type & length via HttpServletResponse
  • (if there was an error, send your error message & quit)
  • Use the response's getOutputStream() method to get your output stream
  • Chunk by chunk, read from the file, write to the output stream
  • Close input stream
  • Flush & close the output stream


  • One security caution - use a context-param or a protected field in your servlet to store the base directory where these files will live, and only serve files from that base directory. DO NOT let this servlet serve just any file on your system, or you may allow people to access /etc/password or file:///C|/WINDOWS/ntusers.dat , or other sensitive files that you probably don't want to share.


    OCPJP
    In preparing for battle I have always found that plans are useless, but planning is indispensable. -- Dwight D. Eisenhower
    prajapatisagar Sagar
    Ranch Hand

    Joined: Feb 28, 2011
    Posts: 35
    Dear Pete Nelson,Does input stream works on image file also the file uploaded may be image or text extension or any extension.Can you give me examples how to follow these steps in general
    Pete Nelson
    Ranch Hand

    Joined: Aug 30, 2010
    Posts: 147

    Input Stream deals with a stream of bytes. It doesn't matter if the bytes are ASCII text or binary data.

    As far as examples, there are a TON on the web waiting for you to google. Try googling for "java file io", or even "file servlet", as Stefan Evans suggested.

    You can also look at the Java Basic I/O Tutorial.
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: uploading file outside of context
     
    Similar Threads
    Hyper link Doubt
    how to handle file uploads in spring
    how to upload using html or javascript?
    How to install tomcat5 in Ubuntu 11.04 when it is not in the apt-cache?
    file upload changing the src path