• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

Servlet -Download Jar file

 
Ranch Hand
Posts: 227
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Sheriff
Posts: 13411
Firefox Browser VI Editor Redhat
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 227
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 VI Editor Redhat
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic