struts2之多个文件上传

struts2之多个文件上传

通过3种方式模拟多个文件上传,效果如下所示

2011032120584292.png

         

目录结构

 2011032120592080.png

新建Action

第一种方式


package
com.ljq.action;


import
java.io.File;


import
org.apache.commons.io.FileUtils;

import
org.apache.struts2.ServletActionContext;


import
com.opensymphony.xwork2.ActionContext;

import
com.opensymphony.xwork2.ActionSupport;

@SuppressWarnings(
“
serial
“
)

public

class
UploadAction
extends
ActionSupport{


private
File[] image;
//
上传的文件



private
String[] imageFileName;
//
文件名称



private
String[] imageContentType;
//
文件类型




public
String execute()
throws
Exception {

ServletActionContext.getRequest().setCharacterEncoding(

“
UTF-8
“
);
String realpath

=
ServletActionContext.getServletContext().getRealPath(
“
/images
“
);
System.out.println(realpath);

if
(image
!=

null
) {

File savedir

=
new
File(realpath);

if
(
!
savedir.getParentFile().exists())
savedir.getParentFile().mkdirs();

for
(
int
i
=
0
;i
<
image.length;i
++
){

File savefile

=

new
File(savedir, imageFileName[i]);
FileUtils.copyFile(image[i], savefile);
}
ActionContext.getContext().put(

“
message
“
,
“
文件上传成功
“
);
}

return

“
success
“
;
}


public
File[] getImage() {


return
image;
}


public

void
setImage(File[] image) {


this
.image
=
image;
}


public
String[] getImageContentType() {


return
imageContentType;
}


public

void
setImageContentType(String[] imageContentType) {


this
.imageContentType
=
imageContentType;
}


public
String[] getImageFileName() {


return
imageFileName;
}


public

void
setImageFileName(String[] imageFileName) {


this
.imageFileName
=
imageFileName;
}

}

                   

第二种方式


package
com.ljq.action;


import
java.io.File;

import
java.io.FileInputStream;

import
java.io.FileOutputStream;


import
org.apache.struts2.ServletActionContext;


import
com.opensymphony.xwork2.ActionSupport;

/**

* 使用数组上传多个文件
*
*

@author
ljq
*

*/

@SuppressWarnings(

“
serial
“
)

public

class
UploadAction2
extends
ActionSupport{


private
File[] image;
//
上传的文件



private
String[] imageFileName;
//
文件名称



private
String[] imageContentType;
//
文件类型



private
String savePath;

@Override

public
String execute()
throws
Exception {

ServletActionContext.getRequest().setCharacterEncoding(

“
UTF-8
“
);

//
取得需要上传的文件数组


File[] files
=
getImage();

if
(files
!=
null

&&
files.length
>

0
) {


for
(
int
i
=

0
; i
<
files.length; i
++
) {


//
建立上传文件的输出流, getImageFileName()[i]


System.out.println(getSavePath()
+

“
\\
“

+
getImageFileName()[i]);
FileOutputStream fos

=

new
FileOutputStream(getSavePath()
+

“
\\
“

+
getImageFileName()[i]);

//
建立上传文件的输入流


FileInputStream fis
=

new
FileInputStream(files[i]);

byte
[] buffer
=

new

byte
[
1024
];

int
len
=

0
;

while
((len
=
fis.read(buffer))
>
0
) {

fos.write(buffer,

0
, len);
}
fos.close();
fis.close();
}
}

return
SUCCESS;
}


public
File[] getImage() {


return
image;
}


public

void
setImage(File[] image) {


this
.image
=
image;
}


public
String[] getImageFileName() {


return
imageFileName;
}


public

void
setImageFileName(String[] imageFileName) {


this
.imageFileName
=
imageFileName;
}


public
String[] getImageContentType() {


return
imageContentType;
}


public

void
setImageContentType(String[] imageContentType) {


this
.imageContentType
=
imageContentType;
}


/**

* 返回上传文件保存的位置
*
*

@return

*

@throws
Exception

*/


public
String getSavePath()
throws
Exception {


return
ServletActionContext.getServletContext().getRealPath(savePath);
}


public

void
setSavePath(String savePath) {


this
.savePath
=
savePath;
}

}

            

第三种方式


package
com.ljq.action;


import
java.io.File;

import
java.io.FileInputStream;

import
java.io.FileOutputStream;

import
java.util.List;


import
org.apache.struts2.ServletActionContext;


import
com.opensymphony.xwork2.ActionSupport;


/**

* 使用List上传多个文件
*
*

@author
ljq
*

*/

@SuppressWarnings(

“
serial
“
)

public

