0

I need to save a file and download file in directory outside server context. I am using Apache Tomacat I am able to do this in directory present in webapps directory of application

If my directory structure is as follows,

--src --WebContent -- uploaddir -- myfile.txt 

Then I am able to download in by simply.

 <a href="uploaddir/myfile.txt" target="_blank">download</a> 

But, problem is when file is in some other directory say d:\\uploadedfile\\myfile.txt

then I wont be able to download it, as resource is not in server context as above.

I have file path to uuid mapping, like,

d:\\uploadedfiles\\myfile.txt <-> some_uuid

then I want file should be downloaded, on click of following,

 <a href="filedownloadservlet?ref_file=some_uuid">download</a> 

So, How to make file downloadable when it is outside the server context, I heard about getResourceAsStream() method which would do this , But would any one help me on how to do this, probably with simple code snippet?

3
  • why not config a BASE_PATH for you storing? Commented Apr 18, 2013 at 5:05
  • Thanks licchengwu , is it related to php? Commented Apr 18, 2013 at 5:14
  • No, just using java, or maybe you can store the real file path to database. Commented Apr 18, 2013 at 15:49

3 Answers 3

1

Try the below code which you can write in filedownloadservet. Fetch the file name from the request parameter and then read and write the file.

If you need to do some security checks then do that before processing the request.

File file = new File("/home/files", "file name which user wants to download"); response.setContentType(getServletContext().getMimeType(file.getName())); response.setContentLength(file.length()); BufferedInputStream inputStream = null; BufferedOutputStream outputStream = null; try { inputStream = new BufferedInputStream(new FileInputStream(file)); outputStream = new BufferedOutputStream(response.getOutputStream()); byte[] buf = new byte[2048]; int len; while ((len = inputStream.read(buf)) > 0) { outputStream.write(buf, 0, len); } } finally { if (outputStream != null) { try { outputStream.close(); } catch (IOException e) { //log it } } // do the same for input stream also } 
Sign up to request clarification or add additional context in comments.

1 Comment

Hi! i tried the code but nothing is happening, when i run the servlet
1

here i found the answer,

 response.setContentType("application/msword"); response.setHeader("Content-Disposition","attachment;filename=downloadname.doc"); File file=new File("d:\\test.doc"); InputStream is=new FileInputStream(file); int read=0; byte[] bytes = new byte[BYTES_DOWNLOAD]; OutputStream os = response.getOutputStream(); while((read = is.read(bytes))!= -1){ os.write(bytes, 0, read); } os.flush(); os.close(); 

Comments

0

Base path will not work that is for HTML and it works if the base path is also exposed by your web server which does not look like case here.

To download an arbitary file you need to open the file using a FileInputStream (and surround it by a buffered input stream), read a byte, then send that byte from your servlet to the client.

Then there are security concerns, so should google that (basically not give access to any file but only file that is to be shared, audit download etc as needed.

Again in your servlet set the mime type etc and then open a input stream and write the bytes to the output stream to client

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.