SpringBoot实现文件上传功能
新建maven项目,pom文件:
1 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 2 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> 3 <modelVersion>4.0.0</modelVersion> 4 <groupId>com.niiam</groupId> 5 <artifactId>SBFileUpload</artifactId> 6 <packaging>war</packaging> 7 <version>0.0.1-SNAPSHOT</version> 8 <name>SBFileUpload Maven Webapp</name> 9 <url>http://maven.apache.org</url> 10 11 <!-- 引入springboot组件 --> 12 <parent> 13 <groupId>org.springframework.boot</groupId> 14 <artifactId>spring-boot-starter-parent</artifactId> 15 <version>1.5.9.RELEASE</version> 16 </parent> 17 18 <dependencies> 19 <dependency> 20 <groupId>org.springframework.boot</groupId> 21 <artifactId>spring-boot-starter-web</artifactId> 22 </dependency> 23 <dependency> 24 <groupId>org.springframework.boot</groupId> 25 <artifactId>spring-boot-starter-test</artifactId> 26 <scope>test</scope> 27 </dependency> 28 <!-- 引入json组件 --> 29 <dependency> 30 <groupId>com.jayway.jsonpath</groupId> 31 <artifactId>json-path</artifactId> 32 <scope>test</scope> 33 </dependency> 34 35 <!-- 使得内嵌的Tomcat不可见,用于导出war包 --> 36 <dependency> 37 <groupId>org.springframework.boot</groupId> 38 <artifactId>spring-boot-starter-tomcat</artifactId> 39 <scope>provided</scope> 40 </dependency> 41 42 </dependencies> 43 44 <!-- 制定Java编译版本,用于消除IDE关于JRE版本的warning --> 45 <properties> 46 <java.version>9</java.version> 47 </properties> 48 49 <build> 50 <finalName>SBFileUpload</finalName> 51 <plugins> 52 <plugin> 53 <groupId>org.springframework.boot</groupId> 54 <artifactId>spring-boot-maven-plugin</artifactId> 55 </plugin> 56 </plugins> 57 </build> 58 59 <repositories> 60 <repository> 61 <id>spring-releases</id> 62 <url>https://repo.spring.io/libs-release</url> 63 </repository> 64 </repositories> 65 <pluginRepositories> 66 <pluginRepository> 67 <id>spring-releases</id> 68 <url>https://repo.spring.io/libs-release</url> 69 </pluginRepository> 70 </pluginRepositories> 71 72 </project>
Test.html文件:
1 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 2 <html> 3 <head> 4 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 5 <title>Insert title here</title> 6 <script type="text/javascript" src="http://apps.bdimg.com/libs/jquery/2.1.4/jquery.min.js"></script> 7 8 </head> 9 <body> 10 <form id="formId" action="/SBFileUpload/testUpload" target="frame1" method="POST" enctype="multipart/form-data"> 11 <input type="file" name="file"/> 12 <input type="button" value="提交" onclick="upload()"> 13 </form> 14 15 <iframe name="frame1" frameborder="0" height="40"></iframe> 16 17 <script type="text/javascript"> 18 function upload() { 19 $("#formId").submit(); 20 } 21 </script> 22 23 </body> 24 </html>
注意:
1、第10行,action里要指明项目名,这样在跳转时才能跳转到该项目的链接中
2、为了防止点击提交按键后页面跳转,此处设置了iframe标签,用于点击按键后发送ajax指令。
后台Java代码:
Application.java
1 package com.niiam; 2 3 import org.springframework.boot.SpringApplication; 4 import org.springframework.boot.autoconfigure.SpringBootApplication; 5 6 import org.springframework.boot.builder.SpringApplicationBuilder; 7 import org.springframework.boot.web.support.SpringBootServletInitializer; 8 9 @SpringBootApplication 10 public class Application extends SpringBootServletInitializer{ 11 12 @Override 13 protected SpringApplicationBuilder configure(SpringApplicationBuilder application) { 14 return application.sources(Application.class); 15 } 16 17 public static void main(String[] args) { 18 SpringApplication.run(Application.class, args); 19 } 20 }
Controller.java
1 package com.niiam; 2 3 import org.springframework.web.bind.annotation.RequestMapping; 4 import org.springframework.web.bind.annotation.RestController; 5 import org.springframework.web.multipart.MultipartHttpServletRequest; 6 import org.springframework.web.bind.annotation.RequestMethod; 7 8 import java.io.*; 9 10 import javax.servlet.http.HttpServletRequest; 11 12 @RestController 13 public class Controller { 14 @RequestMapping(value="/testUpload",method=RequestMethod.POST) 15 public void testUploadFile(HttpServletRequest req,MultipartHttpServletRequest multiReq) throws IOException{ 16 FileOutputStream fos=new FileOutputStream(new File("E://fileuploadtest//src//file//upload.jpg")); 17 FileInputStream fs=(FileInputStream) multiReq.getFile("file").getInputStream(); 18 byte[] buffer=new byte[1024]; 19 int len=0; 20 while((len=fs.read(buffer))!=-1){ 21 fos.write(buffer, 0, len); 22 } 23 fos.close(); 24 fs.close(); 25 } 26 }
如果上传的文件大于 1M 时,上传会报错文件太大的错误,在 application.properties 中设置上传文件的参数即可
spring.http.multipart.maxFileSize=100Mb
spring.http.multipart.maxRequestSize=100Mb
application.properties可以自己新建,放在这里:(该文件可以放在4个地方,详情百度)
三种方法测试:
1、右键点击Test.html,选择run on server
2、项目Export为war包,放在Tomcat上运行
结果展示: