jsp - Java : download file outside server context -


i need save file , download file in directory outside server context. using apache tomacat able in directory present in webapps directory of application

if directory structure follows,

--src --webcontent     -- uploaddir          -- myfile.txt 

then able download in simply.

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

but, problem when file in other directory d:\\uploadedfile\\myfile.txt

then wont able download it, resource not in server context above.

i have file path uuid mapping, like,

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

then want file should downloaded, on click of following,

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

so, how make file downloadable when outside server context, heard getresourceasstream() method , 1 me on how this, simple code snippet?

try below code can write in filedownloadservet. fetch file name request parameter , read , write file.

if need security checks before processing request.

file file = new file("/home/files", "file name user wants 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);     } } {     if (outputstream != null) {          try {             outputstream.close();         } catch (ioexception e) {             //log         }     }     // same input stream } 

Comments

Popular posts from this blog

Why does Ruby on Rails generate add a blank line to the end of a file? -

keyboard - Smiles and long press feature in Android -

node.js - Bad Request - node js ajax post -