Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Downloading a File to the Clients Browser

Status
Not open for further replies.

Enya

MIS
Dec 3, 2004
17
0
0
US
My servlet reads the data (in bytes) from a file....I want to download this data onto the clients browser. How can I do this??

Code so far:
-----------
File f = new File(FileName);
try {
FileInputStream fis = new FileInputStream(f);
BufferedInputStream bis = new BufferedInputStream(fis);
DataInputStream dis = new DataInputStream(bis);

line = dis.read(b);

while(!done) {
line = dis.read(b);
System.out.write(b);
}
if (line == -1) done = true;
dis.close();

followed by catch statements.


 
Here is a snippet.

protected void doGet(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException {
DataOutputStream out;

// open your file
res.setContentType("application/octet-stream");
out=new DataOutputStream(res.getOutputStream());

// make a cycle, read the "b"
out.write(b); // sends "b" to the browser
// end of the cycle

// close your file
out.close();
}

Good luck. Bye, Otto.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top