?
Solved

How do i download a file to my Client Machine

Posted on 2002-05-02
6
Medium Priority
?
149 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 200 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
Python: Series & Data Frames With Pandas

Learn the basics of Python’s pandas library of series & data frames and how we can use these tools for data manipulation.

 

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

Stack Overflow Podcast - Developer Story

Welcome to the Stack Overflow podcast recorded Thursday July 20 at Stack Overflow Headquearters in NYC. Your hosts today are podcast regulars Jay Hanlon, David Fullerton, and Ilana Yitzhaki, plus the quite irregular Matt Sherman (Stack Overflow Engineering Manager extraordinaire)

Question has a verified solution.

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

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
Suggested Courses
Course of the Month9 days, 11 hours left to enroll

762 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