[Java] The problem that uploaded images are not updated due to the influence of cache

Overview

It's okay to upload the image with the web app I'm creating, but I'm stuck with the problem that the uploaded image is not updated on the screen, so I'll make a note for myself. It seems that the cause is that the cache is bad and the image before update is read as it is.

Solution

From the conclusion, I was able to solve it by adding? (Some value) to "<img src = ~" of the image displayed on the jsp side and letting the browser recognize it as a different image. ..

test.jsp


//Change before
<img src="static/img/test.jpg " alt="test.jpg ">
//After change
<img src="static/img/test.jpg?(Any dynamic parameter)" alt="test.jpg ">

If the URL of the image is exactly the same and nothing is changed, the browser will judge ** "Use the cache" **, so it seems that the update was not reflected.

Assign parameters with lastModified

Since it is an arbitrary unique parameter, anything is fine, but since it is a big deal, I will try to get the value automatically and give it. Get the uploaded date and time with lastModified and give it.

Controller.java


//Select the target file
File file = new File("static/img/test.jpg ");
//Get the last modified date and time of the file and make it available in jsp
model.addAttribute("file", file.lastModified());

test.jsp


<img src="/static/img/test.jpg?${file}" alt="test.jpg ">

Now when you update the file, the updated one will be loaded.

reference

-Spring Boot supports browser cache of static resources ・ [Don't say "it's because of the cache"! How to make sure that only updated files have their cache disabled] (https://qiita.com/laineus/items/ae9c9117cbcbd5333035) -[Discard the browser cache and load only updated JS, CSS, and images] (https://qiita.com/ka215/items/20139918642fed39fb5d)

Recommended Posts

[Java] The problem that uploaded images are not updated due to the influence of cache
[Java] [Java EE] [Glassfish] (Continued) Style that doubts the cache of Glassfish
How to fix the problem that Aptana Studio does not start
About the solution to the problem that the log of logback is not output when the web application is stopped
The problem that the contents of params are completely displayed in the [Rails] view
Solved the problem of freezing due to sudden "EXT4-fs error" on Ubuntu 20.04
I translated the grammar of R and Java [Updated from time to time]
Output of the book "Introduction to Java"
10 barrages of drawing with ● or ■ that are likely to appear in training (Java)
[Updated from time to time] Links that are indebted
The date time of java8 has been updated
[Java] What to do if the contents saved in the DB and the name of the enum are different in the enum that reflects the DB definition
[Java] How to get the authority of the folder
[Java] Try to solve the Fizz Buzz problem
Java Welcome to the Swamp of 2D Arrays
[Introduction to Java] List of things that got caught by the 14th day of programming
[Java] The problem that true was returned as a result of comparing Integer with ==
[Java] Dealing with the situation where the program that writes to the Output Stream of Process does not end even if waitFor
Features that are likely to enter Java 10 for now
Cases that are often forgotten due to integer overflow
How to write Scala from the perspective of Java
Initialization of for Try to make Java problem TypeScript 5-4
[Java] How to get the maximum value of HashMap
Change the storage quality of JPEG images in Java
A concise summary of Java 8 date / time APIs that are likely to be used frequently
Memo that transitions to the login screen if you are not logged in with devise
Use cryptographically secure pseudo-random numbers to verify that the solution to the Monty Hall problem is not 50%