我使用Spring引導來開發一個網站。我開發了一個上傳頭像圖片的功能,當我更新頭像圖片並顯示它,但頭像沒有改變,圖片已經在文件夾中改變了,爲什麼?我更新頭像圖片並顯示它,但是在Spring Boot中頭像不會更改,爲什麼?
問題是http緩存?
我簡單的項目:
(注:您需要更改TestingController本地路徑)
我使用Spring引導來開發一個網站。我開發了一個上傳頭像圖片的功能,當我更新頭像圖片並顯示它,但頭像沒有改變,圖片已經在文件夾中改變了,爲什麼?我更新頭像圖片並顯示它,但是在Spring Boot中頭像不會更改,爲什麼?
問題是http緩存?
我簡單的項目:
(注:您需要更改TestingController本地路徑)
你是不是能夠看到上傳的圖片瞬間,因爲你保存圖像應用程序的靜態文件。您可以在您的文件夾中看到保存的圖像,但如果您打電話給您的網址則無法看到。如果您在上傳文件(項目根目錄並按F5)後刷新正在運行的項目,則應該可以看到它。
但更好的解決方案是,您有一個@RequestMapping
加載圖片並在瀏覽器中顯示它。你的項目
構建,嘗試使用這樣的:
import java.io.IOException;
import java.net.MalformedURLException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.stream.Stream;
import org.json.JSONObject;
import org.springframework.core.io.Resource;
import org.springframework.core.io.UrlResource;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
@Controller
public class TestingController {
private final Path p = Paths.get("/Users/devin/Documents/workspace-sts-3.8.1.RELEASE/testing/src/main/resources/static/uploads/images");
@RequestMapping(value={ "/", "/home"})
public String home(){
return "home";
}
@RequestMapping(value = "/post_upload_avatar_file", method = RequestMethod.POST)
@ResponseBody
public Object uploadAvatarFile(@RequestParam("uploadfile") MultipartFile uploadfile) {
JSONObject resJsonData=new JSONObject();
try {
if(uploadfile.isEmpty()){
System.out.println("Empty");
}
Files.copy(uploadfile.getInputStream(), p.resolve(uploadfile.getOriginalFilename()));
resJsonData.put("status", 200);
resJsonData.put("message", "Success!");
resJsonData.put("data", uploadfile.getOriginalFilename());
}catch (Exception e) {
System.out.println(e.getMessage());
resJsonData.put("status", 400);
resJsonData.put("message", "Upload Image Error!");
resJsonData.put("data", "");
}
return resJsonData.toString();
}
@GetMapping("files/{filename:.+}")
@ResponseBody
public ResponseEntity<Resource> serverFile(@PathVariable String filename){
Resource file = loadAsResource(filename);
return ResponseEntity
.ok()
.body(file);
}
public Resource loadAsResource(String filename) {
try {
Path file = p.resolve(filename);
Resource resource = new UrlResource(file.toUri());
if(resource.exists() || resource.isReadable()) {
return resource;
}
else {
System.out.println("no file");
}
} catch (MalformedURLException e) {
System.out.println(e);
}
return null;
}
public Stream<Path> loadAll() {
try {
return Files.walk(p, 1)
.filter(path -> !path.equals(p))
.map(path -> p.relativize(path));
} catch (IOException e) {
System.out.println(e);
}
return null;
}
}
在這段代碼我不落實的異常和錯誤處理。
你可以像上傳你的照片。然後,您可以調用另一個網址在瀏覽器中接收圖片。
而且圖片應該是響應。
請看看這兩個來源的完整解決方案。
這是我的工作,謝謝 –
> 「問題是HTTP緩存?」可能 - 當你重新加載網頁時會發生什麼(按「Ctrl + F5」或按「Ctrl + Shift + R」(Windows,Linux)) – Ralph
@ Ralph,我嘗試刷新頁面並打開圖像的URL,但圖像是舊圖像 –
在哪個文件夾中保存圖像?你的應用程序內部還是外部? – Patrick