Home > Back-end >  How to get pictures from mounted volume in java spring boot
How to get pictures from mounted volume in java spring boot

Time:12-05

I have a java application in a docker container and saving pictures is working fine but getting them doesnt work I get Error: javax.imageio.IIOException: Can't read input file!.

this is my doker image,

FROM openjdk:17-jdk-alpine
ARG JAR_FILE=*.jar
EXPOSE 9000
COPY  build/libs/wall-0.0.1-SNAPSHOT.jar .
ENTRYPOINT ["java","-jar","wall-0.0.1-SNAPSHOT.jar"]

The mounting part is being done in the kubernetes cluster configuration:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: wall-app
  labels:
    app: wall-app
spec:
  replicas: 1
  selector:
    matchLabels:
      app: wall-app
  template:
    metadata:
      labels:
        app: wall-app
    spec:
      containers:
      - name: wall-app
        image: u/wall
        imagePullPolicy: Always
        ports:
        - containerPort: 9010
        resources:
          limits:
            memory: 512Mi
        volumeMounts:
        - name: images-volume
          mountPath: /images
      volumes:
      - name: images-volume
        hostPath:
          path: /home/ubuntu/images-volume

My java application function to get image: Not: Im sure that the pic name is corret the probelm is with the input stream

    @PostMapping(value = "/post/image")
    public @ResponseBody String getImageAsBase64(@RequestBody String json){

        try {
            JSONObject jsonObject = new JSONObject(json);
            String path = "/images/post/"   jsonObject.get("postId)   "/"        jsonObject.get("pictureName");

            InputStream in = getClass()
                    .getResourceAsStream(path);

            String encodedString = Base64.getEncoder().encodeToString(IOUtils.toByteArray(in));

           System.out.print(path);
           //images/post/5/test.png

            return encodedString;
        }catch(Exception e) {
            System.out.println("picture not found" e);
        }

        return null;
    }

This files inside the container were you can see the mounted volume"images"

I tried ../ to get one directory above but it did not work

CodePudding user response:

That getResourceAsStream method is only to be used for reading resources (files) from the classpath, which is a rather logical thing. As far as I can see, you want to read the images directly from the file system, outside of your classpath as can be observed from your ENTRYPOINT. For that, you can use FileInputStream. So, you have to replace the in variable with the following code:

InputStream in = new FileInputStream(path);

But don't forget about closing the input stream. You can use the convenient try-with-resources statement for that purpose. After that, your relevant piece of code will look like this:

try (InputStream in = new FileInputStream(path)) {

    String encodedString = Base64.getEncoder().encodeToString(IOUtils.toByteArray(in));

    System.out.print(path);
    //images/post/5/test.png

    return encodedString;
}
  • Related