Spring 文件上传功能

简介:

本篇文章,我们要来做一个Spring的文件上传功能:

1. 创建一个Maven的web工程,然后配置pom.xml文件,增加依赖:

1
2
3
4
5
<dependency>
     <groupId>org.springframework.boot</groupId>
     <artifactId>spring-boot-starter-web</artifactId>
     <version> 1.0 . 2 .RELEASE</version>
</dependency>

2.在webapp目录下的index.jsp文件中输入一个表单:

1
2
3
4
5
6
7
8
9
10
<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:

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
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 :

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
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文件

1
2
3
4
5
6
7
8
9
10
<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文件:

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
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

目录
相关文章
|
Java 测试技术 Spring
Spring Boot入门(11)实现文件下载功能
  在这篇博客中,我们将展示如何在Spring Boot中实现文件的下载功能。   还是遵循笔者写博客的一贯风格,简单又不失详细,实用又能让你学会。
2114 0
|
4月前
|
前端开发 JavaScript Java
Spring Boot中Spring MVC的基本配置讲解与实战(包括静态资源配置,拦截器配置,文件上传配置及实战 附源码)
Spring Boot中Spring MVC的基本配置讲解与实战(包括静态资源配置,拦截器配置,文件上传配置及实战 附源码)
51 1
|
4月前
|
前端开发 Java Spring
Spring之文件上传下载,jrebel,多文件上传
Spring之文件上传下载,jrebel,多文件上传
28 0
|
8月前
|
存储 前端开发 Java
Spring Boot之文件上传:实现简单易用的文件上传功能
本篇详细介绍了在Spring Boot应用中实现文件上传功能的步骤。从创建前端页面、编写Controller处理、文件存储与访问、添加路由与页面展示等方面进行了详细讲解,并提供了代码示例。通过学习本文,读者可以轻松了解如何在Spring Boot项目中实现简单易用的文件上传功能,从而在实际应用中更好地满足用户需求。
386 0
Spring Boot之文件上传:实现简单易用的文件上传功能
|
12月前
|
前端开发 Java 数据库
|
Web App开发 前端开发 JavaScript
Spring MVC中文件上传和下载
文件上传需将表格的提交方式设为"POST",并且将enctype设为"multipart/form-data",以二进制的方式提交数据。 spring mvc中可通过MultipartResolver监听每个请求,如有上传的文件,则把请求封装为MultipartHttpServletRequest,通过封装的请求可以获取上传的文件信息和上传的文件。 实际使用可直接将MultipartFile作为控制器中请求处理方法的参数,MultipartFile是一个接口,其实现类为CommonsMultipartFile,通过MultipartFile封装的方法也可获取文件相关信息。
|
前端开发 Java 数据格式
Spring Boot实现文件上传
Spring Boot实现文件上传
527 0
|
Java Spring
Spring Boot文件上传
Spring Boot文件上传
83 0
|
缓存 前端开发 Java
Spring MVC 实现文件的上传和下载
文件的上传和下载是项目开发中非常常见的功能,例如图片、邮件附件的上传与下载,下载与上传音频、视频等。
177 0
Spring MVC 实现文件的上传和下载
|
Java Spring
spring boot 实现文件下载
主要介绍了spring boot 实现文件下载

热门文章

最新文章