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();}
            
}
}
cofactorAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

gnoonCommented:
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();}
Mayank SAssociate Director - Product EngineeringCommented:
>> 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?
CEHJCommented:
You should really read streams to EOF or the right signals will sometimes not be sent to the OS. Also, it renders your getContentLength (sometimes unreliable) unnecessary, also it will guard against the possibility of memory errors if the source is a very large file:



class Test1 {

      public void download(URL url) throws IOException {
            URLConnection conn = url.openConnection();
            conn.connect();
            InputStream is = conn.getInputStream();
            final int BUF_LENGTH = 1 << 10 << 3; //8KiB buffer
            byte[] buf = new byte[BUF_LENGTH];
            int bytesRead = -1;
            String path = "c:\\abc\\trial.jpg";
            FileOutputStream fw = new FileOutputStream(path);
            while ((bytesRead = is.read(buf)) > -1) {
                  fw.write(buf, 0, bytesRead);
            }
            is.close();
            fw.close();
      }
}

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Exploring SQL Server 2016: Fundamentals

Learn the fundamentals of Microsoft SQL Server, a relational database management system that stores and retrieves data when requested by other software applications.

CEHJCommented:
Sorry - forgot to delete

>>conn.connect();

(it's redundant)
Mayank SAssociate Director - Product EngineeringCommented:
>> fw.write(buf);

Since its not a BufferedStream, that might not be needed.
CEHJCommented:
>>that might not be needed.

It certainly is if you're interested in producing a file ;-)
Mayank SAssociate Director - Product EngineeringCommented:
I don't use it with FileOutputStream.
Mayank SAssociate Director - Product EngineeringCommented:
Even your code doesn't have it :) I was saying that to cofactor as his code had it.
CEHJCommented:
>>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
Mayank SAssociate Director - Product EngineeringCommented:
That is indeed correct. I was actually pointing to cofactor to use that approach and in that case, flush () is not required.
cofactorAuthor Commented:
>> cmd /C

why  /C ?
CEHJCommented:
8-)
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.