Solved

ULRConnection(), example reading image

Posted on 1998-07-01
9
271 Views
Last Modified: 2010-05-18
I have got the connection to an image/jpeg type now I have to draw it.  so far

      url = new URL(getCodeBase(), "current.jpg");
      urlc = url.openConnection();
      urlc.connect();
      System.out.println("Content type is " + urlc.getContentType();

Now I have to read it and build an Image.    
Anyone have an example or something to get me started?
I don't want to use getImage(URL);
Thanks in advance.
0
Comment
Question by:hank1
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 4
9 Comments
 
LVL 6

Expert Comment

by:gadio
ID: 1224323
ude the folowing:

InputStream is = urlc.getInputStream();
// ....
// loop and read all the file through the input stream into a byte array buf[] .
// ....
Image res_image = Toolkit.getDefaultToolkit().createImage( buf );

Hope that helps.
G.
0
 
LVL 1

Author Comment

by:hank1
ID: 1224324
Back at work on Tuesday (6/7?).  Give it a try and award points then if successful.
Thanks.  Have a good 4th - if you're a yankee anyway. :)
0
 
LVL 1

Author Comment

by:hank1
ID: 1224325

Localized InputStream inside paint() to "implement" rewind.  However I get only
one good read. The stream is closed properly (As I understand it, when all references to
the stream are lost, the gc will close it anyway...right?) Next time into paint(),
I get a good InputStream (is not null) but my read throws an IOEx. with "read error".

Attempting to localize the URLConnection was a looping experience.  Paint never stopped.
0
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

 
LVL 6

Expert Comment

by:gadio
ID: 1224326
hank1, hope you had a happy 4th. :)
You shouldn't do IO operations inside paint, since they take a long time to finish and the UI will be frozen in that time. Instead you should prapare all you need for a paint before (or in another thread. The answer I have written before should be done outside the paint, and also I think that you should use the getInputStream() only once, loop through it, and onl then close it. Give it a try.
Regards,
G.
0
 
LVL 1

Author Comment

by:hank1
ID: 1224327
appletviewer runs this "fine".  Spin this up in nc4.05 and I get stream closed
exception after one successful read.  Hate to dump code but here it is, stripped to the
minimum.

  URL url;
  URLConnection urlc;
  Image eye;
  InputStream is;

  public void run() {
    while (true) {
      repaint();
      try {
        buildNoCacheImage();
        Thread.sleep(15L * 1000L);  // wait 15 seconds
      } catch (InterruptedException ie) {
        // ignore
      }
    }
  }

  public void buildNoCacheImage() {
    try {
      url = new URL(getCodeBase(), "current.jpg");
      urlc = url.openConnection();
      urlc.setUseCaches(false);
      urlc.connect();
      is = urlc.getInputStream();
      index = 0;
      read = 0;
      while ((read = is.read(b, index, 1000)) != -1) {
        index += read;
      }
      System.out.println("index/read " + index + "/" + read);
      is.close();
      eye = Toolkit.getDefaultToolkit().createImage(b);
    } catch (Exception e) {
      e.printStackTrace();
    }
  }
  int index = 0;
  int read = 0;

0
 
LVL 1

Author Comment

by:hank1
ID: 1224328
gadio come get your points.
0
 
LVL 6

Expert Comment

by:gadio
ID: 1224329
I think I know what the problem is. I'm a bit tied up at work now, I'll try and give you an answer today or tomorrow the latest.
:-)
0
 
LVL 1

Author Comment

by:hank1
ID: 1224330
No, it works ok.  I was running into server image update activity.
The other previous problem was, like most filters, closing the inputstream
closed the URLConnection.  I just open everything up again.
Thanks for your help.  Come and get'um.
0
 
LVL 6

Accepted Solution

by:
gadio earned 100 total points
ID: 1224331
OK, I'll quote both my answers again:
-------
Use:

      InputStream is = urlc.getInputStream();
      // ....
      // loop and read all the file through the input stream into a byte array buf[] .
      // ....
      Image res_image = Toolkit.getDefaultToolkit().createImage( buf );
--------------
You shouldn't do IO operations inside paint, since they take a long time to finish and the UI will be frozen in that time. Instead you should prapare all you need for a paint before (or in another thread. The answer I have written before should be done outside the paint, and also I think that you should use the getInputStream() only once, loop through it, and onl then close it. Give it a try.
Regards,
 G.
0

Featured Post

The Ultimate Checklist to Optimize Your Website

Websites are getting bigger and complicated by the day. Video, images, custom fonts are all great for showcasing your product/service. But the price to pay in terms of reduced page load times and ultimately, decreased sales, can lead to some difficult decisions about what to cut.

Question has a verified solution.

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

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

729 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