[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

How do i download a file to my Client Machine

Posted on 2002-05-02
6
Medium Priority
?
150 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

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…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
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 …
Suggested Courses

649 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