createInputStream method in InputStreamFactory returns null?

Hi, I am making application which uploads image files, saves them in resources and next read them and show in Scroller. Files are saving properly, but they don’t appear. What is surprising, when I rebuilded the project, everything started to work properly (files saved before rebuild are displayed), but when I add a new image, it doesn’t appear (after page reloading it still doesn’t work) even though in both cases the same function is invoked…
I got such exception (more in the attachment):

...```

Here the problematic code fragment:
```java
    public MessageListItem(List<Attachment> attachments, String text, Instant time, String userName, AbstractStreamResource avatarResource) {
        this(text, time, userName, avatarResource);

        for (Attachment attachment : attachments) {
            StreamResource streamResource = new StreamResource(attachment.toString(), attachment.getImage());
            Image contentImage = new Image(streamResource, "alt");
            wrapper.add(contentImage);
        }
    }```
I am getting file from resources, creating InputStreamFactory by lambda expression, and next creating Attachement object.
```java
@Getter
@AllArgsConstructor
public class Attachment {
    private InputStreamFactory image;
    private String mime;
}```
![image.png|157x68](upload://kjxIVcPaXBgq1ieeR87NgqLRPVV.png)
![image.png|1781x762](upload://b2avELLHLiK4494iZg1xVXRzgvX.png)

What do you mean with “saves them in resources”?

I mean spring project resources
image.png

That can’t work. Are you expecting to write in your production jar/war?

I do the same thing with user avatars and it works, I’m also getting InputStream → creating StreamResource and creating avatar with this… Why it can’t work now?

So you are telling me you have an application somewhere running where you write in the running application’s jar and losing all uploaded avatars once a new version is deployed? :sweat_smile:

Oh… OH… luckily it’s a student project and no one will use that :laughing:

So, what do you suggest?

Adding the files to the filesystem or another storage system like database; but for a student project - the filesystem next to the app would probably the easiest

https://docs.spring.io/spring-framework/reference/core/resources.html you can use File or Path Resources; those can access the files and get you the input streams you need (always)

Okay thx, I still have a lot to learn :sweat_smile: