0

I'm uploading files with this method:

@Override
public void store(MultipartFile file, AnimalEntity animalEntity) {
    String filename = animalEntity.getNameString() + animalEntity.getAge() + ".jpg";
    try (InputStream inputStream = file.getInputStream()) {
            Files.copy(inputStream, this.rootLocation.resolve(filename), StandardCopyOption.REPLACE_EXISTING);
        }
    } catch (IOException e) {
        throw new StorageException("Failed to store file " + filename, e);
    }

And showing image with:

<img alt="" src="" th:alt="${dog.id}" th:src="@{${dog.getNameString()} + ${dog.getAge()} + '.jpg'}"> 

Images are visible only after restarting application. I don't know why and how to solve this problem.

BTW How can i point files instead of using whole path:

private String location = "C:\\Users\\XXX\\Documents\\Projekty\\IntelliJ\\shelter\\src\\main\\resources\\static\\photos";

?

1 Answers1

0

The application that displays the image loads it before you replace it, so you would have to detect when there is a change. You can do that with a WatchService, already answered here: Java how to detect file changes

Master Fry
  • 46
  • 2
  • I will try this. What about link to a folder? I dont want to use path "C:\\Users\\XXX\\Documents\\Projekty\\IntelliJ\\shelter\\src\\main\\resources\\static\\photos"; because on other computer it might not work –  Feb 04 '19 at 09:06
  • You could create a folder in the user directory that is used by both applications. To get the user path use `System.getProperty("user.home")` – Master Fry Feb 04 '19 at 18:26
  • Ok, i read about WatchService and i do not know where to use it. I save file using method mentioned above, then simple i load it using HTML tag. I do not know which piece of code should be processed after directory's change is detected. –  Feb 05 '19 at 11:18
  • Well after you detect a change in the directory, you simply reload the image from the file. Then you will have the updated image in your program. Also, if you might have a lot of files in that directory, it might be wise to check if the file was actually modified before reloading it. https://docs.oracle.com/javase/9/docs/api/java/io/File.html#lastModified-- – Master Fry Feb 05 '19 at 18:26