Spring Boot : 文件上传(八)
2017-08-25 11:47
381 查看
目录
目录文件上传
文件大小设置
文件上传
添加pom.xml包支持<!-- thymeleaf支持 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency>
页面:
fileupload.html
<!DOCTYPE html> <html> <head> <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>
mutifileupload.html
<!DOCTYPE html> <html> <head> <title>批量文件上传示例</title> </head> <body> <h2>批量文件上传示例</h2> <hr/> <form method="POST" enctype="multipart/form-data" action="/upload/batch"> <p> 文件1:<input type="file" name="file" /> </p> <p> 文件2:<input type="file" name="file" /> </p> <p> 文件3:<input type="file" name="file" /> </p> <p> <input type="submit" value="上传" /> </p> </form> </body> </html>
FileUploadController.java
package cn.milo.controllor; import java.io.BufferedOutputStream; import java.io.File; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.util.List; import javax.servlet.http.HttpServletRequest; 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; /** * Created by admin on 2017/8/25. * 代码摘自 :http://blog.csdn.net/catoop/article/details/61415169 */ @Controller public class FileUploadController { // 访问路径为:http://ip:port/upload @RequestMapping(value = "/upload", method = RequestMethod.GET) public String upload() { return "/fileupload"; } // 访问路径为:http://ip:port/upload/batch @RequestMapping(value = "/upload/batch", method = RequestMethod.GET) public String batchUpload() { return "/mutifileupload"; } /** * 单文件 */ @RequestMapping(value = "/upload", method = RequestMethod.POST) @ResponseBody public String upload(@RequestParam("file") MultipartFile file) { if (!file.isEmpty()) { try { // 这里只是简单例子,文件直接输出到项目路径下。 // 实际项目中,文件需要输出到指定位置,需要在增加代码处理。 // 还有关于文件格式限制、文件大小限制,详见:中配置。 BufferedOutputStream out = new BufferedOutputStream( new FileOutputStream(new File(file.getOriginalFilename()))); out.write(file.getBytes()); out.flush(); out.close(); } catch (FileNotFoundException e) { e.printStackTrace(); return "上传失败," + e.getMessage(); } catch (IOException e) { e.printStackTrace(); return "上传失败," + e.getMessage(); } return "上传成功"; } else { return "上传失败,因为文件是空的."; } } /** * 多文件 */ @RequestMapping(value = "/upload/batch", method = RequestMethod.POST) public @ResponseBody String batchUpload(HttpServletRequest request) { List<MultipartFile> files = ((MultipartHttpServletRequest) request).getFiles("file"); MultipartFile file = null; BufferedOutputStream stream = null; for (int i = 0; i < files.size(); ++i) { file = files.get(i); if (!file.isEmpty()) { try { byte[] bytes = file.getBytes(); stream = new BufferedOutputStream(new FileOutputStream(new File(file.getOriginalFilename()))); stream.write(bytes); stream.close(); } catch (Exception e) { stream = null; return "You failed to upload " + i + " => " + e.getMessage(); } } else { return "You failed to upload " + i + " because the file was empty."; } } return "upload successful"; } }
文件大小设置
方式一:FileUploadConfiguration.java
package cn.milo.controllor; import javax.servlet.MultipartConfigElement; import org.springframework.boot.web.servlet.MultipartConfigFactory; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @Configuration public class FileUploadConfiguration { @Bean public MultipartConfigElement multipartConfigElement() { MultipartConfigFactory factory = new MultipartConfigFactory(); // 设置文件大小限制 ,超出设置页面会抛出异常信息, // 这样在文件上传的地方就需要进行异常信息的处理了; factory.setMaxFileSize("1000KB"); // KB,MB /// 设置总上传数据总大小 factory.setMaxRequestSize("2000KB"); // Sets the directory location where files will be stored. // factory.setLocation("路径地址"); return factory.createMultipartConfig(); } }
一般放在启动类:
@Configuration @SpringBootApplication public class Application { public static void main(String[] args) throws Exception { SpringApplication.run(Application.class, args); } /** * 文件上传配置 * @return */ @Bean public MultipartConfigElement multipartConfigElement() { MultipartConfigFactory factory = new MultipartConfigFactory(); //文件最大 factory.setMaxFileSize("1000KB"); //KB,MB /// 设置总上传数据总大小 factory.setMaxRequestSize("2000KB"); return factory.createMultipartConfig(); } }
方式二:在application.yml中添加配置
spring: http: multipart: maxFileSize: 1000KB maxRequestSize : 2000KB
相关文章推荐
- springboot+jquery实现文件异步上传——浅谈SOA
- Spring boot实现文件上传下载
- Spring Boot 文件上传实例
- Spring Boot搭建文件上传服务的方法
- SpringBoot文件上传下载和多文件上传(图文详解)
- SpringBoot非官方教程 | 第十七篇:上传文件
- Spring Boot使用ServletFileUpload上传文件失败
- 使用Spring boot + jQuery上传文件(kotlin)功能实例详解
- spring boot文件上传、下载
- Spring Boot基础教程12-web应用开发-文件上传
- SpringBoot下文件上传与下载的实现
- SpringBoot上传文件到本服务器 目录与jar包同级问题
- Spring Boot 上传文件(spring boot upload file)
- springboot(十七):使用Spring Boot上传文件
- MongodbGFS结合SpringBoot 实现大文件的简单上传与下载
- SpringBoot+fileUpload获取文件上传进度
- spring-boot上传文件MultiPartFile获取不到文件问题解决
- spring-boot 实现文件上传下载
- 解决使用Spring Boot、Multipartfile上传文件路径错误问题
- Spring Boot 文件上传原理解析