Solved

Download file from servlet

Posted on 2007-03-22
1
216 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
1 Comment
 
LVL 19

Accepted Solution

by:
Kuldeepchaturvedi earned 170 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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
advertisement module in core php 4 160
jdbc error in jsp application 20 45
how to see all occupied ports on windows 10 laptop 15 65
iterator/ListIterator approach 17 26
After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
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 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 …

770 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