InputStream through URL ,problem while rendering the file

hello,

i am fetching the file through URL

-

URL url=new URL("http://localhost:8080/dashboardStore/DashboardDownload?reportId=" + reportId + "&aggregateId=" +aggregateId+"&fileType="+filetype);

try {

URLConnection con=url.openConnection();

con.connect();

InputStream is=url.openStream();

//int len=is.available();

bbuff=new byte[is.available()];

is.read(bbuff);

int len=bbuff.length;

System.out.println("length is............"+len);

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

--

here i am convering byte[] to file

try {

file=File.createTempFile("temp","." + filetype.toLowerCase(),new File("C:/tmpdnld"));

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

ByteArrayInputStream ba = new ByteArrayInputStream(attachment);

FileOutputStream fos=null;

try {

fos = new FileOutputStream(file);

} catch (FileNotFoundException e1) {

// TODO Auto-generated catch block

e1.printStackTrace();

}

int n=0;

while((n = ba.read()) >= 0) {

try {

fos.write(attachment,0,n);

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

try {

fos.close();

} catch (IOException e1) {

// TODO Auto-generated catch block

e1.printStackTrace();

}

try {

ba.close();

} catch (IOException e1) {

// TODO Auto-generated catch block

e1.printStackTrace();

while rendering it's showing some scrap

pls...help me out

[1727 byte] By [ettasrinua] at [2007-11-27 1:20:48]
# 1

> bbuff=new byte[is.available()];

Here you are misusing the available() function. See http://java.sun.com/j2se/1.5.0/docs/api/java/io/InputStream.html#available().

> is.read(bbuff);

Here you are ignoring the value returned by the read(), which could be -1 or any value between 1 and bbuff.length.

> int len=bbuff.length;

> System.out.println("length is............"+len);

and here you therefore have a false assumption leading to incorrect output.

> here i am convering byte[] to file

Here you don't even know that you have the entire input yet.

> ByteArrayInputStream ba = new ByteArrayInputStream(attachment);

What is 'attachment'?

> } catch (FileNotFoundException e1) {

This catch is in the wrong place. At the moment if the exception occurs you are continuing execution with a possibly null 'fos'. Use a single try/catch block to catch all the exceptions in this method, or better still just let them be thrown to the caller.

> while((n = ba.read()) >= 0) {

So you're reading one byte 'n' from 'ba' ...

>try {

> dos.write(attachment,0,n);

and writing the first 'n' bytes from 'attachment' every time. I don't know what this is supposed to achieve, and I don't think you do either. It is nonsense. I'm surprised it doesn't give an ArrayIndexOutOfBoundsException.

> while rendering it's showing some scrap

Of course it is.

This entire mess can be written like this, once you have the input and output streams, and assuming a single try/catch block around the whole thing, or none:

byte[] buffer = new byte[8192];

int count;

while ((count = in.read(buffer)) > 0)

fos.write(buffer, 0, count);

fos.close();

in.close();

ejpa at 2007-7-11 23:58:08 > top of Java-index,Java Essentials,Java Programming...