SpringBoot如何上傳圖片
1.前端準(zhǔn)備
<%@ 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>實(shí)現(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.實(shí)現(xiàn)文件上傳的步驟說明
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)建多級(jí)目錄
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 上傳圖片的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
最全JVM調(diào)優(yōu)步驟和參數(shù)及配置
這篇文章主要給大家介紹了關(guān)于JVM調(diào)優(yōu)的相關(guān)資料,JVM調(diào)優(yōu)是指對(duì)Java虛擬機(jī)(JVM)進(jìn)行優(yōu)化,以提高Java程序的性能和運(yùn)行效率,文中介紹的非常詳細(xì),需要的朋友可以參考下2024-03-03java實(shí)現(xiàn)大文件分割與合并的實(shí)例代碼
java實(shí)現(xiàn)大文件分割與合并的實(shí)例代碼,需要的朋友可以參考一下2013-03-03詳解SpringCloud服務(wù)認(rèn)證(JWT)
本篇文章主要介紹了SpringCloud服務(wù)認(rèn)證(JWT),小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-01-01將Swagger2文檔導(dǎo)出為HTML或markdown等格式離線閱讀解析
這篇文章主要介紹了將Swagger2文檔導(dǎo)出為HTML或markdown等格式離線閱讀,本文給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-11-11java IO流將一個(gè)文件拆分為多個(gè)子文件代碼示例
這篇文章主要介紹了java IO流將一個(gè)文件拆分為多個(gè)子文件代碼示例,具有一定借鑒價(jià)值,需要的朋友可以參考下。2017-12-12IntelliJ IDEA Tomcat控制臺(tái)中文亂碼問題的四種解決方案
這篇文章主要給大家分享了4種方法完美解決IntelliJ IDEA Tomcat控制臺(tái)中文亂碼問題,文中有詳細(xì)的圖文介紹,對(duì)我們的學(xué)習(xí)或工作有一定的幫助,需要的朋友可以參考下2023-08-08springboot實(shí)現(xiàn)返回視圖而不是string的方法
這篇文章主要介紹了springboot實(shí)現(xiàn)返回視圖而不是string的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-01-01IDEA下使用Spring Boot熱加載的實(shí)現(xiàn)
本文主要介紹了IDEA下使用Spring Boot熱加載的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-06-06