1. 通过getInputStream()取得上传文件。
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
|
/**
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package
net.individuals.web.servlet;
import
java.io.DataInputStream;
import
java.io.FileOutputStream;
import
java.io.IOException;
import
javax.servlet.ServletException;
import
javax.servlet.annotation.WebServlet;
import
javax.servlet.http.HttpServlet;
import
javax.servlet.http.HttpServletRequest;
import
javax.servlet.http.HttpServletResponse;
/**
*
* @author Barudisshu
*/
@WebServlet
(name =
"UploadServlet"
, urlPatterns = {
"/UploadServlet"
})
public
class
UploadServlet
extends
HttpServlet {
/**
* Processes requests for both HTTP
* <code>GET</code> and
* <code>POST</code> methods.
*
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
protected
void
processRequest(HttpServletRequest request, HttpServletResponse response)
throws
ServletException, IOException {
response.setContentType(
"text/html;charset=UTF-8"
);
//读取请求Body
byte
[] body = readBody(request);
//取得所有Body内容的字符串表示
String textBody =
new
String(body,
"ISO-8859-1"
);
//取得上传的文件名称
String fileName = getFileName(textBody);
//取得文件开始与结束位置
Position p = getFilePosition(request, textBody);
//输出至文件
writeTo(fileName, body, p);
}
//构造类
class
Position {
int
begin;
int
end;
public
Position(
int
begin,
int
end) {
this
.begin = begin;
this
.end = end;
}
}
private
byte
[] readBody(HttpServletRequest request)
throws
IOException {
//获取请求文本字节长度
int
formDataLength = request.getContentLength();
//取得ServletInputStream输入流对象
DataInputStream dataStream =
new
DataInputStream(request.getInputStream());
byte
body[] =
new
byte
[formDataLength];
int
totalBytes =
0
;
while
(totalBytes < formDataLength) {
int
bytes = dataStream.read(body, totalBytes, formDataLength);
totalBytes += bytes;
}
return
body;
}
private
Position getFilePosition(HttpServletRequest request, String textBody)
throws
IOException {
//取得文件区段边界信息
String contentType = request.getContentType();
String boundaryText = contentType.substring(contentType.lastIndexOf(
"="
) +
1
, contentType.length());
//取得实际上传文件的气势与结束位置
int
pos = textBody.indexOf(
"filename=\""
);
pos = textBody.indexOf(
"\n"
, pos) +
1
;
pos = textBody.indexOf(
"\n"
, pos) +
1
;
pos = textBody.indexOf(
"\n"
, pos) +
1
;
int
boundaryLoc = textBody.indexOf(boundaryText, pos) -
4
;
int
begin = ((textBody.substring(
0
, pos)).getBytes(
"ISO-8859-1"
)).length;
int
end = ((textBody.substring(
0
, boundaryLoc)).getBytes(
"ISO-8859-1"
)).length;
return
new
Position(begin, end);
}
private
String getFileName(String requestBody) {
String fileName = requestBody.substring(requestBody.indexOf(
"filename=\""
) +
10
);
fileName = fileName.substring(
0
, fileName.indexOf(
"\n"
));
fileName = fileName.substring(fileName.indexOf(
"\n"
) +
1
, fileName.indexOf(
"\""
));
return
fileName;
}
private
void
writeTo(String fileName,
byte
[] body, Position p)
throws
IOException {
FileOutputStream fileOutputStream =
new
FileOutputStream(
"e:/workspace/"
+ fileName);
fileOutputStream.write(body, p.begin, (p.end - p.begin));
fileOutputStream.flush();
fileOutputStream.close();
}
// <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
/**
* Handles the HTTP
* <code>GET</code> method.
*
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
@Override
protected
void
doGet(HttpServletRequest request, HttpServletResponse response)
throws
ServletException, IOException {
processRequest(request, response);
}
/**
* Handles the HTTP
* <code>POST</code> method.
*
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
@Override
protected
void
doPost(HttpServletRequest request, HttpServletResponse response)
throws
ServletException, IOException {
processRequest(request, response);
}
/**
* Returns a short description of the servlet.
*
* @return a String containing servlet description
*/
@Override
public
String getServletInfo() {
return
"Short description"
;
}
// </editor-fold>
}
|
2. 通过getPart()、getParts()取得上传文件。
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
|
/**
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package
net.individuals.web.servlet;
import
java.io.FileNotFoundException;
import
java.io.FileOutputStream;
import
java.io.IOException;
import
java.io.InputStream;
import
java.io.OutputStream;
import
javax.servlet.ServletException;
import
javax.servlet.annotation.MultipartConfig;
import
javax.servlet.annotation.WebServlet;
import
javax.servlet.http.HttpServlet;
import
javax.servlet.http.HttpServletRequest;
import
javax.servlet.http.HttpServletResponse;
import
javax.servlet.http.Part;
/**
*
* @author Barudisshu
*/
@MultipartConfig
@WebServlet
(name =
"UploadServlet"
, urlPatterns = {
"/UploadServlet"
})
public
class
UploadServlet
extends
HttpServlet {
/**
* Processes requests for both HTTP
* <code>GET</code> and
* <code>POST</code> methods.
*
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
protected
void
processRequest(HttpServletRequest request, HttpServletResponse response)
throws
ServletException, IOException {
Part part = request.getPart(
"photo"
);
String fileName = getFileName(part);
writeTo(fileName, part);
}
//取得上传文件名
private
String getFileName(Part part) {
String header = part.getHeader(
"Content-Disposition"
);
String fileName = header.substring(header.indexOf(
"filename=\""
) +
10
, header.lastIndexOf(
"\""
));
return
fileName;
}
//存储文件
private
void
writeTo(String fileName, Part part)
throws
IOException, FileNotFoundException {
InputStream in = part.getInputStream();
OutputStream out =
new
FileOutputStream(
"e:/workspace/"
+ fileName);
byte
[] buffer =
new
byte
[
1024
];
int
length = -
1
;
while
((length = in.read(buffer)) != -
1
) {
out.write(buffer,
0
, length);
}
in.close();
out.close();
}
// <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
/**
* Handles the HTTP
* <code>GET</code> method.
*
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
@Override
protected
void
doGet(HttpServletRequest request, HttpServletResponse response)
throws
ServletException, IOException {
processRequest(request, response);
}
/**
* Handles the HTTP
* <code>POST</code> method.
*
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
@Override
protected
void
doPost(HttpServletRequest request, HttpServletResponse response)
throws
ServletException, IOException {
processRequest(request, response);
}
/**
* Returns a short description of the servlet.
*
* @return a String containing servlet description
*/
@Override
public
String getServletInfo() {
return
"Short description"
;
}
// </editor-fold>
}
|
另一种较为简单的方法:
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
|
/**
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package
net.individuals.web.servlet;
import
java.io.IOException;
import
java.io.PrintWriter;
import
javax.servlet.ServletException;
import
javax.servlet.annotation.MultipartConfig;
import
javax.servlet.annotation.WebServlet;
import
javax.servlet.http.HttpServlet;
import
javax.servlet.http.HttpServletRequest;
import
javax.servlet.http.HttpServletResponse;
import
javax.servlet.http.Part;
/**
*采用part的wirte(String fileName)上传,浏览器将产生临时TMP文件。
* @author Barudisshu
*/
@MultipartConfig
(location =
"e:/workspace"
)
@WebServlet
(name =
"UploadServlet"
, urlPatterns = {
"/UploadServlet"
})
public
class
UploadServlet
extends
HttpServlet {
/**
* Processes requests for both HTTP
* <code>GET</code> and
* <code>POST</code> methods.
*
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
protected
void
processRequest(HttpServletRequest request, HttpServletResponse response)
throws
ServletException, IOException {
//处理中文文件名
request.setCharacterEncoding(
"UTF-8"
);
Part part = request.getPart(
"photo"
);
String fileName = getFileName(part);
//将文件写入location指定的目录
part.write(fileName);
}
private
String getFileName(Part part) {
String header = part.getHeader(
"Content-Disposition"
);
String fileName = header.substring(header.indexOf(
"filename=\""
) +
10
, header.lastIndexOf(
"\""
));
return
fileName;
}
// <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
/**
* Handles the HTTP
* <code>GET</code> method.
*
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
@Override
protected
void
doGet(HttpServletRequest request, HttpServletResponse response)
throws
ServletException, IOException {
processRequest(request, response);
}
/**
* Handles the HTTP
* <code>POST</code> method.
*
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
@Override
protected
void
doPost(HttpServletRequest request, HttpServletResponse response)
throws
ServletException, IOException {
processRequest(request, response);
}
/**
* Returns a short description of the servlet.
*
* @return a String containing servlet description
*/
@Override
public
String getServletInfo() {
return
"Short description"
;
}
// </editor-fold>
}
|
使用getParts()上传多个文件:
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
|
<!--
To change this template, choose Tools | Templates
and open the template in the editor.
-->
<!
DOCTYPE
html>
<
html
>
<
head
>
<
title
></
title
>
<
meta
http-equiv
=
"Content-Type"
content
=
"text/html; charset=UTF-8"
>
</
head
>
<
body
>
<
div
>
<
form
action
=
"UploadServlet3"
method
=
"POST"
enctype
=
"multipart/form-data"
>
<
table
>
<
tr
>
<
td
><
label
for
=
"file1"
>文件1:</
label
></
td
>
<
td
><
input
type
=
"file"
id
=
"file1"
name
=
"file"
></
td
>
</
tr
>
<
tr
>
<
td
><
label
for
=
"file2"
>文件2:</
label
></
td
>
<
td
><
input
type
=
"file"
id
=
"file2"
name
=
"file"
></
td
>
</
tr
>
<
tr
>
<
td
><
label
for
=
"file3"
>文件3:</
label
></
td
>
<
td
><
input
type
=
"file"
id
=
"file3"
name
=
"file"
></
td
>
</
tr
>
<
tr
>
<
td
colspan
=
"2"
><
input
type
=
"submit"
value
=
"上传"
name
=
"upload"
></
td
>
</
tr
>
</
table
>
</
form
>
</
div
>
</
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
|
/**
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package
net.individuals.web.servlet;
import
java.io.IOException;
import
java.io.PrintWriter;
import
javax.servlet.ServletException;
import
javax.servlet.annotation.MultipartConfig;
import
javax.servlet.annotation.WebServlet;
import
javax.servlet.http.HttpServlet;
import
javax.servlet.http.HttpServletRequest;
import
javax.servlet.http.HttpServletResponse;
import
javax.servlet.http.Part;
/**
*
* @author Barudisshu
*/
@MultipartConfig
(location =
"e:/workspace"
)
@WebServlet
(name =
"UploadServlet"
, urlPatterns = {
"/UploadServlet"
})
public
class
UploadServlet
extends
HttpServlet {
/**
* Processes requests for both HTTP
* <code>GET</code> and
* <code>POST</code> methods.
*
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
protected
void
processRequest(HttpServletRequest request, HttpServletResponse response)
throws
ServletException, IOException {
request.setCharacterEncoding(
"utf-8"
);
//迭代Collection中所有Part对象
for
(Part part : request.getParts()) {
//只处理上传文件区段
if
(part.getName().startsWith(
"file"
)) {
String fileName = getFileName(part);
part.write(fileName);
}
}
}
private
String getFileName(Part part) {
String header = part.getHeader(
"Content-Disposition"
);
String fileName = header.substring(header.indexOf(
"filename=\""
) +
10
, header.lastIndexOf(
"\""
));
header.lastIndexOf(
"\""
);
return
fileName;
}
// <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
/**
* Handles the HTTP
* <code>GET</code> method.
*
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
@Override
protected
void
doGet(HttpServletRequest request, HttpServletResponse response)
throws
ServletException, IOException {
processRequest(request, response);
}
/**
* Handles the HTTP
* <code>POST</code> method.
*
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
@Override
protected
void
doPost(HttpServletRequest request, HttpServletResponse response)
throws
ServletException, IOException {
processRequest(request, response);
}
/**
* Returns a short description of the servlet.
*
* @return a String containing servlet description
*/
@Override
public
String getServletInfo() {
return
"Short description"
;
}
// </editor-fold>
}
网址:http://my.oschina.net/Barudisshu/blog/150026
|