搭建 springboot
项目
Maven
依赖
< dependency>
< groupId> org.springframework.boot</ groupId>
< artifactId> spring-boot-starter-web</ artifactId>
</ dependency>
< dependency>
< groupId> org.springframework.boot</ groupId>
< artifactId> spring-boot-starter-thymeleaf</ artifactId>
</ dependency>
< dependency>
< groupId> com.fasterxml.jackson.core</ groupId>
< artifactId> jackson-databind</ artifactId>
</ dependency>
application.properties
spring. mvc . view . prefix = classpath: / templates/
spring. mvc . view . suffix = . html
spring. thymeleaf . cache = false
spring. thymeleaf . encoding = UTF- 8
spring. thymeleaf . emode = HTML
单文件上传
前端页面
<! DOCTYPE html >
< html xmlns = " http://www.w3.org/1999/xhtml" xmlns: th= " http://www.thymeleaf.org" >
< head>
< meta charset = " UTF-8" />
< title> Insert title here</ title>
</ head>
< body>
< h1 th: inlines= " text" > 文件上传</ h1>
< form th: action= " @{fileUpload}" method = " post" enctype = " multipart/form-data" >
< p> 选择文件: < input type = " file" name = " fileName" /> </ p>
< p> < input type = " submit" value = " 提交" /> </ p>
</ form>
</ body>
</ html>
后台 Controller
@Controller
public class FileUploadController {
@PostMapping ( "/fileUpload" )
@ResponseBody
public String fileUpload ( @RequestParam ( "fileName" ) MultipartFile file) {
if ( file. isEmpty ( ) ) {
return "false" ;
}
String fileName = file. getOriginalFilename ( ) ;
int size = ( int ) file. getSize ( ) ;
System . out. println ( fileName + "-->" + size) ;
String path = "E:/test" ;
File dest = new File ( path + "/" + fileName) ;
if ( ! dest. getParentFile ( ) . exists ( ) ) {
dest. getParentFile ( ) . mkdir ( ) ;
}
try {
file. transferTo ( dest) ;
return "true" ;
} catch ( IllegalStateException | IOException e) {
e. printStackTrace ( ) ;
return "false" ;
}
}
}
多文件上传
前端页面
<! DOCTYPE html >
< html xmlns = " http://www.w3.org/1999/xhtml" xmlns: th= " http://www.thymeleaf.org" >
< head>
< meta charset = " UTF-8" />
< title> Insert title here</ title>
</ head>
< body>
< h1 th: inlines= " text" > 文件上传</ h1>
< form th: action= " @{multifileUpload}" method = " post" enctype = " multipart/form-data" >
< p> 选择文件1: < input type = " file" name = " fileName" /> </ p>
< p> 选择文件2: < input type = " file" name = " fileName" /> </ p>
< p> 选择文件3: < input type = " file" name = " fileName" /> </ p>
< p> < input type = " submit" value = " 提交" /> </ p>
</ form>
</ body>
</ html>
后台 Controller
@Controller
public class FileUploadController {
@PostMapping ( value = "/multifileUpload" )
@ResponseBody
public String multifileUpload ( HttpServletRequest request) {
List < MultipartFile > files = ( ( MultipartHttpServletRequest ) request) . getFiles ( "fileName" ) ;
if ( files. isEmpty ( ) ) {
return "false" ;
}
String path = "E:/test" ;
for ( MultipartFile file : files) {
String fileName = file. getOriginalFilename ( ) ;
int size = ( int ) file. getSize ( ) ;
System . out. println ( fileName + "-->" + size) ;
if ( file. isEmpty ( ) ) {
return "false" ;
}
File dest = new File ( path + "/" + fileName) ;
if ( ! dest. getParentFile ( ) . exists ( ) ) {
dest. getParentFile ( ) . mkdir ( ) ;
}
try {
file. transferTo ( dest) ;
} catch ( Exception e) {
e. printStackTrace ( ) ;
return "false" ;
}
}
return "true" ;
}
}