2.1. 步驟一:建立帶上傳表單域的頁面html
<%@
page
language
=
"java"
contentType
=
"text/html; charset=UTF-8"
%>
<
html
>
<
head
>
<
title
>
Struts2 File Upload
</
title
>
</
head
>
<
body
>
<
form
action
=
"fileUpload.action"
method
=
"POST"
enctype
=
"multipart/form-data"
>
文件標題:
<
input
type
=
"text"
name
=
"title"
size
=
"50"
/><
br
/>
選擇文件:
<
input
type
=
"file"
name
=
"upload"
size
=
"50"
/><
br
/>
<
input
type
=
"submit"
value
=
"
上傳
"
/>
</
form
>
</
body
>
</
html
>
|
package
org.qiujy.web.struts2;
import
java.io.BufferedInputStream;
import
java.io.BufferedOutputStream;
import
java.io.File;
import
java.io.FileInputStream;
import
java.io.FileOutputStream;
import
java.io.InputStream;
import
java.io.OutputStream;
import
org.apache.struts2.ServletActionContext;
import
com.opensymphony.xwork2.ActionSupport;
/**
*
處理文件上傳的
Action
類
*
@author
qiujy
*
@version
1.0
*/
public
class
FileUploadAction
extends
ActionSupport {
private
static
final
int
BUFFER_SIZE
= 16 * 1024;
//
文件標題
private
String
title
;
//
上傳文件域對象
private
File
upload
;
//
上傳文件名
private
String
uploadFileName
;
//
上傳文件類型
private
String
uploadContentType
;
//
保存文件的目錄路徑
(
經過依賴注入
)
private
String
savePath
;
//
如下省略
getter
和
setter......
//
本身封裝的一個把源文件對象複製成目標文件對象
private
static
void
copy(File src, File dst) {
InputStream in =
null
;
OutputStream out =
null
;
try
{
in =
new
BufferedInputStream(
new
FileInputStream(src),
BUFFER_SIZE
);
out =
new
BufferedOutputStream(
new
FileOutputStream(dst),
BUFFER_SIZE
);
byte
[] buffer =
new
byte
[
BUFFER_SIZE
];
int
len = 0;
while
((len = in.read(buffer)) > 0) {
out.write(buffer, 0, len);
}
}
catch
(Exception e) {
e.printStackTrace();
}
finally
{
if
(
null
!= in) {
try
{
in.close();
}
catch
(IOException e) {
e.printStackTrace();
}
}
if
(
null
!= out) {
try
{
out.close();
}
catch
(IOException e) {
e.printStackTrace();
}
}
}
}
@Override
public
String execute()
throws
Exception {
//
根據服務器的文件保存地址和原文件名建立目錄文件全路徑
String dstPath = ServletActionContext.getServletContext()
.getRealPath(
this
.getSavePath())
+
"\\"
+
this
.getUploadFileName();
System.
out
.println(
"
上傳的文件的類型:
"
+
this
.getUploadContentType());
File dstFile =
new
File(dstPath);
copy(
this
.
upload
, dstFile);
return
SUCCESS
;
}
}
|
<!
DOCTYPE
struts
PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd"
>
<
struts
>
<
package
name
=
"fileUploadDemo"
extends
=
"struts-default"
>
<
action
name
=
"fileUpload"
class
=
"org.qiujy.web.struts2.FileUploadAction"
>
<!--
動態設置
Action
中的
savePath
屬性的值
-->
<
param
name
=
"savePath"
>
/upload
</
param
>
<
result
name
=
"success"
>
/showupload.jsp
</
result
>
</
action
>
</
package
>
</
struts
>
|
<!
DOCTYPE
struts
PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd"
>
<
struts
>
<
constant
name
=
"struts.custom.i18n.resources"
value
=
"messages"
/>
<
package
name
=
"fileUploadDemo"
extends
=
"struts-default"
>
<
action
name
=
"fileUpload"
class
=
"org.qiujy.web.struts2.FileUploadAction"
>
<
interceptor-ref
name
=
"fileUpload"
>
<!--
配置容許上傳的文件類型,多個用
","
分隔
-->
<
param
name
=
"allowedTypes"
>
image/bmp,image/png,image/gif,image/jpeg,image/jpg
,image/x-png, image/pjpegjava
</
param
>
<!--
配置容許上傳的文件大小,單位字節
-->
<
param
name
=
"maximumSize"
>
102400
</
param
>
</
interceptor-ref
>
<
interceptor-ref
name
=
"defaultStack"
/>
<!--
動態設置
Action
中的
savePath
屬性的值
-->
<
param
name
=
"savePath"
>
/upload
</
param
>
<
result
name
=
"input"
>
/index.jsp
</
result
>
<
result
name
=
"success"
>
/showupload.jsp
</
result
>
</
action
>
</
package
>
</
struts
>
|
#
更改上傳文件類型不容許的提示信息
struts.messages.error.content.type.not.allowed=
文件上傳失敗:你要上傳的文件類型不容許
#
更改上傳文件太大的提示信息
struts.messages.error.file.too.large=
文件上傳失敗:你要上傳的文件太大
#
文件上傳其它錯誤信息
struts.messages.error.uploading=
文件上傳失敗:發生內部錯誤
|
Unable to find 'struts.multipart.saveDir' property setting. Defaulting to javax.servlet.context.tempdir
Removing file upload D:\tomcat6.0.13\work\Catalina\localhost\fileload_struts2\upload__4b616fd1_115a3d5d9dc__7fff_00000005.tmp
|