av一区二区在线观看_亚洲男人的天堂网站_日韩亚洲视频_在线成人免费_欧美日韩精品免费观看视频_久草视

您的位置:首頁技術文章
文章詳情頁

SpringBoot如何上傳圖片

瀏覽:3日期:2023-04-26 08:30:36

1.前端準備

SpringBoot如何上傳圖片

<%@ page language='java' contentType='text/html; charset=UTF-8' pageEncoding='UTF-8'%> <!DOCTYPE html PUBLIC '-//W3C//DTD HTML 4.01 Transitional//EN' 'http://www.w3.org/TR/html4/loose.dtd'> <html> <head> <meta http-equiv='Content-Type' content='text/html; charset=UTF-8'> <title>Insert title here</title> </head> <body> <h1>實現文件長傳</h1> <!--enctype='開啟多媒體標簽' --> <form action='http://localhost:8091/filetest' method='post' enctype='multipart/form-data'> <input name='fileImage' type='file' /> <input type='submit' value='提交'/> </form> </body> </html>

2.實現文件上傳的步驟說明

SpringBoot如何上傳圖片

package com.jt.controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.multipart.MultipartFile; import java.io.File; import java.io.IOException; @RestController public class FileTestController { @RequestMapping('/filetest') public String file(MultipartFile fileImage){ String fileDir = 'F:/CloudMusic/images'; File file = new File(fileDir); if(!file.exists()){ file.mkdirs(); } String fileName = fileImage.getOriginalFilename(); File imageFile = new File(fileDir+'/'+fileName); try { fileImage.transferTo(imageFile);//Transfer the received file to the given destination file. }catch(IOException e){ e.printStackTrace(); } return 'ok'; } }

3.代碼解釋

3.1 前提

MultipartFile是spring類型,代表HTML中form data方式上傳的文件,包含二進制數據+文件名稱。

public String file(MultipartFile fileImage){}<form action='http://localhost:8091/filetest' method='post' enctype='multipart/form-data'> <input name='fileImage' type='file' /> <input type='submit' value='提交'/> </form>

3.2 封裝文件的上傳路徑

封裝文件上傳的路徑,如果文件存在直接封裝,如果文件不存在使用 file.mkdirs() 方法創建多級目錄

String fileDir = 'F:/CloudMusic/images'; File file = new File(fileDir); if(!file.exists()){ file.mkdirs(); }

3.3 封裝文件的名稱

fileImage.getOriginalFilename()//Return the original filename in the client’s filesystem. 返回客戶端文件系統中的原始文件名。

String fileName = fileImage.getOriginalFilename(); File imageFile = new File(fileDir+'/'+fileName);

3.4 文件的上傳

fileImage.getOriginalFilename()//Transfer the received file to the given destination file. 將接收到的文件傳輸到給定的目標文件。

try { fileImage.transferTo(imageFile);//Transfer the received file to the given destination file. }catch(IOException e){ e.printStackTrace(); }

以上就是SpringBoot如何上傳圖片的詳細內容,更多關于SpringBoot 上傳圖片的資料請關注好吧啦網其它相關文章!

標簽: Spring
相關文章:
主站蜘蛛池模板: 国产一级黄色录像 | 91片黄在线观看 | 午夜影院在线 | 亚洲精品伦理 | 成人精品免费 | 国产又粗又猛又黄又爽无遮挡 | 青青草免费在线观看视频 | 色婷婷香蕉在线一区二区 | 午夜视频在线看 | 欧美精品三区 | 久久视频免费看 | 成人三级在线观看 | 欧美一级二级三级 | 久久久精品国产sm调教网站 | 免费网站观看www在线观看 | 福利小视频 | 中文字幕伊人 | 黄色在线免费网站 | www精品| 久久毛片视频 | 亚洲日本视频 | 欧美专区第一页 | 黄视频在线播放 | 日韩在线看片 | 午夜无遮挡 | 久久久噜噜噜 | 日韩精品一区在线 | 久久久网 | 福利视频网站 | 免费成人深夜夜国外 | 日韩综合一区 | 欧美日韩性 | 久久狠狠干 | 亚洲一区二区中文字幕 | 精品 | 国产精品福利在线观看 | 狠狠操天天操 | 久久五月婷 | 中文字幕1区 | 久久99精品久久久久久 | 日韩在线免费 |