Link to home
Start Free TrialLog in
Avatar of cofactor
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.available = "+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.available = "+is.available());
buflength+=is.read(buf);
//buflength+=reader.read(buf);
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.MalformedURLException e ) {System.out.println(e);}
                 catch (java.io.IOException e) {e.printStackTrace();}
            
}
}
Avatar of gnoon
gnoon
Flag of Thailand image

The downloaded file is at c:\abc\trial.jpg.

>i am not getting the preview of downloaded jpeg image

try this code at line after the writing line

    try{
       Runtime.getRuntime().exec("cmd /C c:\\abc\\trial.jpg"); //<-- use command (instead of cmd) if win 95/98/me
    }catch(Exception){e.printStackTrace();}
Avatar of Mayank S
>> 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?
ASKER CERTIFIED SOLUTION
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Sorry - forgot to delete

>>conn.connect();

(it's redundant)
>> fw.write(buf);

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 ;-)
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
That is indeed correct. I was actually pointing to cofactor to use that approach and in that case, flush () is not required.
Avatar of cofactor
cofactor

ASKER

>> cmd /C

why  /C ?
8-)