This week's book giveaway is in the Clojure forum.
We're giving away four copies of Clojure in Action and have Amit Rathore and Francis Avila on-line!
See this thread for details.
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

uploading file outside of context

 
prajapatisagar Sagar
Ranch Hand
Posts: 35
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


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
Posts: 119
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1640
9
  • 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 35
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 147
Debian Eclipse IDE Tomcat Server
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
     
    prajapatisagar Sagar
    Ranch Hand
    Posts: 35
    • 0
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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
    Posts: 147
    Debian Eclipse IDE Tomcat Server
    • 0
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic