spring boot 图片的上传与显示
2016-09-02 15:40
274 查看
首先描述一下问题,spring boot 使用的是内嵌的tomcat, 所以不清楚文件上传到哪里去了, 而且spring boot 把静态的文件全部在启动的时候都会加载到classpath的目录下的,所以上传的文件不知相对于应用目录在哪,也不知怎么写访问路径合适,对于新手的自己真的一头雾水。
后面想起了官方的例子,没想到一开始被自己找到的官方例子,后面太依赖百度谷歌了,结果发现只有官方的例子能帮上忙,而且帮上大忙,直接上密码的代码
看完上面的代码可以理解到spring boot 的存取文件思路了,存的时候的路径为
后面想起了官方的例子,没想到一开始被自己找到的官方例子,后面太依赖百度谷歌了,结果发现只有官方的例子能帮上忙,而且帮上大忙,直接上密码的代码
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(); } }
//上传的方法 @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:/"; } }
看完上面的代码可以理解到spring boot 的存取文件思路了,存的时候的路径为
Paths.get(ROOT, filename).toString()))
这个路径会在本地的工程根目录上创建,不应用部署里的目录,所以一般的访问http访问不可能 ,所以它提供了ResourceLoader,利于这个类可以加载非应用目录的里文件然后返回
所以就可以读取文件,所以就要写getFIle方法来显示图片
相关文章推荐
- spring boot 图片的上传与显示
- spring boot实现上传图片并在页面上显示及遇到的问题小结
- 上传图片以二进制插入到数据库,且读取显示
- 上传图片及显示图片
- 图片服务器上图片的管理网站上传与前台显示
- 图片文件上传入SQL库及显示代码 转
- 上传图片以二进制插入到数据库,且读取显示
- 上传图片并且显示所略图
- ASP.NET2.0中将文件上传到Access数据库的代码、从数据库显示图片的代码=
- 即时显示上传图片的做法
- 上传图片以二进制插入到数据库,且读取显示
- 怎样在CSDN的博客里面显示上传的图片,教你一招
- JSP中图片的上传与显示
- 使用asp.net将图片上传并存入SqlServer中,然后从SqlServer中读取并显示出来
- 将多个图片上传,并在另外的页面显示
- java+mysql中保存图片及显示上传的图片struts+hibernate
- 上传图片并显示缩略图的最简单方法(c#)
- 【File Field控件】上传图片的同时直接显示图片的属性设置方法
- [导入]图片服务器上图片的管理网站上传与前台显示
- 图片文件上传入SQL库及显示代码