文件上傳SpringBoot后端MultipartFile參數(shù)報(bào)空問題的解決辦法
最近寫了一個(gè)文件上傳的小demo,就是簡單的前端html頁面,后端controller接收,但是后端一直報(bào)錯(cuò)文件為null,看了很多文章,有說spring-boot自帶的org.springframework.web.multipart.MultipartFile和Multipart沖突了,要在啟動(dòng)類中加入@EnableAutoConfiguration(exclue={MultipartAutoConfiguration.class}),有說要在MultipartFile參數(shù)前加上@RequestParam(“file”)以表明該參數(shù)類型是文件類型,還有說是前端表單沒有設(shè)置enctype=“multipart/form-data”,參照上面方法我還是沒有解決問題,后來更換了spring-boot版本就好了,代碼如下:
1、pom.xml(注意我這里用的是2.0.4版本)
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.4.RELEASE</version>
</parent>
<groupId>org.sang</groupId>
<artifactId>chapter01</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
2、upload.html頁面
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> <form action="/upload" method="post" enctype="multipart/form-data"> <input type="file" name="uploadFile" value="請選擇文件"> <input type="submit" value="上傳"> </form> </body> </html>
3、后端controller
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.UUID;
@RestController
public class FileUploadController {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd");
@PostMapping("upload")
public String upload(MultipartFile uploadFile, HttpServletRequest req){
String realPath = req.getSession().getServletContext().getRealPath("/uploadFile");
String format = sdf.format(new Date());
File folder = new File(realPath+format);
if(!folder.isDirectory()){
folder.mkdirs();
}
String oldName = uploadFile.getOriginalFilename();
String newName = UUID.randomUUID().toString()+oldName.substring(oldName.lastIndexOf("."),oldName.length());
try{
uploadFile.transferTo(new File(folder,newName));
String path = req.getScheme()+"://"+req.getServerName()+":"+req.getServerPort()+"/uploadFile/"+format+newName;
return path;
}catch (IOException e){
e.printStackTrace();
}
return "上傳失敗";
}
}
到此這篇關(guān)于文件上傳SpringBoot后端MultipartFile參數(shù)報(bào)空問題的解決辦法的文章就介紹到這了,更多相關(guān)SpringBoot MultipartFile參數(shù)報(bào)空 內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
MyBatis源碼解析——獲取SqlSessionFactory方式
這篇文章主要介紹了MyBatis源碼解析——獲取SqlSessionFactory方式,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-12-12
SpringBoot集成百度AI實(shí)現(xiàn)人臉識(shí)別的項(xiàng)目實(shí)踐
本文主要介紹了SpringBoot集成百度AI實(shí)現(xiàn)人臉識(shí)別的項(xiàng)目實(shí)踐,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-05-05
JAVA Swing實(shí)現(xiàn)窗口添加課程信息過程解析
這篇文章主要介紹了JAVA Swing實(shí)現(xiàn)窗口添加課程信息過程解析,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-10-10
SpringCloud使用Feign實(shí)現(xiàn)遠(yuǎn)程調(diào)用的使用示例
Feign是一個(gè)基于注解的HTTP客戶端庫,它允許您將HTTP請求轉(zhuǎn)換為聲明式的Java接口,本文主要介紹了SpringCloud使用Feign實(shí)現(xiàn)遠(yuǎn)程調(diào)用的使用示例,感興趣的可以了解一下2023-09-09
Mybatis-plus:${ew.sqlselect}用法說明
這篇文章主要介紹了Mybatis-plus:${ew.sqlselect}用法說明,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-06-06

