This week's book giveaway is in the Android forum.
We're giving away four copies of Head First Android and have Dawn & David Griffiths on-line!
See this thread for details.
The moose likes Web Component Certification (SCWCD/OCPJWCD) and the fly likes jar file download Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Head First Android this week in the Android forum!
JavaRanch » Java Forums » Certification » Web Component Certification (SCWCD/OCPJWCD)
Bookmark "jar file download" Watch "jar file download" New topic
Author

jar file download

Naresh Chaurasia
Ranch Hand

Joined: May 18, 2005
Posts: 361
Hi,
I have taken the following code from HFSJ that is used for downloading jar file.When the above servlet is called,the file is downloaded with name InputOutputStream. Is it possible that when the download starts, it is done with extension jar(without myself explicitly specify it during the download).

public class InputOutputStream extends HttpServlet {

public void init(ServletConfig config)throws ServletException{
super.init(config);
}


public void doGet(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException {

res.setContentType("application/jar");
InputStream is = getServletContext().getResourceAsStream("/archive.jar");

int iRead=0;
byte b[] = new byte[1024];

OutputStream os = res.getOutputStream();

while( (iRead=is.read(b) ) != -1)
{
os.write(b,0,iRead);
}

os.flush();
os.close();

}//end of doGet


public void doPost(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException {
doGet(req, res);
}
}


SCJP 1.4, SCWCD1.4, OCA(1Z0-007)
Anand Wadhwani
Ranch Hand

Joined: Mar 21, 2005
Posts: 151
Put the following line immediately after setContentType():


This line will make the browser aware of the name to use for downloading the stram. Therefore the browser will display the download dialog box with the given name 'Archive.jar' by default. Hope this helps.
[ June 17, 2005: Message edited by: Anand Wadhwani ]

SCWCD 1.4<br />---------------------<br />Ability is what you're capable of. <br />Motivation determines what you do. <br />Attitude determines how well you do it.<br />---------------------
Naresh Chaurasia
Ranch Hand

Joined: May 18, 2005
Posts: 361
Hi Anand,
thnx. It is woking fine.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: jar file download
 
jQuery in Action, 3rd edition