Question

How do I get the images I stored in my web pages folder out so I can read them into a buffered image?

I found many explanations online on how it works but it's so confusing! Maybe someone could help me by explaining it to me in a scenario familiar to me?

This is my server folder tree:

Folder tree

I want to read PNG pictures from images in Web Pages from within my ItemStorage class. Here's what that class looks like:

import java.util.ArrayList;
import java.util.List;

import be.pxl.minecraft.model.Item;
import com.sun.jersey.spi.resource.Singleton;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.ImageIO;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Response;

@Singleton
public class ItemStorage {
    private ImageStorage test;
    private HashMap<String, Image> categories;
    private HashMap<String, BufferedImage> images;
    private List<Item> recipesList;

    public ItemStorage() {

        File directory = new File("/images");
        if (directory.isDirectory()) { //FILE PATH NOT A DIRECTORY
            BufferedImage img = null;
            for (File f : directory.listFiles()) {
                try {
                    img = ImageIO.read(f);
                    images.put(f.getName(), img);
                } catch (IOException ex) {
                    Logger.getLogger(ItemStorage.class.getName()).log(Level.SEVERE, "Error loading image", ex);
                }
            }
        }

        recipesList = new ArrayList<Item>();
        //BufferedImage air = getImage("air"); //TRIED DIFFERENT APPROACH, SEE getImage()

        //Armor
        recipesList.add(new Item(7, 2, getImage("diamond_boots"), "Boots (Diamond)",
                        "0,0,0,1,0,1,1,0,1", String.format("%d,%d", getImage("air"), R.drawable.diamond_ingot )));
    }

    public void setItems(List<Item> list) {
        recipesList = list;
    }

    public List<Item> getItems() {
        return recipesList;
    }

    @Path("/images")
    @Produces("image/png")
    public Response getImage(String imageName) { //TRYING TO HTTP TO THE IMAGE
        BufferedImage img = null;
        try {
            File imageFile = new File(imageName + ".png");
            img = ImageIO.read(imageFile);
            return Response.ok(img).build();
        } catch (IOException ex) {
            Logger.getLogger(ItemStorage.class.getName()).log(Level.SEVERE, "Error loading image", ex);
        } finally {
            return Response.ok(img).build();
        }
    }
}

As you can see, I tried to call the images both through HTTP and a simple IO directory. This is a restful server running under tomcat 7.0.41.0 Please collaborate.

Was it helpful?

Solution

what puzzles me with your approach is, that you access a file from within your service, that is supposed to be located at the "root" ("/") directory. This is not inside your application.

In the rest service, there is no "context", as you have it in the servlet world. You need to identify the images folder somehow using a config option. You can also check the documentation of your REST server to see how to access the MessageContext and HTTPRequest. Then you can use them to access your web-application's runtime folder and access the images.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top