jar包:
commons-fileupload-1.3.1.jar
commons-io-1.3.2.jar
maven库
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.3.1</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-io</artifactId>
<version>1.3.2</version>
</dependency>
jsp:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
<%@ page contentType=
"text/html;charset=UTF-8"
language=
"java"
%>
<html>
<head>
<title></title>
</head>
<body>
<h1>
this
is
file upload test!</h1>
<form action=
"/upload"
method=
"post"
enctype=
"multipart/form-data"
>
<input type=
"text"
name=
"username"
/>
<input type=
"file"
name=
"upfile"
/>
<input type=
"submit"
value=
"submit"
/>
</form>
</body>
</html>
|
servlet:
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
|
import
org.apache.commons.fileupload.FileItemIterator;
import
org.apache.commons.fileupload.FileItemStream;
import
org.apache.commons.fileupload.servlet.ServletFileUpload;
import
org.apache.commons.fileupload.util.Streams;
import
javax.servlet.ServletException;
import
javax.servlet.http.HttpServlet;
import
javax.servlet.http.HttpServletRequest;
import
javax.servlet.http.HttpServletResponse;
import
java.io.FileOutputStream;
import
java.io.IOException;
import
java.io.InputStream;
import
java.io.PrintWriter;
/**
* fileupload上传测试
*
* @author leizhimin 14-4-11 上午10:35
*/
public
class
TestuploadServlet
extends
HttpServlet {
@Override
protected
void
doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
boolean flag = ServletFileUpload.isMultipartContent(req);
FileOutputStream fos =
null
;
InputStream
is
=
null
;
try
{
if
(flag) {
ServletFileUpload upload =
new
ServletFileUpload();
FileItemIterator iter = upload.getItemIterator(req);
while
(iter.hasNext()){
FileItemStream fis = iter.next();
is
= fis.openStream();
if
(fis.isFormField()){
System.out.print(fis.getFieldName());
System.out.println(
":"
+ Streams.asString(
is
));
}
else
{
System.out.println(fis.getName());
String
path = req.getSession().getServletContext().getRealPath(
"/upload"
);
path = path+
"/"
+fis.getName();
System.out.println(path);
fos =
new
FileOutputStream(path);
byte[] buff =
new
byte[
1024
];
int
len =
0
;
while
((len=
is
.read(buff))>
0
){
fos.write(buff,
0
,len);
}
}
}
}
}
catch
(Exception e){
}
PrintWriter writer = resp.getWriter();
writer.print(
"Hello World !"
);
}
private
static
final
long serialVersionUID = 1L;
}
|
web.xml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
<?xml version=
"1.0"
encoding=
"UTF-8"
?>
<web-app xmlns=
"http://java.sun.com/xml/ns/javaee"
xmlns:xsi=
"http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http:
//java.sun.com/xml/ns/javaee
http:
//java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
version=
"3.0"
>
<servlet>
<servlet-name>upload</servlet-name>
<servlet-
class
>TestuploadServlet</servlet-
class
>
</servlet>
<servlet-mapping>
<servlet-name>upload</servlet-name>
<url-pattern>/upload</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
<welcome-file>upload.jsp</welcome-file>
</welcome-file-list>
</web-app>
|
运行结果:
1
2
3
|
username:asdf
qhtf.sql
D:\IdeaProjects\fileuploadtest\web\upload/qhtf.sql
|
本文转自 leizhimin 51CTO博客,原文链接:http://blog.51cto.com/lavasoft/1394053,如需转载请自行联系原作者