tags:

views:

20

answers:

2

I need to create an ImageIcon from an image on a remote server. When I loaded it with a URL like http://www.server.com/1.png it worked fine.

But recently the server was changed so that the images are stored outside of web root and must be accessed with something like http://www.server.com/get_image.php?id=1, where get_image.php outputs the correct headers to serve the image. When I create an ImageIcon with that type of URL, it fails to load without throwing any exceptions and getImageLoadStatus() returns MediaTracker.ERRORED. The image serving script works fine from a browser.

Any ideas on how could I make this work?

Thanks.

Edit: Here's the Java code.

ImageIcon labelIcon = new ImageIcon(new URL(IMAGE_URL));
System.out.println(labelIcon.getImageLoadStatus());

I also tried the following, but it didn't work either.

ImageIcon labelIcon = new ImageIcon();
BufferedImage image = ImageIO.read(new URL(IMAGE_URL));
labelIcon.setImage(image);

Edit: Here's the code from get_image.php. $DB is an object included from global.php that is used to interact with a MySQL database. I've verified that the MIME type being returned is image/png.

<?php
    if (isset($_GET['uid']))
    {
        require_once('./include/global.php');       
        getUpload(intval($_GET['uid']));
    }

    function getUpload($id)
    {
        global $DB;

        $query = "SELECT `name`, `mime_type` FROM `uploads` WHERE `id` = " . $id . " LIMIT 1";
        $arrUpload = $DB->getSingleRecord($query);

        if (count($arrUpload) > 0)
        {
            $file = UPLOADS_ROOT . $arrUpload['name'];
            header("Content-type: " . $arrUpload['mime_type']);
            header("Content-Disposition: filename=" . $arrUpload['name']);
            readfile($file);
        }
        else
        {
            header("HTTP/1.1 404 Not Found");
        }
        die();
    }
?>
A: 

The server may be returning the wrong content type: make sure that its properly setting the content-type header to image/png on the response.

Michael Brewer-Davis
It looks like the content-type is being set correctly.
A: 

One of the reasons that ImageIcon(URL) may fail is that the response didn't return status 200. Ensure that the URL didn't returned a redirect (status 301/302) or so. Also ensure that the request parameter name is correct. You used id in URL, but the PHP script expects uid.

To play a bit around yourself just to check what response headers the Java code actually gets, try this one:

URL url = new URL("http://www.server.com/get_image.php?id=1");
URLConnection connection = url.openConnection();
for (Entry<String, List<String>> header : connection.getHeaderFields().entrySet()) {
    System.out.println(header.getKey() + "=" + header.getValue());            
}
BalusC
The id/uid mismatch was just a posting error. The code you provided indicates null=[HTTP/1.1 200 OK]. I'll try setting it to 200 explicitly in PHP.
Got it! Changing the header functions in the PHP to explicity return the 200 code as you suggested fixed the problem.Thanks!
You're welcome.
BalusC