我更新了头像并显示它,但头像在 Spring Boot 中没有改变,为什么? [英] I update avatar image and display it but the avatar does not change in Spring Boot , why?

查看:53
本文介绍了我更新了头像并显示它,但头像在 Spring Boot 中没有改变,为什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用Spring boot开发了一个网站,我开发了一个上传头像的功能,当我更新头像并显示时,头像没有变化,文件夹里的图片已经变了,为什么?

I use Spring boot to develop a website.I develop a function of uploading avatar image , when I update avatar image and display it but the avatar does not change and the image already has changed in the folder, why?

问题是http缓存?

我的简单项目:

我的项目的保管箱链接

(注意:需要在TestingController中更改本地路径)

(note: you need to change the local path in TestingController)

推荐答案

您无法立即看到上传的图片,因为您将图像保存在应用程序的静态文件中.您可以在文件夹中看到保存的图像,但如果您调用 url,则无法看到.如果您在上传文件(项目根目录并按 F5)后刷新正在运行的项目,您应该能够看到它.

You are not able to see the uploaded picture instantly because you save the image in the static files of your application. You are able to see the the saved image in your folder but not if you call your url. If you refresh your running project after uploading the file (project root and hit F5) you should be able to see it.

但更好的解决方案是你有一个 @RequestMapping 来加载图片并在浏览器中显示.

But the better solution would be that you have a @RequestMapping to load the picture and show it in browser.

以您的项目为基础,尝试像这样使用它:

Build on your project, try to use it like this:

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;
    }
}

在这段代码中,我没有实现异常和错误处理.

In this code I dont implement exception and error handling.

您可以像以前一样上传图片.然后你可以调用另一个 url 在浏览器中接收图片.

You can upload your picture like it was. And then you can call another url to receive the picture in your browser.

http://localhost:8080/files/testing.png

图片应该是回复.

请查看这两个来源以获得完整的解决方案.

Please have a look to those two sources for a complete solution.

Spring 文件上传 - 后端

Spring 文件上传 - 前端

这篇关于我更新了头像并显示它,但头像在 Spring Boot 中没有改变,为什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