Solved

How do i download a file to my Client Machine

Posted on 2002-05-02
6
148 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 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
Get Actionable Data from Your Monitoring Solution

Your communication platform is only as good as the relevance of the information you send. Ensure your alerts get to the right people every time with actionable responses. Create escalation rules that ensure everyone follows the process and nothing is left to chance.

 

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

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

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

Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
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 …
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…

717 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