基于maven管理spring boot的批量上传
来源:互联网 发布:理财 大数据 组合 编辑:程序博客网 时间:2024/06/03 20:44
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"
;
}
}
需要注意的是本地的地址 和后台的取值和遍历问题
0 0
- 基于maven管理spring boot的批量上传
- spring-boot入门整合jpa基于maven
- 初识spring boot maven管理--使用spring-boot-starter-parent
- 基于Spring CommonsMultipartResolver批量上传文件
- 基于uploadfile的批量上传
- 初识spring boot maven管理--配置文件
- 初识spring boot maven管理--HelloWorld
- 初识spring boot maven管理--SpringMVC
- Spring Boot Maven 管理 pom.xml
- Spring Boot的Maven插件Spring Boot Maven plugin详解
- Spring Boot的Maven插件Spring Boot Maven plugin详解
- Spring Boot的Maven插件Spring Boot Maven plugin详解
- 基于maven 管理下Spring,Struts2,hibernate的pom配置
- 如何构建基于maven管理的spring web项目
- Maven构建一个多模块的Spring Boot + Spring MVC项目,完全基于java config
- Spring Boot的日志管理
- Spring Boot的日志管理
- 在IntelliJ IDEA下搭建基于Maven的spring boot框架
- xml与ajax联合实例
- Android学习笔记(二十二):关于ListView自动滚动一个位置
- 私有通讯协议
- JS maximum call stack size exceeded
- stm32 JTAG和SWD的使用区别
- 基于maven管理spring boot的批量上传
- JDBC连接池的创建
- tensorflow 莫烦视频学习笔记 变量 (二)
- 新人程序员如何脱颖而出
- this详解
- FreeRTOS源码分析之heap1.c
- JIRA增加自定义导航菜单项---普通用户查看所有项目
- 【IMWeb训练营作业】Vuejs 简单使用
- 俄罗斯黑客在暗网销售廉价的Ransomware服务