Sending jar file from a servlet

Hi guys,

Itried sendind a jar file from my webserver with d ffg snippet

if(url != ""){

try{

File app = new File(url);

FileInputStream fileInputStream = new FileInputStream(app);

response.setContentType("application/octet-stream");

response.setHeader("Content-Disposition","Attachment");

int i;

while ((i=fileInputStream.read())!=-1){

out.write(i);

}

fileInputStream.close();

}

but the on the mobile device, it says no response.

I am try with nokia 6070 with a jar file.

[566 byte] By [lekkie.aydota] at [2007-11-26 16:40:11]
# 1

If your object "url" is a URL object then the if block will never get executed.

Try creating a URL object:

URL url = new URL("path literal");

If it is a String object then try,

if ("".equals(url)) {

maple_shafta at 2007-7-8 23:07:06 > top of Java-index,Desktop,Deploying...
# 2

I dont have issues with the comparison.

It writes the jar file to a stream but somehow it never gets to the mobile device.

url is a string (a literal path to the jar file).

"

String url = fetcher.GetApp(msisdn,request,getServletContext());

"

are u saying i shld try

URL appURL = new URL(url);

this is the outstream am using dont u think it shld be OutputStream out - response.getOutputStream?

?PrintWriter out = response.getWriter();?

Message was edited by:

lekkie.aydot

lekkie.aydota at 2007-7-8 23:07:06 > top of Java-index,Desktop,Deploying...
# 3
You should use URL's when communicating in a client-server way.In fact you should almost always use URL's to point to resources.I am not sure what you are asking me though...
maple_shafta at 2007-7-8 23:07:06 > top of Java-index,Desktop,Deploying...
# 4

but the truth is, its in a file on the server and I av passed it into the o/p stream

PrintWriter out = response.getWriter();

OutputStream appOut = response.getOutputStream();

String url = fetcher.GetApp(msisdn,request,getServletContext());

if(url != ""){

File app = new File(url);

FileInputStream fileInputStream = new FileInputStream(app);

if(url.endsWith("jad")){

response.setContentType("application/jad");

}else{

response.setContentType("application/jar");

}

//response.setHeader("Content-Disposition","Attachment");

int i;

while ((i=fileInputStream.read())!=-1){

appOut.write(i);

}

fileInputStream.close();

}

else{

response.setContentType("text/html");

out.println("Mobility for your phone is not yet available");

}

appOut.flush();

appOut.close();

out.flush();

out.close();

I tried this also, it didnt work.

lekkie.aydota at 2007-7-8 23:07:06 > top of Java-index,Desktop,Deploying...
# 5
Can I see your class so I can test your code?Please also use the code tags. It makes it easier to read.
maple_shafta at 2007-7-8 23:07:06 > top of Java-index,Desktop,Deploying...
# 6

OutputStream out = response.getOutputStream();

PrintWriter writer = new PrintWriter(out);

//PrintWriter out = response.getWriter();

//OutputStream appOut = response.getOutputStream();

String msisdn = request.getParameter("sender");

if(msisdn == null){

msisdn = "";

}

URLFetcher fetcher = new URLFetcher();

String url = fetcher.GetApp(msisdn,request,getServletContext());

if(url != ""){

try{

File app = new File(url);

FileInputStream fileInputStream = new FileInputStream(app);

response.setStatus(HttpServletResponse.SC_OK);

if(url.endsWith("jad")){

response.setContentType("text/vnd.sun.j2me.app-descriptor");

}

else{

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

}

int len = (int) app.length();

response.setContentLength(len);

BufferedInputStream bis = new BufferedInputStream(fileInputStream);

if (len > 0)

throw new Exception();

//int ch;

byte fileData[] = new byte[1024];

int res;

res = bis.read(fileData, 0, 1024);

while(res != -1){

out.write(fileData, 0, res);

out.flush();

res = bis.read(fileData, 0, 1024);

}

out.close();

bis.close();

}

catch(Exception e){

response.setContentType("text/html");

writer.println("Error getting U-Mobile application");

}

}

else{

response.setContentType("text/html");

writer.println("Mobility for your phone is not yet available");

}

writer.close();

Are dia any size issues considered when downloading on mobile phones? The jar size is 130KB

Message was edited by:

lekkie.aydot

lekkie.aydota at 2007-7-8 23:07:06 > top of Java-index,Desktop,Deploying...
# 7
Does anybody knows where to add the appplication/java-archive header to tomcat settings cos it seems my tomcat cannot serve a jar file.It returns webserver returned is too large messageon a sony ericsson phone.
lekkie.aydota at 2007-7-8 23:07:06 > top of Java-index,Desktop,Deploying...
# 8

You add them to either httpd.conf or .htaccess on your web server.

Add these lines:

AddType text/vnd.sun.j2me.app-descriptor jad

AddType application/java-archive jar

You are serving J2ME so make sure you have this line in there as well:

AddType text/vnd.wap.wml wml

You don't have a MIME type specified for JAD or JAR files so that is why your phone can't download them.

There is no special reason I can think of why a phone COULD'NT download a JAR file. As far as I am concerned, you are sending it as an ordinary file.

So this question really doesn't have very much to do with JAR files, but should have been posted in the J2ME section.

You should probably direct your J2ME questions there in the future.

maple_shafta at 2007-7-8 23:07:06 > top of Java-index,Desktop,Deploying...