• 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
  • Tim Cooke
  • paul wheaton
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

uploading file outside of context

 
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 119
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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;
 
Bartender
Posts: 1845
10
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 147
Eclipse IDE Tomcat Server Debian
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 38
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • 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
    Eclipse IDE Tomcat Server Debian
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • 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.
     
    If you are using a rototiller, you are doing it wrong. Even on this tiny ad:
    Clean our rivers and oceans from home
    https://www.kickstarter.com/projects/paulwheaton/willow-feeders
    reply
      Bookmark Topic Watch Topic
    • New Topic