Solved

Download file from servlet

Posted on 2007-03-22
1
215 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

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…
I had a project requirement for a displaying a user workbench .This workbench would consist multiple data grids .In each grid the user will be able to see a large number of data. These data grids should allow the user to 1. Sort 2. Export the …
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
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.

863 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

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now