SpringBoot之文件上传
来源:互联网 发布:手机做菜单软件 编辑:程序博客网 时间:2024/05/01 13:47
新建一个SpringBoot项目:SpringBoot–uploadfile
工程目录:
pom.xml依赖:
<dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!-- thmleaf模板依赖. --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies>
FileUploadConfiguration文件信息配置:
public class FileUploadConfiguration { @Bean public MultipartConfigElement multipartConfigElement() { MultipartConfigFactory factory = new MultipartConfigFactory(); // 设置文件大小限制 ,超出设置页面会抛出异常信息, // 这样在文件上传的地方就需要进行异常信息的处理了; factory.setMaxFileSize("256KB"); // KB,MB /// 设置总上传数据总大小 factory.setMaxRequestSize("512KB"); // Sets the directory location where files will be stored. // factory.setLocation("路径地址"); return factory.createMultipartConfig(); }}
Controller类:
@Controllerpublic class FileUploadController { @RequestMapping(value = "/upload", method = RequestMethod.GET) public String upload() { return "/fileupload"; } @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 "上传失败,因为文件是空的."; } } /** * 多文件上传 主要是使用了MultipartHttpServletRequest和MultipartFile * */ @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"; }}
HTML文件配置:
单文件:
<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>
批量文件:
<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>
阅读全文
0 0
- SpringBoot之文件上传
- Springboot 之 多文件上传-知识林
- SPRINGBOOT之文件上传与下载
- SpringBoot文件上传
- springboot文件上传
- SpringBoot上传文件
- springboot 文件上传
- springboot 文件上传
- SpringBoot实现文件上传
- springboot 上传文件
- Springboot 文件上传
- SpringBoot上传文件
- SpringBoot文件上传控制
- springboot上传文件
- SpringBoot进阶之文件上传(单文件上传/多文件上传)
- springboot+文件上传和下载
- springboot实现多文件上传
- springboot 文件上传大小配置
- hdu4310 Hero
- datacleaner第八部分
- Error configuring application listener of class XXX ClassNotFoundException
- 终端输出打印日志
- JS数组方法汇总 array数组元素的添加和删除
- SpringBoot之文件上传
- BZOJ 3670: [Noi2014]动物园&&51nod 1277 字符串中的最大值
- Oracle12C 的安装图文教程
- 深入理解 CSS3 弹性盒布局模型(flex)----以及实战应用
- Struts2:前后台的数据交互
- 数码管静态显示
- Xamarin.Android开发入门——Hello,Android Multiscreen快速上手
- JavaScript保留关键字
- PyCharm杂记(mac版)