Solved

Download file from servlet

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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

744 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

13 Experts available now in Live!

Get 1:1 Help Now