?
Solved

jpeg images not loading correctly

Posted on 2004-11-09
12
Medium Priority
?
200 Views
Last Modified: 2010-03-31
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();}
            
}
}
0
Comment
Question by:cofactor
12 Comments
 
LVL 16

Expert Comment

by:gnoon
ID: 12541777
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();}
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 12541979
>> 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?
0
 
LVL 86

Accepted Solution

by:
CEHJ earned 160 total points
ID: 12542937
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();
      }
}

0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 86

Expert Comment

by:CEHJ
ID: 12542947
Sorry - forgot to delete

>>conn.connect();

(it's redundant)
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 12543095
>> fw.write(buf);

Since its not a BufferedStream, that might not be needed.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 12543119
>>that might not be needed.

It certainly is if you're interested in producing a file ;-)
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 12552124
I don't use it with FileOutputStream.
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 12552131
Even your code doesn't have it :) I was saying that to cofactor as his code had it.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 12554181
>>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
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 12554564
That is indeed correct. I was actually pointing to cofactor to use that approach and in that case, flush () is not required.
0
 

Author Comment

by:cofactor
ID: 12573093
>> cmd /C

why  /C ?
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 12573415
8-)
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
The viewer will learn how to implement Singleton Design Pattern in Java.
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
Suggested Courses
Course of the Month12 days, 19 hours left to enroll

579 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question