<%@
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
*/
publicclass
FileUploadAction
extends
ActionSupport {
privatestaticfinalint
BUFFER_SIZE
= 16 * 1024;
//
文件标题
private
String
title
;
// 上传文件域对象
private File upload;
// 上传文件名
private String uploadFileName;
// 上传文件类型
private String uploadContentType;
// 保存文件的目录路径(通过依赖注入)
private String savePath;
//
以下省略
getter
和
setter......
//
自己封装的一个把源文件对象复制成目标文件对象
privatestaticvoid
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 =
newbyte
[
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/pjpeg
</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
|
<%@
page
language
=
"java"
contentType
=
"text/html; charset=UTF-8"
%>
<%@
taglib
uri
=
"/struts-tags"
prefix
=
"s"
%>
<
html
>
<
head
>
<
title
>
多文件上传
</
title
>
</
head
>
<
body
>
<
font
color
=
"red"
><
s:fielderror
/></
font
>
<
form
action
=
"multiFileUpload.action"
method
=
"POST"
enctype
=
"multipart/form-data"
>
文件标题:
<
input
type
=
"text"
name
=
"title"
size
=
"50"
value
=
"${param.title }"/><br/>
<!--
设置二个文件域
,
名字相同
-->
选择第一个文件:
<
input
type
=
"file"
name
=
"upload"
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.IOException;
import
java.io.InputStream;
import
java.io.OutputStream;
import
org.apache.struts2.ServletActionContext;
import
com.opensymphony.xwork2.ActionSupport;
/**
*
处理多文件上传的
Action
类
*
*
@author
qiujy
*
@version
1.0
*/
publicclass
MultiFileUploadAction
extends
ActionSupport {
privatestaticfinalint
BUFFER_SIZE
= 16 * 1024;
//
文件标题
private
String
title
;
//
用
File
数组来封装多个上传文件域对象
private
File[]
upload
;
//
用
String
数组来封装多个上传文件名
private
String[]
uploadFileName
;
//
用
String
数组来封装多个上传文件类型
private
String[]
uploadContentType
;
//
保存文件的目录路径
(
通过依赖注入
)
private
String
savePath
;
//
以下为所有属性的
getter
和
setter
。省略。。。
//
自己封装的一个把源文件对象复制成目标文件对象
privatestaticvoid
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 =
newbyte
[
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 {
File[] srcFiles =
this
.getUpload();
//
处理每个要上传的文件
for
(
int
i = 0; i < srcFiles.
length
; i++) {
//
根据服务器的文件保存地址和原文件名创建目录文件全路径
String dstPath = ServletActionContext.getServletContext()
.getRealPath(
this
.getSavePath())
+
"//"
+
this
.getUploadFileName()[i];
File dstFile =
new
File(dstPath);
this
.copy(srcFiles[i], dstFile);
}
return
SUCCESS
;
}
}
|