Solved

How do i download a file to my Client Machine

Posted on 2002-05-02
6
144 Views
Last Modified: 2013-11-24
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.
0
Comment
Question by:V_Ram
  • 3
  • 3
6 Comments
 
LVL 9

Expert Comment

by:Ovi
Comment Utility
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.
0
 

Author Comment

by:V_Ram
Comment Utility
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.
0
 
LVL 9

Accepted Solution

by:
Ovi earned 50 total points
Comment Utility
"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."

In other words, in your servlet you will receive in the doGet method a request with some parameters, and a response stream. You only need to open the required file from the server disc, read from it and write'it on the response stream. The code from reading a file on the servlet is the same as reading on any client program (there are not any sophisticated issues to care about).
0
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 

Author Comment

by:V_Ram
Comment Utility
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
0
 
LVL 9

Expert Comment

by:Ovi
Comment Utility
What do you understand by a default name ?
0
 

Author Comment

by:V_Ram
Comment Utility
The name given to the file that's going to be downloaded.

0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now