sending jar file to client using OutputStream.

I am a newbie in servlet.Just wrote a servlet to send jar file to client using OutputStream. Below is the code.

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

import javax.servlet.http.HttpServlet;

import javax.servlet.ServletException;

import java.io.OutputStream;

import java.io.File;

import java.io.FileInputStream;

import java.io.IOException;

publicclass ReadingJarFileextends HttpServlet

{

publicvoid doGet(HttpServletRequest request,HttpServletResponse response)throws ServletException,IOException

{

response.setContentType("application/jar");

File file =new File("bootstrap.jar");

byte[] b =newbyte[(int)file.length()];

FileInputStream inputStream =new FileInputStream(file);

inputStream.read(b);

OutputStream outputStream = response.getOutputStream();

outputStream.write(b);

outputStream.flush();

//OutputStream out = response.getOutputStream();

}

}

While running the above servlet in server I get following error:

java.io.FileNotFoundException: bootstrap.jar (The system cannot find the file specified)

java.io.FileInputStream.open(Native Method)

java.io.FileInputStream.<init>(FileInputStream.java:106)

ReadingJarFile.doGet(ReadingJarFile.java:16)

javax.servlet.http.HttpServlet.service(HttpServlet.java:689)

javax.servlet.http.HttpServlet.service(HttpServlet.java:802)

The file bootstrap.jar is present in the bin directory of Tomcat. Please let me know where I am making mistake.

[2496 byte] By [jaaya] at [2007-11-26 19:34:34]
# 1

File file = new File("bootstrap.jar");

this declaration will point to file at located at getServletContext().getRealPath("/")

this path. Give the absolute path of the file like File file = new File("c:/tomcat/bootstrap.jar");

Vishwas_Prasannaa at 2007-7-9 22:08:26 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 2

Ok, After specifying the absolute path I do not get any error and when executing the servlet I get popup with following message.

Do you want to save this file?

Name: GetJarFile

Type:Unknown File Type

From:localhost

However if I change response.setContentType("application/jar"); to response.setContentType("text/html"); then I get the same error except for Name: GetJarFile.zip and Type: WinZip File.

Why has the extension changed.

jaaya at 2007-7-9 22:08:26 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 3

Set file name in the response header

response.setContentType("application/java-archive");

response.setHeader("Content-Disposition","attachment; filename=ting.jar");

may be this will resolve this issue.

Vishwas_Prasannaa at 2007-7-9 22:08:26 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 4
No the issue still exists.
jaaya at 2007-7-9 22:08:26 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 5

Try resetting the response before starting by calling response.reset(). I tried out the same code in my machine and i am getting

Name : ting.jar

Type : Ececutable jar file

from :localhost

the code is as below.

response.reset();

response.setContentType("application/java-archive");

response.setHeader("Content-Disposition","attachment; filename=ting.jar");

File file = new File("c:/commons-pool.jar");

byte[] b = new byte[(int)file.length()];

FileInputStream inputStream = new FileInputStream(file);

inputStream.read(b);

OutputStream outputStream = response.getOutputStream();

outputStream.write(b);

outputStream.flush();

One more reason may be there is no application on your machine to handle jar files. In my machine under folder options->File options am able to see an entry for .jar extension. If not then associate .jat with javaw.exe

Vishwas_Prasannaa at 2007-7-9 22:08:26 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 6
How to associate jar with javaw.exe
AnjanReddya at 2007-7-9 22:08:26 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 7
Refer your OS documentation. For windows XP go to windows explorer and Tools->Folder options->File types -> New
Vishwas_Prasannaa at 2007-7-9 22:08:26 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...