转自:
http://www.cnblogs.com/rollenholt/p/3693087.html
1. 创建一个Maven的web工程,然后配置pom.xml文件,增加依赖:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>1.0.2.RELEASE</version>
</dependency>
2. 在webapp目录下的index.jsp文件中输入一个表单:
<html>
<body>
<form method="POST" enctype="multipart/form-data"
action="/upload">
File to upload: <input type="file" name="file"><br /> Name: <input
type="text" name="name"><br /> <br /> <input type="submit"
value="Upload"> Press here to upload the file!
</form>
</body>
</html>
这个表单就是我们模拟的上传页面
3. 编写处理这个表单的Controller:
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
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;
@Controller
public class FileUploadController {
@RequestMapping(value="/upload", method=RequestMethod.GET)
public @ResponseBody String provideUploadInfo() {
return "You can upload a file by posting to this same URL.";
}
@RequestMapping(value="/upload", method=RequestMethod.POST)
public @ResponseBody String handleFileUpload(@RequestParam("name") String name,
@RequestParam("file") MultipartFile file){
if (!file.isEmpty()) {
try {
byte[] bytes = file.getBytes();
BufferedOutputStream stream =
new BufferedOutputStream(new FileOutputStream(new File(name + "-uploaded")));
stream.write(bytes);
stream.close();
return "You successfully uploaded " + name + " into " + name + "-uploaded !";
} catch (Exception e) {
return "You failed to upload " + name + " => " + e.getMessage();
}
} else {
return "You failed to upload " + name + " because the file was empty.";
}
}
}
4. 然后我们对上传的文件做一些限制,同时编写main方法来启动这个web :
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.context.embedded.MultiPartConfigFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import javax.servlet.MultipartConfigElement;
@Configuration
@ComponentScan
@EnableAutoConfiguration
public class Application {
@Bean
public MultipartConfigElement multipartConfigElement() {
MultiPartConfigFactory factory = new MultiPartConfigFactory();
factory.setMaxFileSize("128KB");
factory.setMaxRequestSize("128KB");
return factory.createMultipartConfig();
}
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
5. 然后访问http://localhost:8080/upload就可以看到页面了。
上面的例子是实现的是单个文件上传的功能,假定我们现在要实现文件批量上传的功能的话,我们只需要简单的修改一下上面的代码就行,考虑到篇幅的问题,下面只是贴出和上面不同的代码,没有贴出的说明和上面一样。:
1. 新增batchUpload.jsp文件
<html>
<body>
<form method="POST" enctype="multipart/form-data"
action="/batch/upload">
File to upload: <input type="file" name="file"><br />
File to upload: <input type="file" name="file"><br />
<input type="submit" value="Upload"> Press here to upload the file!
</form>
</body>
</html>
2. 新增BatchFileUploadController.java文件:
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.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import javax.servlet.http.HttpServletRequest;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.util.List;
/**
* Created by wenchao.ren on 2014/4/26.
*/
@Controller
public class BatchFileUploadController {
@RequestMapping(value="/batch/upload", method= RequestMethod.POST)
public @ResponseBody
String handleFileUpload(HttpServletRequest request){
List<MultipartFile> files = ((MultipartHttpServletRequest)request).getFiles("file");
for (int i =0; i< files.size(); ++i) {
MultipartFile file = files.get(i);
String name = file.getName();
if (!file.isEmpty()) {
try {
byte[] bytes = file.getBytes();
BufferedOutputStream stream =
new BufferedOutputStream(new FileOutputStream(new File(name + i)));
stream.write(bytes);
stream.close();
} catch (Exception e) {
return "You failed to upload " + name + " => " + e.getMessage();
}
} else {
return "You failed to upload " + name + " because the file was empty.";
}
}
return "upload successful";
}
}
这样一个简单的批量上传文件的功能就ok了,是不是很简单啊。
注意:上面的代码只是为了演示而已,所以编码风格上采取了随性的方式,不建议大家模仿。
参考资料:
1. MultipartResolver也可以实现文件上传功能。参考文章:
http://mylfd.iteye.com/blog/1893648
分享到:
相关推荐
spring-boot-file-upload:使用Spring Boot 上传文件示例 spring-boot-fastDFS:Spring Boot 整合FastDFS示例 spring-boot-actuator:Spring Boot Actuator 使用示例 spring-boot-admin-simple:Spring Boot Admin ...
第 2-7 课:使用 Spring Boot 上传文件到 FastDFS/spring-boot-fastDFS 第 2-8 课: Spring Boot 构建一个 RESTful Web 服务/spring-boot-web-restful 第 2-9 课:Spring Boot 中使用 Swagger2 构建 RESTful APIs/...
- [spring-boot-file-upload](https://github.com/ityouknow/spring-boot-examples/tree/master/spring-boot-file-upload):使用 Spring Boot 上传文件示例 - [spring-boot-fastDFS]...
Spring Boot(十一)—文件上传和下载 文件上传和下载是Web应用程序比较常用的功能之一,在本章节中,我将以一个简单的案例案例解压缩在Spring Boot中如何进行文件的上传与下载。思维导图来了解一下文件上传与下载的...
MinIO 是一个基于Apache License v2.0开源协议的对象存储服务。它兼容亚马逊S3云存储服务接口,非常适合于存储大容量非结构化的数据,例如图片、视频、日志文件、备份数据和...Spring boot整合MinIO客户端实现文件管理
Spring Boot文件上传/下载Rest API示例 教程: 设定步骤 1.克隆存储库 git clone https://github.com/callicoder/spring-boot-file-upload-download-rest-api-example.git 2.指定文件上传目录 打开src/main/...
上传文件是互联网中常常应用的场景之一,最典型的情况就是上传头像等,今天就带着带着大家做一个Spring Boot上传文件的小案例。
spring-boot-upload-file-lead-to-rce-tricks一. docker 漏洞环境搭建docker pull landgrey/spring-boot-fat-jar-write-file-rce:1.2docker run -d -p ... docker 漏洞环境的功能文件上传功能 (默认上传到 /tmp/ 目录,
基于SpringBoot的文件上传系统,前后端分离,单文件上传,多文件上传,大文件上传,断点续传,文件秒传,图片上传 项目经过严格测试,确保可以运行! 采用前后端分离的方式进行开发,实现了几种常用的文件上传功能...
在Spring Boot中实现文件上传和下载功能可以通过以下步骤进行操作: 文件上传: 1.配置文件上传相关的依赖: 在项目的 pom.xml 文件中添加依赖 2.配置文件上传的控制器(Controller): 创建一个控制器来处理文件...
Spring Boot 文件上传原理其实就是Spring MVC,因为这部分工作是Spring MVC做的而不是Spring Boot,那么,SpringMVC又是怎么处理文件上传这个过程的呢?下面通过本文给大家详细介绍下,一起看看吧
基于Spring Boot的文件管理系统,支持文件上传,下载,删除等操作,在线浏览文件列表及基本信息等操作。部署简单,Java课程设计必备。 依赖软件: 1、jdk1.8+ 2、maven 开发调试方法: 解压压缩包,导入IDE,...
1.32 Spring boot 文件上传(多文件上传) 1.33 导入时如何定制spring-boot依赖项的版本 1.34 Spring Boot导入XML配置 1.35 Spring Boot使用@SpringBootApplication注解 1.36 Spring Boot 监控和管理生产环境 1.37 ...
详细的spring boot的文件上传代码,支持多文件上传,带有注释!!!
Spring Boot基础教程和示例代码 ...该教程的内容将记录我在学习和开发Spring Boot项目时遇到的技术和坑。欢迎单击项目右上方的Star按钮,给予支持。...Spring Boot系列之文件上传与下载 教程地址: : 示例代码地址: :
s3-file-uploader:Spring Boot应用程序用于S3文件上传
教程合集 ...Spring Boot文件上传 Spring Boot使用XML配置文件 Spring Boot配置拦截器拦截器 Spring Boot配置Servlet / Filter / Listener Spring Boot配置ViewController Spring Boot配置转换器
4.5.1 文件上传配置 98 4.5.2 自定义HttpMessageConverter 101 4.5.3 服务器端推送技术 106 4.6 Spring MVC 的测试 113 4.6.1 点睛 113 4.6.2 示例 114 第三部分 实战Spring Boot 第5 章 Spring Boot 基础 122 5.1 ...
spring boot内置jetty开发,附带一些基础小功能,比如上传下载文件,走马灯,图片预览等等。
多文件上传 支持文件搜索 复制、移动、重命名 删除、修改、预览、下载 直接压缩、解压缩zip文件(支持解压zip、tar.gz、rar) 支持设置文件权限(UNIX chmod格式) 移动端支持 支持 OFFICE 等文档在线预览 (原生 HTML,...