[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

Download file from servlet

Posted on 2007-03-22
1
Medium Priority
?
227 Views
Last Modified: 2013-11-24
I am new to servlets. I want to render a word document from a servlet.

There is a JSP page

<HTML>
<BODY>
  <a href="Sample">Download</a>
</BODY>
</HTML>

When user clicks on this link, a servlet is called

public class Sample extends HttpServlet {


    public void doGet(HttpServletRequest request,
                      HttpServletResponse response)
        throws IOException, ServletException   {
        String file ="myWord.doc";
        response.setContentType("text/html");
       
            ServletOutputStream loStr = null;
        loStr = response.getOutputStream();
        loStr.write(file.getBytes());
        loStr.flush();
        loStr.close();
       
    }
}

This servlet has to read a word document from the web server file system and download it on user's machine. But what I have written above only prints "myWord.doc" on the browser and does not download the file.
Please help
0
Comment
Question by:zensa
[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
1 Comment
 
LVL 19

Accepted Solution

by:
Kuldeepchaturvedi earned 680 total points
ID: 18777787
loStr = response.getOutputStream();
        loStr.write(file.getBytes());
here you have taken the bytes of the string "file" and printed it out...
obviously it will print just the name of the file..

you need to do following..
String file = :myWord.doc";
File f = new File(file);
FileInputStream fin = new FileInputStream(f);
int byte b = fin.read();
while (b!=-1)
{
loStr.write(b);
b=fin.read();
}


also
response.setContentType("text/html");

should be
response.setContentType("application/msword");
for it to display the doc correctly.
 
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying 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 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…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
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…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
Suggested Courses

650 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