Skip to Main Content

Java EE (Java Enterprise Edition) General Discussion

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Downloading a Text file from Server to Local Desktop Directly

843829Feb 3 2003 — edited Feb 7 2003
Hello All,
I am using the Code,as below, to Download a Text File from a particular
Directory on the Server to any where the user need to download it through the Dialouge box...
But what i Really want to achieve is that the user should not be asked for an Dialouge box, instead it
should Download the Text file directly to "Desktop"(By Default)...
Is there a way to do so...if yes can anybody help me out with the solution...
public HttpServletResponse doDownloadFile(HttpServletRequest req,HttpServletResponse res)throws ServletException,IOException {

ServletOutputStream stream = null; 
res.setContentType("application/binary");

String fname1 = req.getParameter("BusinessId");
String fname2 = req.getParameter("AccountUserId");
String fname =fname1+"-"+fname2+".txt";
res.setHeader("Content-Disposition","inline; filename=\""+fname+"\";");

BufferedInputStream bif = null; 
try {
bif = new BufferedInputStream(new FileInputStream("C:/ABCD/download/XYZ/"+fname)); 
stream = res.getOutputStream();
byte [] buf = new byte[20480]; 
int data;
while(( data = bif.read(buf,0,20480)) != -1) {
stream.write(buf, 0, data);
stream.flush(); 
}
bif.close();
stream.close(); 
return res;
} catch(Exception e) {
System.out.println("The Exception in MainServlet is :"+e);
}
finally {
if (stream != null)
stream.close();
return res;
}
}//end of method
I had already posted the same question on java forum,but did not get any response..So please help me out...

Thanks a million in advance,
Regards
Sam
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Mar 7 2003
Added on Feb 3 2003
2 comments
336 views