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)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • Android 开机动画的制作「建议收藏」

    Android 开机动画的制作「建议收藏」最近项目需要对项目Android设备进行开机动画的替换,此项需求操作比较简单,但是也有一些细节需要注意,分享给有需求的极客们开机动画的制作、替换流程。

    2022年5月14日
    56
  • IDEA 2022 激活码_最新在线免费激活

    (IDEA 2022 激活码)最近有小伙伴私信我,问我这边有没有免费的intellijIdea的激活码,然后我将全栈君台教程分享给他了。激活成功之后他一直表示感谢,哈哈~IntelliJ2021最新激活注册码,破解教程可免费永久激活,亲测有效,下面是详细链接哦~https://javaforall.net/100143.html00…

    2022年3月30日
    160
  • Activity中setContentView过程

    Activity中setContentView过程Windows概念Android手机中所有的视图都是通过Window来呈现的,像常用的Activity,Dialog,PopupWindow,Toast,他们的视图都是附加在Window上的,所以可以这么说——Window是View的直接管理者。Activity中加载布局Activity中加载布局,都是通过在onCreate中调用setContentView方法开始:

    2022年6月26日
    27
  • 单源最短路径dijkstra算法_tous les jours蛋糕

    单源最短路径dijkstra算法_tous les jours蛋糕年轻的探险家来到了一个印第安部落里。在那里他和酋长的女儿相爱了,于是便向酋长去求亲。酋长要他用 10000 个金币作为聘礼才答应把女儿嫁给他。探险家拿不出这么多金币,便请求酋长降低要求。酋长说:”嗯,如果你能够替我弄到大祭司的皮袄,我可以只要 8000 金币。如果你能够弄来他的水晶球,那么只要 5000 金币就行了。”探险家就跑到大祭司那里,向他要求皮袄或水晶球,大祭司要他用金币来换,或者替他弄来其他的东西,他可以降低价格。探险家于是又跑到其他地方,其他人也提出了类似的要求,或者直接用金币换,或

    2022年8月10日
    8
  • django debug_django运行命令

    django debug_django运行命令介绍Django框架的调试工具栏使用django-debug-toolbar库,是一组可配置的面板,显示有关当前请求/响应的各种调试信息,点击时,显示有关面板内容的更多详细信息。应用1.安装

    2022年8月7日
    7
  • anaconda卸载方法

    anaconda卸载方法卸载anaconda

    2022年6月15日
    85

发表回复

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

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