class
UploadAction3
extends
ActionSupport {


private
List
<
File
>
image;
//
上传的文件



private
List
<
String
>
imageFileName;
//
文件名称



private
List
<
String
>
imageContentType;
//
文件类型



private
String savePath;

@Override

public
String execute()
throws
Exception {

ServletActionContext.getRequest().setCharacterEncoding(

“
UTF-8
“
);

//
取得需要上传的文件数组


List
<
File
>
files
=
getImage();

if
(files
!=

null

&&
files.size()
>

0
) {


for
(
int
i
=

0
; i
<
files.size(); i
++
) {

FileOutputStream fos

=

new
FileOutputStream(getSavePath()
+

“
\\
“

+
getImageFileName().get(i));
FileInputStream fis

=

new
FileInputStream(files.get(i));

byte
[] buffer
=

new

byte
[
1024
];

int
len
=

0
;

while
((len
=
fis.read(buffer))
>

0
) {

fos.write(buffer,

0
, len);
}
fis.close();
fos.close();
}
}

return
SUCCESS;
}


public
List
<
File
>
getImage() {


return
image;
}


public

void
setImage(List
<
File
>
image) {


this
.image
=
image;
}


public
List
<
String
>
getImageFileName() {


return
imageFileName;
}


public

void
setImageFileName(List
<
String
>
imageFileName) {


this
.imageFileName
=
imageFileName;
}


public
List
<
String
>
getImageContentType() {


return
imageContentType;
}


public

void
setImageContentType(List
<
String
>
imageContentType) {


this
.imageContentType
=
imageContentType;
}


/**

* 返回上传文件保存的位置
*
*

@return

*

@throws
Exception

*/


public
String getSavePath()
throws
Exception {


return
ServletActionContext.getServletContext().getRealPath(savePath);
}


public

void
setSavePath(String savePath) {


this
.savePath
=
savePath;
}

}

              

struts.xml配置文件


<?
xml version
=
“
1.0
“
encoding
=
“
UTF-8
“

?>


<!
DOCTYPE struts PUBLIC

“
-//Apache Software Foundation//DTD Struts Configuration 2.0//EN
“


“
http://struts.apache.org/dtds/struts-2.0.dtd
“
>


<
struts
>


<!–
该属性指定需要Struts2处理的请求后缀,该属性的默认值是action,即所有匹配
*
.action的请求都由Struts2处理。
如果用户需要指定多个请求后缀,则多个后缀之间以英文逗号(,)隔开。

–>


<
constant name
=
“
struts.action.extension
“
value
=
“
do
“

/>


<!–
设置浏览器是否缓存静态内容,默认值为true(生产环境下使用),开发阶段最好关闭
–>


<
constant name
=
“
struts.serve.static.browserCache
“
value
=
“
false
“

/>


<!–
当struts的配置文件修改后,系统是否自动重新加载该文件,默认值为false(生产环境下使用),开发阶段最好打开
–>


<
constant name
=
“
struts.configuration.xml.reload
“
value
=
“
true
“

/>


<!–
开发模式下使用,这样可以打印出更详细的错误信息
–>


<
constant name
=
“
struts.devMode
“
value
=
“
true
“

/>


<!–
默认的视图主题
–>


<
constant name
=
“
struts.ui.theme
“
value
=
“
simple
“

/>


<!–<
constant name
=
“
struts.objectFactory
“
value
=
“
spring
“

/>–>


<!–
解决乱码
–>


<
constant name
=
“
struts.i18n.encoding
“
value
=
“
UTF-8
“

/>


<
constant name
=
“
struts.multipart.maxSize
“
value
=
“
10701096
“
/>


<
package
name
=
“
upload
“
namespace
=
“
/upload
“

extends
=
“
struts-default
“
>


<
action name
=
“
*_upload
“

class
=
“
com.ljq.action.UploadAction
“
method
=
“
{1}
“
>


<
result name
=
“
success
“
>/
WEB
–
INF
/
page
/
message.jsp
</
result
>


</
action
>


</
package
>


<
package
name
=
“
upload1
“
namespace
=
“
/upload1
“

extends
=
“
struts-default
“
>


<
action name
=
“
upload1
“

class
=
“
com.ljq.action.UploadAction2
“
method
=
“
execute
“
>


<!–
要创建
/
image文件夹,否则会报找不到文件
–>


<
param name
=
“
savePath
“
>/
image
</
param
>


<
result name
=
“
success
“
>/
WEB
–
INF
/
page
/
message.jsp
</
result
>


</
action
>


</
package
>


<
package
name
=
“
upload2
“
namespace
=
“
/upload2
“

extends
=
“
struts-default
“
>


<
action name
=
“
upload2
“

class
=
“
com.ljq.action.UploadAction3
“
method
=
“
execute
“
>


<!–
要创建
/
image文件夹,否则会报找不到文件
–>


<
param name
=
“
savePath
“
>/
image
</
param
>


