栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

我更新并显示了头像图片,但头像在Spring Boot中没有更改,为什么?

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

我更新并显示了头像图片,但头像在Spring Boot中没有更改,为什么?

您无法立即看到上传的图片,因为您将图片保存在应用程序的静态文件中。您可以在文件夹中看到已保存的图像,但如果调用url则看不到。如果您在上传文件后刷新正在运行的项目(项目根目录并按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;@Controllerpublic 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;    }}

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

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

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

图片应该是回应。

请查看这两个资源以获取完整的解决方案。

Spring文件上传-后端

spring文件上传-前端



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/379298.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号