转载来自:http://www.it165.net/pro/html/201402/9201.html
摘要:web开发、文件上传就像我们平时吃饭一样、少不了的功能。本文主要实现springMVC的两种实现方式:通过参数传递上传文件、通过解析request上传文件、强推后面一种。
一:实现功能
1、通过参数传递形式上传文件
2、通过解析request上传文件
二:具体步骤
1、引入jar包、主要是两个
![图片加载中... 加载中...](https://i-blog.csdnimg.cn/blog_migrate/e932def16518dbb7f683060a49d35347.gif)
2、不管是那种方式、首先要在springMVC的配置文件中配置有管springMVC对上传文件的处理的类springAnnotation-servlet.xml关于文件处理配置:
1.
<!-- 配置springMVC处理上传文件的信息 -->
2.
<bean id=
"multipartResolver"
3.
class
=
"org.springframework.web.multipart.commons.CommonsMultipartResolver"
>
4.
<property name=
"defaultEncoding"
value=
"utf-8"
/>
5.
<property name=
"maxUploadSize"
value=
"10485760000"
/>
6.
<property name=
"maxInMemorySize"
value=
"40960"
/>
7.
</bean>
3、参数传递(详细信息代码有说明):
01.
/**
02.
* 参数接收形式上传文件
03.
*/
04.
@RequestMapping
(
"/upload"
)
05.
public
String uploadFile(
@RequestParam
(
"file"
) CommonsMultipartFile file,
06.
HttpServletRequest request)
throws
IOException {
07.
System.out.println(
"fileName : "
+ file.getOriginalFilename());
08.
09.
if
(!file.isEmpty()) {
10.
BufferedOutputStream bos =
new
BufferedOutputStream(
11.
new
FileOutputStream(
"D:/"
+ file.getOriginalFilename()));
12.
InputStream in = file.getInputStream();
13.
BufferedInputStream bis =
new
BufferedInputStream(in);
14.
int
n =
0
;
15.
byte
[] b =
new
byte
[
1024
];
16.
while
((n = bis.read(b)) != -
1
) {
17.
bos.write(b,
0
, n);
18.
}
19.
bos.flush();
20.
bos.close();
21.
bis.close();
22.
}
23.
return
"/success"
;
24.
}
4、解析request(详细信息代码有说明):
01.
/**
02.
* springMVC封装的解析request上传文件(快捷、与springMVC很好结合,首选)
03.
*/
04.
@RequestMapping
(
"/upload2"
)
05.
public
String uploadFile2(
@RequestParam
(
"file"
) CommonsMultipartFile file,
06.
HttpServletRequest request)
throws
IOException {
07.
// 定义解析器去解析request
08.
CommonsMultipartResolver mutilpartResolver =
new
CommonsMultipartResolver(request.getSession().getServletContext());
09.
//request如果是Multipart类型、
10.
if
(mutilpartResolver.isMultipart(request)) {
11.
//强转成 MultipartHttpServletRequest
12.
MultipartHttpServletRequest multiRequest = (MultipartHttpServletRequest) request;
13.
//获取文件名
14.
Iterator<String> it = multiRequest.getFileNames();
15.
while
(it.hasNext()) {
16.
//获取MultipartFile类型文件
17.
MultipartFile fileDetail = multiRequest.getFile(it.next());
18.
if
(fileDetail !=
null
) {
19.
String fileName =
"demoUpload"
+ fileDetail.getOriginalFilename();
20.
String path =
"D:/"
+ fileName;
21.
File localFile =
new
File(path);
22.
//将上传文件写入到指定文件出、核心!
23.
fileDetail.transferTo(localFile);
24.
//非常重要、有了这个想做什么处理都可以
25.
//fileDetail.getInputStream();
26.
}
27.
}
28.
}
29.
return
"/success"
;
30.
}
补充
1、整体结构图及jar包图
2、具体页面、Controller、springAnnotation-servlet.xml(嫌烦可无视)
a) 上传页面 upload.jsp
01.
<%@ page language=
"java"
contentType=
"text/html; charset=UTF-8"
02.
pageEncoding=
"UTF-8"
%>
03.
<%
@taglib
prefix=
"c"
uri=
"http://java.sun.com/<;a href="
http://www.it165.net/pro/webjsp/
" target="
_blank
" class="
keylink
">jsp</a>/jstl/core"
%>
04.
<!DOCTYPE html PUBLIC
"-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd"
>
05.
<html>
06.
<head>
07.
<script type=
"text/javascript"
src=
"../js/jquery-1.7.1.min.js"
></script>
08.
<meta http-equiv=
"Content-Type"
content=
"text/html; charset=UTF-8"
>
09.
<title>上传文件</title>
10.
<script type=
"text/javascript"
>
11.
12.
</script>
13.
</head>
14.
<body>
15.
<h4>上传文件
1
</h4>
16.
<form name=
"userForm"
action=
"/springMVC7/file/upload2"
method=
"post"
enctype=
"multipart/form-data"
>
17.
选择文件:<input type=
"file"
name=
"file"
>
18.
<input type=
"submit"
value=
"上传"
>
19.
</form>
20.
</body>
21.
</html>
b)配置文件 springAnnotation-servlet.xml
01.
<?xml version=
"1.0"
encoding=
"UTF-8"
?>
02.
<beans xmlns=
"http://www.springframework.org/schema/beans"
03.
xmlns:context=
"http://www.springframework.org/schema/context"
xmlns:p=
"http://www.springframework.org/schema/p"
04.
xmlns:mvc=
"http://www.springframework.org/schema/mvc"
xmlns:xsi=
"http://www.w3.org/2001/XMLSchema-instance"
05.
xsi:schemaLocation="http:
//www.springframework.org/schema/beans
06.
http:
//www.springframework.org/schema/beans/spring-beans-3.0.xsd
07.
http:
//www.springframework.org/schema/context
08.
http:
//www.springframework.org/schema/context/spring-context.xsd
09.
http:
//www.springframework.org/schema/mvc
10.
http:
//www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
11.
12.
13.
<!-- 注解扫描包 -->
14.
<context:component-scan base-
package
=
"com.chy.web.controller.annotation"
></context:component-scan>
15.
16.
<!-- 优化一:使用mvc标签的代替下面的两个bean -->
17.
<mvc:annotation-driven />
18.
<!-- 不拦截静态资源 -->
19.
<mvc:
default
-servlet-handler />
20.
21.
<!-- 开启注解 <bean
class
=
"org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping"
></bean>
22.
<bean
class
=
"org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"
></bean> -->
23.
24.
<bean id=
"viewResolver"
25.
class
=
"org.springframework.web.servlet.view.InternalResourceViewResolver"
>
26.
<property name=
"prefix"
value=
"/"
></property>
27.
<property name=
"suffix"
value=
".jsp"
></property>
28.
</bean>
29.
30.
<!-- 配置springMVC处理上传文件的信息 -->
31.
<bean id=
"multipartResolver"
32.
class
=
"org.springframework.web.multipart.commons.CommonsMultipartResolver"
>
33.
<property name=
"defaultEncoding"
value=
"utf-8"
/>
34.
<property name=
"maxUploadSize"
value=
"10485760000"
/>
35.
<property name=
"maxInMemorySize"
value=
"40960"
/>
36.
</bean>
37.
</beans>
c)处理上传文件的类 UploadController
01.
package
com.chy.web.controller.annotation.upload;
02.
03.
import
java.io.BufferedInputStream;
04.
import
java.io.BufferedOutputStream;
05.
import
java.io.File;
06.
import
java.io.FileOutputStream;
07.
import
java.io.IOException;
08.
import
java.io.InputStream;
09.
import
java.util.Iterator;
10.
11.
import
javax.servlet.http.HttpServletRequest;
12.
13.
import
org.springframework.stereotype.Controller;
14.
import
org.springframework.web.bind.annotation.RequestMapping;
15.
import
org.springframework.web.bind.annotation.RequestParam;
16.
import
org.springframework.web.multipart.MultipartFile;
17.
import
org.springframework.web.multipart.MultipartHttpServletRequest;
18.
import
org.springframework.web.multipart.commons.CommonsMultipartFile;
19.
import
org.springframework.web.multipart.commons.CommonsMultipartResolver;
20.
21.
@Controller
22.
@RequestMapping
(
"/file"
)
23.
public
class
UploadController {
24.
/*
25.
* 上传文件的两种方法、 1、一种用参数接收 2、解析request
26.
*/
27.
28.
/**
29.
* 参数接收形式上传文件
30.
*/
31.
@RequestMapping
(
"/upload"
)
32.
public
String uploadFile(
@RequestParam
(
"file"
) CommonsMultipartFile file,
33.
HttpServletRequest request)
throws
IOException {
34.
System.out.println(
"fileName : "
+ file.getOriginalFilename());
35.
36.
if
(!file.isEmpty()) {
37.
BufferedOutputStream bos =
new
BufferedOutputStream(
38.
new
FileOutputStream(
"D:/"
+ file.getOriginalFilename()));
39.
InputStream in = file.getInputStream();
40.
BufferedInputStream bis =
new
BufferedInputStream(in);
41.
int
n =
0
;
42.
byte
[] b =
new
byte
[
1024
];
43.
while
((n = bis.read(b)) != -
1
) {
44.
bos.write(b,
0
, n);
45.
}
46.
bos.flush();
47.
bos.close();
48.
bis.close();
49.
}
50.
return
"/success"
;
51.
}
52.
53.
/**
54.
* springMVC封装的解析request上传文件(快捷、与springMVC很好结合,首选)
55.
*/
56.
@RequestMapping
(
"/upload2"
)
57.
public
String uploadFile2(
@RequestParam
(
"file"
) CommonsMultipartFile file,
58.
HttpServletRequest request)
throws
IOException {
59.
// 定义解析器去解析request
60.
CommonsMultipartResolver mutilpartResolver =
new
CommonsMultipartResolver(request.getSession().getServletContext());
61.
//request如果是Multipart类型、
62.
if
(mutilpartResolver.isMultipart(request)) {
63.
//强转成 MultipartHttpServletRequest
64.
MultipartHttpServletRequest multiRequest = (MultipartHttpServletRequest) request;
65.
//获取文件名
66.
Iterator<String> it = multiRequest.getFileNames();
67.
while
(it.hasNext()) {
68.
//获取MultipartFile类型文件
69.
MultipartFile fileDetail = multiRequest.getFile(it.next());
70.
if
(fileDetail !=
null
) {
71.
String fileName =
"demoUpload"
+ fileDetail.getOriginalFilename();
72.
String path =
"D:/"
+ fileName;
73.
File localFile =
new
File(path);
74.
//将上传文件写入到指定文件出、核心!
75.
fileDetail.transferTo(localFile);
76.
//非常重要、有了这个想做什么处理都可以
77.
//fileDetail.getInputStream();
78.
}
79.
}
80.
}
81.
return
"/success"
;
82.
}
83.
84.
@RequestMapping
(
"toUpload"
)
85.
public
String tpUpload() {
86.
return
"/upload"
;
87.
}
88.
}