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
posted
0
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: 1005
posted
1
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 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
posted
0
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
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
posted
0
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
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.