使用SpringBoot进行文件上传的方法和SpringMVC差不多,本文单独新建一个最简单的springboot工程来说明一下。
主要步骤包括:
1、创建一个springboot项目工程,本例名称(springboot)。
2、配置 pom.xml 依赖。
3、创建和编写文件上传的 Controller(包含单文件上传)。
4、创建和编写文件上传的 HTML 测试页面。
5、文件上传相关限制的配置(可选)。
6、运行测试。
项目工程截图如下:
文件代码:
<dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web-services</artifactId> </dependency> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>1.3.2</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <scope>runtime</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies>
package com.example.springboot.controller; import com.example.springboot.util.FileUtil; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.ResponseBody; import org.springframework.web.multipart.MultipartFile; import org.springframework.web.multipart.MultipartHttpServletRequest; import javax.servlet.http.HttpServletRequest; import java.io.*; import java.util.List; import java.util.stream.Stream; /** *文件上传的Controller * @author lrd * @date 2018/10/22 * @param * @param null * @return null */ @Controller public class UploadFileController { @RequestMapping(value = "/upload",method = RequestMethod.GET) public String upload(){ return "/upload"; } /** *单文件上传 * @param file * @return String */ @RequestMapping(value = "/upload", method = RequestMethod.POST) @ResponseBody public String upload(@RequestParam("file")MultipartFile file,HttpServletRequest request){ String contentType = file.getContentType(); //图片文件类型 String fileName = FileUtil.getFileName(file.getOriginalFilename()); //图片名字 String filePath = "C:\\Users\\Administrator\\IdeaProjects\\springboot\\src\\main\\resources\\static\\upload\\"; try { //调用文件处理类FileUtil,处理文件,将文件写入指定位置 FileUtil.uploadFile(file.getBytes(),filePath,fileName); } catch (Exception e) { e.printStackTrace(); } return filePath; } }
package com.example.springboot.util; import java.io.File; import java.io.FileOutputStream; import java.util.UUID; /** *FileUtil工具类,实现uploadFile方法 * @author lrd * @date 2018/10/23 * @param * @param null */ public class FileUtil { /** *文件上传工具类服务方法 * @param * @param file * @param filePath * @param fileName * @return */ public static void uploadFile(byte[] file,String filePath,String fileName)throws Exception{ File targetFile = new File(filePath); if(!targetFile.exists()){ targetFile.mkdirs(); } FileOutputStream out = new FileOutputStream(filePath+fileName); out.write(file); out.flush(); out.close(); } /** *获取文件后缀名 * @param * @param fileName * @return String */ public static String getSuffix(String fileName){ return fileName.substring(fileName.lastIndexOf(".")); } /** *生成新的文件名 * @param * @param fileOriginName 源文件名 * @return */ public static String getFileName(String fileOriginName){ return getUUID() + getSuffix(fileOriginName); } /** *生成文件名 * @param * @param * @return */ public static String getUUID(){ return UUID.randomUUID().toString().replace("-",""); } }
package com.example.springboot.config; import org.springframework.boot.web.servlet.MultipartConfigFactory; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import javax.servlet.MultipartConfigElement; /** *文件上传配置 * @author lrd * @date 2018/10/22 * @param * @param null * @return null */ @Configuration public class UploadFileConfiguration { @Bean public MultipartConfigElement multipartConfigElement(){ MultipartConfigFactory factory = new MultipartConfigFactory(); // 设置文件大小限制 ,超出设置页面会抛出异常信息 factory.setMaxFileSize("256KB"); //设置总上传文件大小 factory.setMaxRequestSize("512KB"); return factory.createMultipartConfig(); } }
package com.example.springboot; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration; @SpringBootApplication(exclude = {DataSourceAutoConfiguration.class}) public class SpringbootApplication { public static void main(String[] args) { SpringApplication.run(SpringbootApplication.class, args); System.out.println("启动成功"); } } 注:因为我在pom.xml中添加了数据库组件,所以autoconfig会去读取数据源配置,而我新建的项目还没有配置数据源,所以会导致启动报错 Failed to auto-configure a DataSource,那么在@SpringBootApplication中添加exclude ={DataSourceAutoConfiguration.class},排除此类的autoconfig,这样就可以正常启动运行了。
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>文件上传示例</title> </head> <body> <h2>文件上传示例</h2> <hr/> <form method="POST" enctype="multipart/form-data" action="/upload"> <p> 文件:<input type="file" name="file" /> </p> <p> <input type="submit" value="上传" /> </p> </form> </body> </html>
最后启动Application服务,访问 http://localhost:8080/upload 测试文件上传。