java - downloading a input stream using servlets -


i using following code content object in s3 bucket. able copy data file locally, file needs 'downloaded' , has shown in browser's downloads list. searched bit , cam know has response.setheader( "content-disposition", "attachment;filename=\"" + filename + "\"" ); tried add somehow couldn't work. understanding source has file on server converted stream. s3 contents in form of input stream. how download file in browser? below code have tried far

amazons3 s3 = new amazons3client(new profilecredentialsprovider());     s3object fetchfile = s3.getobject(new getobjectrequest(bucketname, fileloc));         final bufferedinputstream = new bufferedinputstream(fetchfile.getobjectcontent());         response.setcontenttype("application/json");         response.setheader( "content-disposition", "attachment;filename=\"" + filename + "\"" );                     servletoutputstream sos = response.getoutputstream();         int bytesread = i.read();         while(bytesread!=-1)         {             sos.write(bytesread);             bytesread = i.read();         }         if(i!= null)i.close();         if(sos!= null)sos.close(); 

everything correct except file reading part.

//set size of buffer stream data.   byte []buffer=new byte[1024*8]; 

instead of

  int byte=i.read(); 

now read file.

  while( ( length = yourinputstream.read(buffer))!=-1)     {       youroutputstream.write(buffer);                  }     system.out.println("file downloaded."); 

additionally,puting whole code within try/catch block know exact reason of problem.


Comments

Popular posts from this blog

Command prompt result in label. Python 2.7 -

javascript - How do I use URL parameters to change link href on page? -

amazon web services - AWS Route53 Trying To Get Site To Resolve To www -