<
result name
=
“
success
“
>/
WEB
–
INF
/
page
/
message.jsp
</
result
>


</
action
>


</
package
>


</
struts
>

           

上传表单页面upload.jsp


<%
@ page language
=
“
java
“

import
=
“
java.util.*
“
pageEncoding
=
“
UTF-8
“
%>


<!
DOCTYPE HTML PUBLIC
“
-//W3C//DTD HTML 4.01 Transitional//EN
“
>


<
html
>


<
head
>


<
title
>
文件上传
</
title
>


<
meta http
–
equiv
=
“
pragma
“
content
=
“
no-cache
“
>


<
meta http
–
equiv
=
“
cache-control
“
content
=
“
no-cache
“
>


<
meta http
–
equiv
=
“
expires
“
content
=
“
0
“
>


</
head
>


<
body
>


<!–
${pageContext.request.contextPath}
/
upload
/
execute_upload.
do

–>


<!–
${pageContext.request.contextPath}
/
upload1
/
upload1.
do

–>


<!–
${pageContext.request.contextPath}
/
upload2
/
upload2.
do

–>


<!–

–>


<
form action
=
“
${pageContext.request.contextPath}/upload2/upload2.do
“
enctype
=
“
multipart/form-data
“
method
=
“
post
“
>

文件1:

<
input type
=
“
file
“
name
=
“
image
“
><
br
/>

文件2:

<
input type
=
“
file
“
name
=
“
image
“
><
br
/>

文件3:

<
input type
=
“
file
“
name
=
“
image
“
><
br
/>


<
input type
=
“
submit
“
value
=
“
上传
“

/>


</
form
>


</
body
>


</
html
>

显示页面message.jsp


<%
@ page language
=
“
java
“

import
=
“
java.util.*
“
pageEncoding
=
“
UTF-8
“
%>


<%
@ taglib uri
=
“
/struts-tags
“
prefix
=
“
s
“
%>


<!
DOCTYPE HTML PUBLIC
“
-//W3C//DTD HTML 4.01 Transitional//EN
“
>


<
html
>


<
head
>


<
title
>
My JSP
‘
message.jsp
‘
starting page
</
title
>


<
meta http
–
equiv
=
“
pragma
“
content
=
“
no-cache
“
>


<
meta http
–
equiv
=
“
cache-control
“
content
=
“
no-cache
“
>


<
meta http
–
equiv
=
“
expires
“
content
=
“
0
“
>


</
head
>


<
body
>

上传成功

<
br
/>


<
s:debug
></
s:debug
>


</
body
>


</
html
>

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/110641.html原文链接:https://javaforall.net

赞 (0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • Git现实(一个)版本控制概述

    Git现实(一个)版本控制概述

    2022年1月7日
  • createmutex函数参数含义_pthread_create函数

    createmutex函数参数含义_pthread_create函数CreateMutexCreateMutex函数的作用是找出当前系统是否已经存在指定进程的实例,如果没有则创建一个互斥体。//VC声明HANDLECreateMutex(LPSECURITY_ATTRIBUTESlpMutexAttributes,//指向安全属性的指针BOOLbInitialOwner,//初始化互斥对象的所有者LPCTSTRlpName//指向互斥对象名的指针);一个应用:HANDLEhMutex;hMutex=CreateMutex(

    2022年10月5日
  • 上帝掷骰子吗–量子物理史话

    上帝掷骰子吗–量子物理史话  上帝掷骰子吗–量子物理史话    第一章黄金时代    一    我们的故事要从1887年的德国开始。位于莱茵河边的卡尔斯鲁厄是一座风景秀丽的城市,在它的城中心,矗立着著名的18世纪的宫殿。郁郁葱葱的森林和温暖的气候也使得这座小城成为了欧洲的一个旅游名胜。然而这些怡人的景色似乎没有分散海因里希?鲁道夫?赫兹(HeinrichRudolfHertz)的注意力:现在他正

    2022年6月7日
  • RPN网络代码解读

    RPN网络代码解读1.说在前面的话在目标检测领域FasterRCNN可以说是无人不知无人不晓,它里面有一个网络结构RPN(RegionProposalNetwork)用于在特征图上产生候选预测区域。但是呢,这个网络结构具体是怎么工作的呢?网上有很多种解释,但是都是云里雾里的,还是直接撸代码来得直接,这里就直接从代码入手直接撸吧-_-||。首先,来看一下FasterRCNN中RPN的结构是什么样子的吧。…

    2022年6月23日
  • 关于红黑树的学习笔记

    关于红黑树的学习笔记

    2021年6月14日
  • UE4选不中半透明物体_半透明物体有哪些

    UE4选不中半透明物体_半透明物体有哪些UE4开发时,会出现透明材质的物体显示不全的情况本文给出几种解决方式供各位参考

    2022年10月5日

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号