This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
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.
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.
In preparing for battle I have always found that plans are useless, but planning is indispensable. -- Dwight D. Eisenhower
Joined: Feb 28, 2011
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