Click to See Complete Forum and Search --> : File Download


shirl_yap
July 10th, 2001, 12:04 AM
Hi,

i m running out of time. can u guys please help me on how to develop a system that downloads any files from the server to the localdrive. The system should prompt the user of the location to save the file.

I would appreciate alot if u guys could forward me some sample codes or any good websites that contains sample codes.

thanks alot
reply at shirl_yap@yahoo.com

dlorde
July 10th, 2001, 07:17 AM
If you're talking about local drives on browser-based clients, it's a question of setting the correct response headers in your servlet. Here's some code that I've used to do this (note that MS IE 5.5 has a bug affecting browser file downloads): if (verbose) System.out.println("Retrieving binary file '" + fileName + "'...");
byte[] fileBuffer = getBinaryFile(fileName);

// Set HttpServletResponse details
if (verbose) System.out.println("Setting servlet response details...");

// 'clientFile' is the default client file name, the client can
// change it.
response.setContentType("application/x-filler");
response.setHeader("Content-Disposition", "attachment; filename="+clientFile);

// Read from file into servlet output stream
ServletOutputStream stream = response.getOutputStream();

if (verbose) System.out.println("Buffering binary file...");
BufferedInputStream fif = new BufferedInputStream(new ByteArrayInputStream(fileBuffer));

if (verbose) System.out.println("Sending file to client as '"+clientFile+"'...");
int data = 0;
while((data = fif.read()) != -1)
{
stream.write(data);
}
fif.close();
stream.close();
if (verbose) System.out.println("Finished sending file.");
}

Dave


To email me remove '_spamjam' from my email address