Spring Boot文件上传与下载
在实际的Web应用开发中,为了成功上传文件,必须将表单的method设置为post,并将enctype设置为multipart/form-data。只有这种设置,浏览器才能将所选文件的二进制数据发送给服务器。
从Servlet 3.0开始,就提供了处理文件上传的方法,但这种文件上传需要在Java Servlet中完成,而Spring MVC提供了更简单的封装。Spring MVC是通过Apache Commons FileUpload技术实现一个MultipartResolver的实现类CommonsMultipartResolver完成文件上传的。因此,Spring MVC的文件上传需要依赖Apache Commons FileUpload组件。
Spring MVC将上传文件自动绑定到MultipartFile对象中,MultipartFile提供了获取上传文件内容、文件名等方法,并通过transferTo方法将文件上传到服务器的磁盘中,MultipartFile的常用方法如下:
byte[] getBytes():获取文件数据。 String getContentType():获取文件MIME类型,如image/jpeg等。 InputStream getInputStream():获取文件流。 String getName():获取表单中文件组件的名字。 String getOriginalFilename():获取上传文件的原名。 long getSize():获取文件的字节大小,单位为byte。 boolean isEmpty():是否有(选择)上传文件。 void transferTo(File dest):将上传文件保存到一个目标文件中。Spring Boot的spring-boot-starter-web已经集成了Spring MVC,所以使用Spring Boot实现文件上传,更加便捷,只需要引入Apache Commons FileUpload组件依赖即可。
举例说明
下面通过一个实例讲解Spring Boot文件上传与下载的实现过程。
【例7】Spring Boot文件上传与下载。
具体实现步骤如下。
1.引入Apache Commons FileUpload组件依赖
在Web应用ch7_2的pom.xml文件中,添加Apache Commons FileUpload组件依赖,具体代码如下:
|
1 2 3 4 5 6 |
< dependency > < groupId >commons-fileupload</ groupId > < artifactId >commons-fileupload</ artifactId > <!-- 由于commons-fileupload组件不属于Spring Boot,所以需要加上版本 --> < version >1.4</ version > </ dependency > |
2.设置上传文件大小限制
在Web应用ch7_2的配置文件application.properties中,添加如下配置进行限制上传文件大小。
|
1 2 3 4 |
#上传文件时,默认单个上传文件大小是1MB,max-file-size设置单个上传文件大小 spring.servlet.multipart.max-file-size=50MB #默认总文件大小是10MB,max-request-size设置总上传文件大小 spring.servlet.multipart.max-request-size=500MB |
3.创建选择文件视图页面
在ch7_2应用的src/main/resources/templates目录下,创建选择文件视图页面uploadFile.html。该页面中有个enctype属性值为multipart/form-data的form表单,具体代码如下:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
<!DOCTYPE html> < html xmlns:th = "http://HdhCmsTestthymeleaf.org" > < head > < meta charset = "UTF-8" > < title >Insert title here</ title > < link rel = "stylesheet" th:href = "@{css/bootstrap.min.css}" /> <!-- 默认访问 src/main/resources/static下的css文件夹--> < link rel = "stylesheet" th:href = "@{css/bootstrap-theme.min.css}" /> </ head > < body > < div class = "panel panel-primary" > < div class = "panel-heading" > < h3 class = "panel-title" >文件上传示例</ h3 > </ div > </ div > < div class = "container" > < div class = "row" > < div class = "col-md-6 col-sm-6" > < form class = "form-horizontal" action = "upload" method = "post" enctype = "multipart/form-data" > < div class = "form-group" > < div class = "input-group col-md-6" > < span class = "input-group-addon" > < i class = "glyphicon glyphicon-pencil" ></ i > </ span > < input class = "form-control" type = "text" name = "description" th:placeholder = "文件描述" /> </ div > </ div > < div class = "form-group" > < div class = "input-group col-md-6" > < span class = "input-group-addon" > < i class = "glyphicon glyphicon-search" ></ i > </ span > < input class = "form-control" type = "file" name = "myfile" th:placeholder = "请选择文件" /> </ div > </ div > < div class = "form-group" > < div class = "col-md-6" > < div class = "btn-group btn-group-justified" > < div class = "btn-group" > < button type = "submit" class = "btn btn-success" > < span class = "glyphicon glyphicon-share" ></ span > 上传文件 </ button > </ div > </ div > </ div > </ div > </ form > </ div > </ div > </ div > </ body > </ html > |
4.创建控制器
在ch7_2应用的com.ch.ch7_2.controller包中,创建控制器类TestFileUpload。在该类中有4个处理方法,一个是界面导航方法uploadFile,一个是实现文件上传的upload方法,一个是显示将要被下载文件的showDownLoad方法,一个是实现下载功能的download方法。核心代码如下:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 |
@Controller public class TestFileUpload { @RequestMapping ( "/uploadFile" ) public String uploadFile() { return "uploadFile" ; } /** * 上传文件自动绑定到MultipartFile对象中, * 在这里使用处理方法的形参接收请求参数。 */ @RequestMapping ( "/upload" ) public String upload( HttpServletRequest request, @RequestParam ( "description" ) String description, @RequestParam ( "myfile" ) MultipartFile myfile) throws IllegalStateException, IOException { System.out.println( "文件描述:" + description); //如果选择了上传文件,将文件上传到指定的目录uploadFiles if (!myfile.isEmpty()) { //上传文件路径 String path = request.getServletContext().getRealPath( "/uploadFiles/" ); //获得上传文件原名 String fileName = myfile.getOriginalFilename(); File filePath = new File(path + File.separator + fileName); //如果文件目录不存在,创建目录 if (!filePath.getParentFile().exists()) { filePath.getParentFile().mkdirs(); } //将上传文件保存到一个目标文件中 myfile.transferTo(filePath); } //转发到一个请求处理方法,查询将要下载的文件 return "forward:/showDownLoad" ; } /** * 显示要下载的文件 */ @RequestMapping ( "/showDownLoad" ) public String showDownLoad(HttpServletRequest request, Model model) { String path = request.getServletContext().getRealPath( "/uploadFiles/" ); File fileDir = new File(path); //从指定目录获得文件列表 File filesList[] = fileDir.listFiles(); model.addAttribute( "filesList" , filesList); return "showFile" ; } /** * 实现下载功能 */ @RequestMapping ( "/download" ) public ResponseEntity< byte []> download( HttpServletRequest request, @RequestParam ( "filename" ) String filename, @RequestHeader ( "User-Agent" ) String userAgent) throws IOException { //下载文件路径 String path = request.getServletContext().getRealPath( "/uploadFiles/" ); //构建将要下载的文件对象 File downFile = new File(path + File.separator + filename); //ok表示HTTP中的状态是200 BodyBuilder builder = ResponseEntity.ok(); //内容长度 builder.contentLength(downFile.length()); //application/octet-stream:二进制流数据(最常见的文件下载) builder.contentType(MediaType.APPLICATION_OCTET_STREAM); //使用URLEncoder.encode对文件名进行编码 filename = URLEncoder.encode(filename, "UTF-8" ); /** * 设置实际的响应文件名,告诉浏览器文件要用于[下载]和[保存]。 * 不同的浏览器,处理方式不同,根据浏览器的实际情况区别对待。 */ if (userAgent.indexOf( "MSIE" ) > 0 ) { //IE浏览器,只需要用UTF-8字符集进行URL编码 builder.header( "Content-Disposition" , "attachment; filename=" + filename); } else { /**非IE浏览器,如FireFox、Chrome等浏览器,则需要说明编码的字符集 * filename后面有个*号,在UTF-8后面有两个单引号 */ builder.header( "Content-Disposition" , "attachment; filename*=UTF-8''" + filename); } return builder.body(FileUtils.readFileToByteArray(downFile)); } } |
5.创建文件下载视图页面
在ch7_2应用的src/main/resources/templates目录下,创建文件下载视图页面showFile.html。核心代码如下:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
< body > < div class = "panel panel-primary" > < div class = "panel-heading" > < h3 class = "panel-title" >文件下载示例</ h3 > </ div > </ div > < div class = "container" > < div class = "panel panel-primary" > < div class = "panel-heading" > < h3 class = "panel-title" >文件列表</ h3 > </ div > < div class = "panel-body" > < div class = "table table-responsive" > < table class = "table table-bordered table-hover" > < tbody class = "text-center" > < tr th:each = "file,fileStat:${filesList}" > < td > < span th:text = "${fileStat.count}" ></ span > </ td > < td > <!--file.name相当于调用getName()方法获得文件名称 --> < a th:href = "@{download(filename=${file.name})}" > < span th:text = "${file.name}" ></ span > </ a > </ td > </ tr > </ tbody > </ table > </ div > </ div > </ div > </ div > </ body > |
6.运行
首先,运行Ch72Application主类。然后,访问http://localhost:8080/ch7_2/uploadFile测试文件上传与下载。
以上就是SpringBoot实现文件上传与下载功能的示例代码的详细内容,更多关于SpringBoot文件上传 下载的资料请关注其它相关文章!
原文链接:https://blog.csdn.net/qq_41640218/article/details/125179653
查看更多关于SpringBoot实现文件上传与下载功能的示例代码的详细内容...