精品专区-精品自拍9-精品自拍三级乱伦-精品自拍视频-精品自拍视频曝光-精品自拍小视频

網(wǎng)站建設(shè)資訊

NEWS

網(wǎng)站建設(shè)資訊

springboot圖片上傳與顯示功能實例詳解-創(chuàng)新互聯(lián)

創(chuàng)新互聯(lián)www.cdcxhl.cn八線動態(tài)BGP香港云服務(wù)器提供商,新人活動買多久送多久,劃算不套路!

西藏ssl適用于網(wǎng)站、小程序/APP、API接口等需要進行數(shù)據(jù)傳輸應(yīng)用場景,ssl證書未來市場廣闊!成為創(chuàng)新互聯(lián)公司的ssl證書銷售渠道,可以享受市場價格4-6折優(yōu)惠!如果有意向歡迎電話聯(lián)系或者加微信:18980820575(備注:SSL證書合作)期待與您的合作!

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

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

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(); 
 } 
 } 
//上傳的方法 
 @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:/"; 
 } 
} 

網(wǎng)頁標題:springboot圖片上傳與顯示功能實例詳解-創(chuàng)新互聯(lián)
轉(zhuǎn)載來于:http://m.jcarcd.cn/article/csoegs.html
主站蜘蛛池模板: 午夜免费日韩小电影 | 日本片成人在线 | 日本在线干| 精品人人槡 | 国产精品亚洲欧 | 日韩中文字幕无 | 欧美日韩国产三区 | 国产精品专区二 | 麻花传媒| 国产精品第32页 | 91九色老熟女免费 | 国产精品成人免费 | 国产又滑又嫩又白 | 日本高清天码一区 | 成人极品 | 国产91网| 中文字幕日韩欧美 | 国产日韩一区欧美 | 精品熟女中文字幕 | 韩国日本香港三级 | 成人午夜福利群爱片 | 国产另类亚洲日韩 | 午夜免费福利影院 | 欧洲在线 | 国自产拍视频 | 日韩一区二区三区波 | 欧美一区在线日韩 | 精品在线97 | 日本一区二区在线看 | 日韩国产在线观看 | 井川里予打扑克 | 日韩福利在线观看 | 日韩伦理在线 | 国产高清自拍一区 | 精品免费人成视 | 老司机午夜精 | 欧美一区二区 | 午夜日韩绝美 | 国产精品久 | 国产高清在线免 | 成人国产精品视频 |