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
Reggie_BEAsked:
Who is Participating?
 
CEHJCommented:
You need to request it in a loop
0
 
Reggie_BEAuthor Commented:
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 :(
0
 
hoomanvCommented:
ImageIO.write(img, "JPG", outputStream);
0
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
Reggie_BEAuthor Commented:
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);
        }
    }
0
 
CEHJCommented:
>>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?
0
 
Reggie_BEAuthor Commented:
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
0
 
CEHJCommented:
>>The 2nd time it gives me null.

Please post the code you're using to do this
0
 
objectsCommented:
> 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.
0
 
Reggie_BEAuthor Commented:
@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 ?
0
 
objectsCommented:
no by including the frame size in the stream

o/wise you need to reconnect to get each frame.
0
 
Reggie_BEAuthor Commented:
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());
        }
    }
0
 
objectsCommented:
no, in your data stream. Otherwise the reciving end won't know where one image ends and the next one starts.


while not eof
   read size of frame
   read frame
end while
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.