jpeg images not loading correctly

i am not getting the preview of downloaded jpeg image. how can i make it work? the code follows.....


class  Test1
public void download (URL url) throws IOException
URLConnection conn = url.openConnection();
int length = conn.getContentLength();
int needed = length;
InputStream is = conn.getInputStream();
System.out.println("is.available = "+is.available());
InputStreamReader reader = new InputStreamReader (is);

int buflength = 0;
byte[] buf = new byte[length];

buflength +=, buflength, length-buflength);
/*while (buflength!=length)
System.out.println("is.available = "+is.available());;
System.out.println("read "+buflength+" out of "+length+" bytes");
} */

FileOutputStream fw = new FileOutputStream("c:\\abc\\trial.jpg");


class Test
public static void main(String args[])
              URL h = new URL("");    // load this image
              Test1 T = new Test1();
              }catch ( e ) {System.out.println(e);}
                 catch ( e) {e.printStackTrace();}
Who is Participating?

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

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.

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

       Runtime.getRuntime().exec("cmd /C c:\\abc\\trial.jpg"); //<-- use command (instead of cmd) if win 95/98/me
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?
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();
            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 = > -1) {
                  fw.write(buf, 0, bytesRead);

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.

Sorry - forgot to delete


(it's redundant)
Mayank SAssociate Director - Product EngineeringCommented:
>> 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 ;-)
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.
>>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 ?
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

From novice to tech pro — start learning today.