cofactor
asked on
jpeg images not loading correctly
i am not getting the preview of downloaded jpeg image. how can i make it work? the code follows.....
code
------
import java.io.*;
import java.net.URL;
import java.net.URLConnection;
class Test1
{
public void download (URL url) throws IOException
{
URLConnection conn = url.openConnection();
conn.connect();
int length = conn.getContentLength();
int needed = length;
InputStream is = conn.getInputStream();
System.out.println("hello" );
System.out.println("is.ava ilable = "+is.available());
InputStreamReader reader = new InputStreamReader (is);
int buflength = 0;
byte[] buf = new byte[length];
buflength += is.read(buf, buflength, length-buflength);
/*while (buflength!=length)
{
System.out.println("is.ava ilable = "+is.available());
buflength+=is.read(buf);
//buflength+=reader.read(b uf);
System.out.println("read "+buflength+" out of "+length+" bytes");
} */
FileOutputStream fw = new FileOutputStream("c:\\abc\ \trial.jpg ");
fw.write(buf);
fw.flush();
fw.close();
}
}
class Test
{
public static void main(String args[])
{
try
{
URL h = new URL("http://search.cpan.org/src/LGODDARD/Image-ThousandWords-0.08/rose.jpg"); // load this image
Test1 T = new Test1();
T.download(h);
}catch (java.net.MalformedURLExce ption e ) {System.out.println(e);}
catch (java.io.IOException e) {e.printStackTrace();}
}
}
code
------
import java.io.*;
import java.net.URL;
import java.net.URLConnection;
class Test1
{
public void download (URL url) throws IOException
{
URLConnection conn = url.openConnection();
conn.connect();
int length = conn.getContentLength();
int needed = length;
InputStream is = conn.getInputStream();
System.out.println("hello"
System.out.println("is.ava
InputStreamReader reader = new InputStreamReader (is);
int buflength = 0;
byte[] buf = new byte[length];
buflength += is.read(buf, buflength, length-buflength);
/*while (buflength!=length)
{
System.out.println("is.ava
buflength+=is.read(buf);
//buflength+=reader.read(b
System.out.println("read "+buflength+" out of "+length+" bytes");
} */
FileOutputStream fw = new FileOutputStream("c:\\abc\
fw.write(buf);
fw.flush();
fw.close();
}
}
class Test
{
public static void main(String args[])
{
try
{
URL h = new URL("http://search.cpan.org/src/LGODDARD/Image-ThousandWords-0.08/rose.jpg"); // load this image
Test1 T = new Test1();
T.download(h);
}catch (java.net.MalformedURLExce
catch (java.io.IOException e) {e.printStackTrace();}
}
}
>> i am not getting the preview of downloaded jpeg image
Are you trying to give the path for the image in an HTML page and viewing it on the browser? Does it show a corss-mark?
Are you trying to give the path for the image in an HTML page and viewing it on the browser? Does it show a corss-mark?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Sorry - forgot to delete
>>conn.connect();
(it's redundant)
>>conn.connect();
(it's redundant)
>> fw.write(buf);
Since its not a BufferedStream, that might not be needed.
Since its not a BufferedStream, that might not be needed.
>>that might not be needed.
It certainly is if you're interested in producing a file ;-)
It certainly is if you're interested in producing a file ;-)
I don't use it with FileOutputStream.
Even your code doesn't have it :) I was saying that to cofactor as his code had it.
>>Even your code doesn't have it
My code has this overloaded form
>>fw.write(buf, 0, bytesRead);
which again is needed if you want to produce a file
My code has this overloaded form
>>fw.write(buf, 0, bytesRead);
which again is needed if you want to produce a file
That is indeed correct. I was actually pointing to cofactor to use that approach and in that case, flush () is not required.
ASKER
>> cmd /C
why /C ?
why /C ?
8-)
>i am not getting the preview of downloaded jpeg image
try this code at line after the writing line
try{
Runtime.getRuntime().exec(
}catch(Exception){e.printS