本篇文章,我们要来做一个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
相关推荐
采用前后端分离的方式进行开发,实现了几种常用的文件上传功能。 前端采用 vue.js + plupload + element-ui 实现了文件在浏览器端的发送, 后端采用 spring boot + spring + spring mvc + mybatis 实现了文件在服务器...
文件上传是Web 程序经常用到的功能,例如MSDN 的资源上传,yahoo邮箱的附件上传,论坛的附件上传等,文件上传的方式有很多种,例如,使用Struts框架实现文件上传,在JSP中使用jspSmartUpload 组件实现文件上传和使用...
简单实现了spring上传文件的功能简单实现了spring上传文件的功能简单实现了spring上传文件的功能简单实现了spring上传文件的功能简单实现了spring上传文件的功能简单实现了spring上传文件的功能简单实现了spring上传...
通用excel导入功能 文件上传下载功能 以及相关的依赖包 自己用的,下载你自己给分
开发工具:eclipse+tomcat+jdk1.7 基于springMVC的文件上传功能
3、实现文件上传功能:在文件上传的方法中,可以使用MultipartFile类型的参数接收上传的文件。通过transferTo()方法将文件保存到指定的路径。 4、实现文件下载功能:在文件下载的方法中,可以使用ResponseEntity...
Spring 官方提供的代码示例,支持通过“multi-part” 实现文件上传的功能。 代码运行要求: 1. 至少 Java 17 版本 2. Gradle 7.5+ 或者 Maven 3.5+
spring boot+thymeleaf 文件上传,下载功能实现。刚写的热乎的,需要的自行下载。
本篇文章主要介绍了Spring实现文件上传功能,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
基于Spring Boot的文件管理系统,支持文件上传,下载,删除等操作,在线浏览文件列表及基本信息等操作。部署简单,Java课程设计必备。 依赖软件: 1、jdk1.8+ 2、maven 开发调试方法: 解压压缩包,导入IDE,...
前几天在网上找springmvc 文件上传显示进度大部分找的都有问题,然后自己结合网上资料写了一个可以实现的功能,现在已经用在向项目中
加上做学校用得系统,不断地调研,分析需求,所以做的比较慢,一直上线测试,然后就调式改BUG,最后系统能基本实现学校的要求,文件上传下载这个模块功能也是很好地实现,我将系统中的文件上传和下载的功能模块抽...
本文是结合博客的源码,链接是:http://blog.csdn.net/u012660464/article/details/53434331 。名为:SpringMVC轻松实现文件上传功能。其是通过表单进行上传的。
主要介绍了Spring boot 实现单个或批量文件上传功能,非常不错,具有一定的参考借鉴价值,需要的朋友参考下吧
1、该demo是基于之前的spring boot集成mybatis的基础上实现了文件上传功能。 2、工具为eclipse,启动类为Application(main方法)。 3、启动后浏览器输入:http://localhost:8080/main/gouploadimg
在Spring Boot中实现文件上传和下载功能可以通过以下步骤进行操作: 文件上传: 1.配置文件上传相关的依赖: 在项目的 pom.xml 文件中添加依赖 2.配置文件上传的控制器(Controller): 创建一个控制器来处理文件...
SpringBoot实现文件上传和下载
主要为大家详细介绍了Spring Cloud中FeignClient实现文件上传功能,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
ssh整合 struts2.3 spring3.1 hibernate4.0 mysql 整合。包含了所有的配置文件和jar包。并且实现文件上传,同时检测上传文件的大小。解压文件。包含所有的文件。