Link to home
Start Free TrialLog in
Avatar of Reggie_BE
Reggie_BE

asked on

Re-Request image from webserver (java.net.URL)

Hi,

I wrote something that reads an image from a website.
For example http://www.myblog.com/mywebcam gives me an image of my webcam. Now that image changes every second.
How can I re-request the image ? Do I have to disconnect and connect again ? Or can I just resend the headers ?
Cause I can't find any info about how to do that using the Java.net.URL class :\ It's HTTP/1.1
ASKER CERTIFIED SOLUTION
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Reggie_BE
Reggie_BE

ASKER

Yeah .. I think I need to use URLConnection.getContent() .. found it just after I asked my question :s
Now I'm trying to save an Image object to a jpeg file .. can find how to save BufferedImage, but that's not what I need :(
ImageIO.write(img, "JPG", outputStream);
That only accepts File or BufferedImage.
This is the way I have it now:

     while(connected) {
          saveImage(getImage(uc), "test" + i++ + ".jpg");
          this.sleep(1000);
     }

    public void saveImage(Image img, String filename) {
        try {
            ImageIO.write(img, "jpg", new File(filename)); //  Can't find symbol method write(javax.awt.Image, String, File)
        } catch (Exception e) {
            System.out.println(e);
        }
    }
   
    private void saveBytesToFile(byte[] buff, String filename) {
        try {        
            FileOutputStream fos = new FileOutputStream(new File(filename));
            fos.write(buff);
        } catch (Exception e) {
            System.out.println(e);
        }
    }

This is how I used to do it, but when I repeat that, he will write the first image, but after that i get "null" as errormessage
   while (connected) {
       int bufferSize = uc.getContentLength();
       DataInputStream dis = new DataInputStream(uc.getInputStream());
       byte[] buf = new byte[bufferSize];
       dis.readFully(buf);
       saveBytesToFile(buf, "test_" + i++ + ".jpg");
   }
   private void saveBytesToFile(byte[] buff, String filename) {
        try {        
            FileOutputStream fos = new FileOutputStream(new File(filename));
            fos.write(buff);
        } catch (Exception e) {
            System.out.println(e);
        }
    }
>>Now I'm trying to save an Image object to a jpeg file .. can find how to save BufferedImage, but that's not what I need :(

Why has it changed and where does it come in to your original?
Why has what changed ?
The problem is when I use uc.getInputStream() I can only call it once. The 2nd time it gives me null. So I figured out that I had to use uc.getContent(), but then I can't save the file properly :s
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
>>The 2nd time it gives me null.

Please post the code you're using to do this
> The problem is when I use uc.getInputStream() I can only call it once. The 2nd time it gives me null. So I figured out that I had to use uc.getContent(), but then I can't save the file properly :s

you'll need to call it once for each file.
ie. each request returns a single frame.

To stream the requests you need to implement some way to mark where each frame ends.
For example send the size of the frame, followed by the b=frame bytes, followed by the size of the next frame etc.
@CEHJ: the code's above, 2 different ways to fetch and save the image.
@objects:
> For example send the size of the frame, followed by the b=frame bytes, followed by the size of the next frame etc.
Do u mean by sending out Content-Length ? Or do I just have to disconnect and reconnect every second ?
no by including the frame size in the stream

o/wise you need to reconnect to get each frame.
I'm sorry, I'm not following... "including the frame size in the stream", where do I have to send out the frame size ? In the headers ?

    private void sendHttpRequest() {
        try {
            /* Post header */
            Ticket ticket = new Ticket(); // The ticket is something that webserver uses...
            uc.setRequestProperty("Host", this.hostname);
            uc.setRequestProperty("Ticket-Data", ticket.getData());
            uc.setRequestProperty("User-Agent", "jsyntax");
            uc.setRequestProperty("Ticket-Id", ticket.getId());
            uc.connect();
        } catch (Exception e) {
            System.out.println("sendHttpRequest: " + e.getMessage());
        }
    }
    public void startReceiving() {
        int i = 0;
       
        try {
            sendHttpRequest();
           
            while(uc.getContentLength() > 0) {
                DataInputStream dis = new DataInputStream(uc.getInputStream());
                int bufferSize = uc.getContentLength();
                byte[] buf = new byte[bufferSize];
                dis.readFully(buf);
                saveBytesToFile(buf, "test_" + i++ + ".jpg");
                this.sleep(1000);
            }
        } catch (Exception e) {
            System.out.println("startReceiving: " + e.getMessage());
        }
    }
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial