Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Servlet -Download Jar file

 
Karthik Rajendiran
Ranch Hand
Posts: 218
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Friends,
I am facing some problem in servlet which is written to download files .
I wrote a servlet for downloading jar files. once the user clicks a button or link the servlet is called.

HTML CODE.
----------


In the above code HelloWorld is my servlet .The code is as follows
SERVLET CODE
------------


I have stored my webapps as d:\tomcat\webapps\karthik
and the a.jar in the d:\tomcat\webapps\karthik path.

when i submit i unable to download the jar , the file i receive is as of unknown format.The file is named HelloWorld.

My web.xml

[CODE
<servlet>
<servlet-name>HelloWorld</servlet-name>
<servlet-class>HelloWorld</servlet-class>
</servlet>

<servlet-mapping>
<servlet-name>HelloWorld</servlet-name>
<url-pattern>/HelloWorld</url-pattern>
</servlet-mapping>] [/CODE]

I tried with text files , my text file is opening in the same page without prompting.

Can any one please help me out.
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
MSIE feels no need to obey the content-type header.
Set the Content-Disposition header.
Make sure you use "attachment" and create a file name with a ".jar" extension.
 
Anil Sadi
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Use the following code in your servlet:

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class HelloWorld extends HttpServlet
{
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException,IOException
{

// new code -- start
String filename = request.getParameter("filename");
filename = filename + ".jar";
response.setHeader("Content-Disposition","attachment;filename=\"" + filename + "\"");
// new code -- end

ServletContext ct = getServletContext();
InputStream in = ct.getResourceAsStream("/a.jar");
int read=0;
byte[] bytes = new byte[1024];
OutputStream os = response.getOutputStream();
while((read=in.read(bytes))!=-1)
{
os.write(bytes,0,read);
}
os.flush();
os.close();
}
}
 
Karthik Rajendiran
Ranch Hand
Posts: 218
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks you very much,
It is working ..
As per the Web component developer exam book-kathy sierra,
the code is not working. I couldnt understand why that code in the book is not working.
Then i included the content disposition header which made to work.
When i used the code mentioned by me, the HelloWorld file was downloading instead of the file i requested. May i know the reason for it.

Please explain
Thanks
karthik
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As I said earlier, MSIE (wrongly) ignores the content-type header sometimes.
It probably works fine in FireFox or any other proper browser.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic