?
Solved

ULRConnection(), example reading image

Posted on 1998-07-01
9
Medium Priority
?
274 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
Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

 
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 300 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

Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

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…
In this post we will learn different types of Android Layout and some basics of an Android App.
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
Suggested Courses
Course of the Month13 days, 4 hours left to enroll

777 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