Link to home
Start Free TrialLog in
Avatar of V_Ram
V_Ram

asked on

How do i download a file to my Client Machine

Hello All,
    I am currently working on a Java project involving Servlet, Jsp and Oracle. Currently i am using Tomcat as my servlet container.

    In the project i have a requirement to download a export file to the Client's machine. Does any one have idea about how to perform the same.

    This is very urgent.

V. Ramkumar.
Avatar of Ovi
Ovi

server side :
  set the content type of the response to whatever mimetype has the file if is normaly downloadable or force to application mimetype, read the file and write'it on the response.

client side :
  open a connection to your url, get the inputstream, read from it and write the readed content as a new file.

  URL url = new URL("your url here");
  InputStream is = url.openStream();
  FileOutputStream fos = new FileOutputStream("a file");
  byte[] buffer = new byte[4096];
  while(is.read(buffer, 0, buffer.length) != -1) {
    fos.write(buffer);
    fos.flush();
  }


...add the required try&catch statements, and of course close methods for streams.
Avatar of V_Ram

ASKER

Hello Ovi,
     You talk about client side scripting, but i do not need any client side scripting. Is there any way that i could do with servlets.
 
     Thanks for your efforts.

V. Ram.
ASKER CERTIFIED SOLUTION
Avatar of Ovi
Ovi

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of V_Ram

ASKER

Got it, Thanks.By the way how do i give a default file name to the file that's going to be downloaded.

V. Ramkumar
What do you understand by a default name ?
Avatar of V_Ram

ASKER

The name given to the file that's going to be downloaded.