iis服务器助手广告广告
返回顶部
首页 > 资讯 > 精选 >spring boot 图片上传与显示功能实例详解
  • 914
分享到

spring boot 图片上传与显示功能实例详解

springboot上传 2023-05-31 16:05:40 914人浏览 八月长安
摘要

首先描述一下问题,Spring Boot 使用的是内嵌的Tomcat, 所以不清楚文件上传到哪里去了, 而且spring boot 把静态的文件全部在启动的时候都会加载到classpath的目录下的,所以上传的文件不知相对于应用目录在哪,也

首先描述一下问题,Spring Boot 使用的是内嵌的Tomcat, 所以不清楚文件上传到哪里去了, 而且spring boot 把静态的文件全部在启动的时候都会加载到classpath的目录下的,所以上传的文件不知相对于应用目录在哪,也不知怎么写访问路径合适,对于新手的自己真的一头雾水。

后面想起了官方的例子,没想到一开始被自己找到的官方例子,后面太依赖百度谷歌了,结果发现只有官方的例子能帮上忙,而且帮上大忙,直接上密码的代码

package hello; import static org.springframework.hateoas.mvc.ControllerLinkBuilder.linkTo; import static org.springframework.hateoas.mvc.ControllerLinkBuilder.methodOn; import java.io.IOException; import java.NIO.file.Files; import java.nio.file.Paths; import java.util.stream.Collectors; import javax.servlet.Http.httpservletRequest; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.core.io.ResourceLoader; import org.springframework.http.ResponseEntity; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; 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; import org.springframework.web.servlet.mvc.support.RedirectAttributes; @Controller public class FileUploadController {  private static final Logger log = LoggerFactory.getLogger(FileUploadController.class);  public static final String ROOT = "upload-dir";  private final ResourceLoader resourceLoader;  @Autowired  public FileUploadController(ResourceLoader resourceLoader) {  this.resourceLoader = resourceLoader;  }  @RequestMapping(method = RequestMethod.GET, value = "/")  public String provideUploadInfo(Model model) throws IOException {  model.addAttribute("files", Files.walk(Paths.get(ROOT))   .filter(path -> !path.equals(Paths.get(ROOT)))   .map(path -> Paths.get(ROOT).relativize(path))   .map(path -> linkTo(methodOn(FileUploadController.class).getFile(path.toString())).withRel(path.toString()))   .collect(Collectors.toList()));  return "uploadFORM";  } //显示图片的方法关键 匹配路径像 localhost:8080/b7c76eb3-5a67-4d41-ae5c-1642af3f8746.png  @RequestMapping(method = RequestMethod.GET, value = "/{filename:.+}")  @ResponseBody  public ResponseEntity<?> getFile(@PathVariable String filename) {   try {   return ResponseEntity.ok(resourceLoader.getResource("file:" + Paths.get(ROOT, filename).toString()));  } catch (Exception e) {   return ResponseEntity.notFound().build();  }  } <strong>//上传的方法</strong>  @RequestMapping(method = RequestMethod.POST, value = "/")  public String handleFileUpload(@RequestParam("file") MultipartFile file, RedirectAttributes redirectAttributes, HttpServletRequest request) {  System.out.println(request.getParameter("member"));  if (!file.isEmpty()) {   try {   Files.copy(file.getInputStream(), Paths.get(ROOT, file.getOriginalFilename()));   redirectAttributes.addFlashAttribute("message",    "You successfully uploaded " + file.getOriginalFilename() + "!");   } catch (IOException|RuntimeException e) {   redirectAttributes.addFlashAttribute("message", "Failued to upload " + file.getOriginalFilename() + " => " + e.getMessage());   }  } else {   redirectAttributes.addFlashAttribute("message", "Failed to upload " + file.getOriginalFilename() + " because it was empty");  }  return "redirect:/";  } } 

--结束END--

本文标题: spring boot 图片上传与显示功能实例详解

本文链接: https://www.lsjlt.com/news/226986.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作