Solved

How do i download a file to my Client Machine

Posted on 2002-05-02
6
146 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
ID: 6988321
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
ID: 6992904
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
ID: 6995640
"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
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 

Author Comment

by:V_Ram
ID: 6995691
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
ID: 6995723
What do you understand by a default name ?
0
 

Author Comment

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

0

Featured Post

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
java 8 lambda expresssions exception handling 3 88
stackato and cloud 4 85
why cannot we forward request once the response is committed 2 42
ejb on wildfly 5 19
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…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
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.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

813 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

16 Experts available now in Live!

Get 1:1 Help Now