激情久久久_欧美视频区_成人av免费_不卡视频一二三区_欧美精品在欧美一区二区少妇_欧美一区二区三区的

服務(wù)器之家:專注于服務(wù)器技術(shù)及軟件下載分享
分類導(dǎo)航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術(shù)|正則表達(dá)式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

香港云服务器
服務(wù)器之家 - 編程語言 - Java教程 - spring boot 圖片上傳與顯示功能實(shí)例詳解

spring boot 圖片上傳與顯示功能實(shí)例詳解

2020-09-10 13:53ohyeah-44 Java教程

這篇文章主要介紹了spring boot 圖片上傳與顯示功能實(shí)例詳解,需要的朋友可以參考下

首先描述一下問題,spring boot 使用的是內(nèi)嵌的tomcat, 所以不清楚文件上傳到哪里去了, 而且spring boot 把靜態(tài)的文件全部在啟動的時(shí)候都會加載到classpath的目錄下的,所以上傳的文件不知相對于應(yīng)用目錄在哪,也不知怎么寫訪問路徑合適,對于新手的自己真的一頭霧水。

后面想起了官方的例子,沒想到一開始被自己找到的官方例子,后面太依賴百度谷歌了,結(jié)果發(fā)現(xiàn)只有官方的例子能幫上忙,而且?guī)蜕洗竺?,直接上密碼的代碼

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
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";
 }
//顯示圖片的方法關(guān)鍵 匹配路徑像 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:/";
 }
}

看完上面的代碼可以理解到spring boot 的存取文件思路了,存的時(shí)候的路徑為

?
1
Paths.get(ROOT, filename).toString()))

這個(gè)路徑會在本地的工程根目錄上創(chuàng)建,不應(yīng)用部署里的目錄,所以一般的訪問http訪問不可能 ,所以它提供了ResourceLoader,利于這個(gè)類可以加載非應(yīng)用目錄的里文件然后返回 

所以就可以讀取文件,所以就要寫getFIle方法來顯示圖片 

spring boot 圖片上傳與顯示功能實(shí)例詳解

如果大家對spring boot不是很了解,大家可以參考下面兩篇文章。

Spring Boot 快速入門教程

Spring Boot 快速入門指南

以上所述是小編給大家介紹的spring boot 圖片上傳與顯示功能實(shí)例詳解,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時(shí)回復(fù)大家的。在此也非常感謝大家對服務(wù)器之家網(wǎng)站的支持!

原文鏈接:http://blog.csdn.net/a625013/article/details/52414470

 

延伸 · 閱讀

精彩推薦
400
Weibo Article 1 Weibo Article 2 Weibo Article 3 Weibo Article 4 Weibo Article 5 Weibo Article 6 Weibo Article 7 Weibo Article 8 Weibo Article 9 Weibo Article 10 Weibo Article 11 Weibo Article 12 Weibo Article 13 Weibo Article 14 Weibo Article 15 Weibo Article 16 Weibo Article 17 Weibo Article 18 Weibo Article 19 Weibo Article 20 Weibo Article 21 Weibo Article 22 Weibo Article 23 Weibo Article 24 Weibo Article 25
主站蜘蛛池模板: 韩国精品视频在线观看 | 欧美精品电影一区二区 | 日韩精品中文字幕一区 | 免费h片 | 91快色视频 | 国产毛片视频 | 亚洲午夜免费 | 欧美hdfree性xxxx | 羞羞的视频免费在线观看 | 亚州视频在线 | 在线成人免费网站 | 欧美大穴| 国产成人综合在线观看 | 一区二区三区在线观看免费视频 | 一级片在线免费 | 日本在线不卡一区二区三区 | 国产日韩久久久久69影院 | 羞羞视频免费观看网站 | 精品一区久久久 | 欧美成人免费一区二区三区 | 成人久久久精品乱码一区二区三区 | 一级成人黄色片 | 久久精品中文字幕一区二区 | 久久精品女人天堂av | 中文字幕偷拍 | 国内精品久久久久久久星辰影视 | 国产精品刺激对白麻豆99 | 日本a v免费观看 | 欧美一级美国一级 | 97人操| 国产噜噜噜 | 天天干天天碰 | 成人免费国产视频 | 成码无人av片在线观看网站 | 视频一区 中文字幕 | av大全在线免费观看 | 久久人人爽人人爽人人片av免费 | 精品一区二区三区免费视频 | 欧美成人理论片乱 | 好吊色欧美一区二区三区四区 | 黄色一级毛片免费看 |