亚洲精品久久久中文字幕-亚洲精品久久片久久-亚洲精品久久青草-亚洲精品久久婷婷爱久久婷婷-亚洲精品久久午夜香蕉

您的位置:首頁技術(shù)文章
文章詳情頁

SpringBoot如何上傳圖片

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

1.前端準(zhǔn)備

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>實現(xiàn)文件長傳</h1> <!--enctype='開啟多媒體標(biāo)簽' --> <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.實現(xiàn)文件上傳的步驟說明

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方式上傳的文件,包含二進(jìn)制數(shù)據(jù)+文件名稱。

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() 方法創(chuàng)建多級目錄

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. 返回客戶端文件系統(tǒng)中的原始文件名。

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

3.4 文件的上傳

fileImage.getOriginalFilename()//Transfer the received file to the given destination file. 將接收到的文件傳輸?shù)浇o定的目標(biāo)文件。

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

以上就是SpringBoot如何上傳圖片的詳細(xì)內(nèi)容,更多關(guān)于SpringBoot 上傳圖片的資料請關(guān)注好吧啦網(wǎng)其它相關(guān)文章!

標(biāo)簽: Spring
相關(guān)文章:
主站蜘蛛池模板: 91精品国产91久久久久久最新 | 国产福利一区二区三区在线观看 | 青草在线观看 | 欧美精品免费一区欧美久久优播 | 国内国内在线精品视频 | 高清成年美女xx免费网站黄 | 精品免费在线观看 | 国产吧在线 | 中文字幕一区视频一线 | 久草视频网址 | 亚洲综合激情另类图片专区 | 日韩在线操| 欧美精品在欧美一区二区 | 黄色毛片网 | 国产精品亚洲一区二区在线观看 | 国产在线视频自拍 | 高清欧美在线三级视频 | 正在播放国产大学生情侣 | 久久久精品免费热线观看 | 高颜值露脸极品在线播放 | 在线国产一区二区 | 亚洲国产m3u8在线观看 | 女人18毛片久久鬼色 | 中文字幕免费在线观看动作大片 | 91精东| 三级久久 | 久久国产精品久久久久久 | 韩国福利影视一区二区三区 | 美国三级网 | 亚洲国产精久久久久久久春色 | 国产成人精品免费影视大全 | 国产午夜精品不卡视频 | 黄色影片在线免费观看 | 在线免费观看a级片 | 青草悠悠视频在线观看 | 欧美三级在线观看不卡视频 | 国产福利一区二区在线观看 | 欧美性色黄大片www 欧美性色黄大片一级毛片视频 | 国产午夜影院 | 亚洲一区二区三区网站 | 午夜亚洲视频 |