博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
struts2之多个文件上传
阅读量:4079 次
发布时间:2019-05-25

本文共 15249 字,大约阅读时间需要 50 分钟。

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

         

目录结构

 

新建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;
/**
* 使用数组上传多个文件 *
*
*/
@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; }
/**
* 返回上传文件保存的位置
*/
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上传多个文件
*
*/
@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; }
/**
* 返回上传文件保存的位置
*/
public
String getSavePath()
throws
Exception {
return
ServletActionContext.getServletContext().getRealPath(savePath); }
public
void
setSavePath(String savePath) {
this
.savePath
=
savePath; }}

              

struts.xml配置文件

<
struts
>
<
constant name
=
"
struts.action.extension
"
value
=
"
do
"
/>
<
constant name
=
"
struts.serve.static.browserCache
"
value
=
"
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.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
<
package
name
=
"
upload1
"
namespace
=
"
/upload1
"
extends
=
"
struts-default
"
>
<
action name
=
"
upload1
"
class
=
"
com.ljq.action.UploadAction2
"
method
=
"
execute
"
>
<
param name
=
"
savePath
"
>/
image
<
result name
=
"
success
"
>/
WEB
-
INF
/
page
/
message.jsp
<
package
name
=
"
upload2
"
namespace
=
"
/upload2
"
extends
=
"
struts-default
"
>
<
action name
=
"
upload2
"
class
=
"
com.ljq.action.UploadAction3
"
method
=
"
execute
"
>
<
param name
=
"
savePath
"
>/
image
<
result name
=
"
success
"
>/
WEB
-
INF
/
page
/
message.jsp

           

上传表单页面upload.jsp

<%   
@ page language
=
"
java
"
import
=
"
java.util.*
"
pageEncoding
=
"
UTF-8
"
%>
<
html
>
<
head
>
<
title
>
文件上传
<
meta http
-
equiv
=
"
pragma
"
content
=
"
no-cache
"
>
<
meta http
-
equiv
=
"
cache-control
"
content
=
"
no-cache
"
>
<
meta http
-
equiv
=
"
expires
"
content
=
"
0
"
>
<
body
>
<
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
=
"
上传
"
/>

显示页面message.jsp

<%   
@ page language
=
"
java
"
import
=
"
java.util.*
"
pageEncoding
=
"
UTF-8
"
%>
<%
@ taglib uri
=
"
/struts-tags
"
prefix
=
"
s
"
%>
<
html
>
<
head
>
<
title
>
My JSP
'
message.jsp
'
starting page
<
meta http
-
equiv
=
"
pragma
"
content
=
"
no-cache
"
>
<
meta http
-
equiv
=
"
cache-control
"
content
=
"
no-cache
"
>
<
meta http
-
equiv
=
"
expires
"
content
=
"
0
"
>
<
body
>
上传成功
<
br
/>
<
s:debug
>

转载地址:http://ednni.baihongyu.com/

你可能感兴趣的文章
找工作准备的方向(4月22日写的)
查看>>
关于fwrite写入文件后打开查看是乱码的问题
查看>>
用结构体指针前必须要用malloc,不然会出现段错误
查看>>
Linux系统中的美
查看>>
一些实战项目(linux应用层编程,多线程编程,网络编程)
查看>>
我觉得专注于去学东西就好了,与世无争。
查看>>
原来k8s docker是用go语言写的,和现在所讲的go是一个东西!
查看>>
进程的创建分为两步,先fork(),再exec()
查看>>
可折叠机架
查看>>
不要用XXD电调
查看>>
弄底层基础的东西往往慢,枯燥,要慢慢磨
查看>>
使用STM32Cube可以直接生成使用FreeRTOS的工程
查看>>
STM32CubeMX 真的不要太好用
查看>>
STM32CubeMX介绍、下载与安装
查看>>
感觉也可以自己做公众号,写一些好点的文章,也和讲课类似,输出倒逼输入
查看>>
STM32CubeMX使用方法及功能介绍
查看>>
pixhawk固件的安装
查看>>
电机和桨叶要搭配选择
查看>>
STM32cube我还是喜欢这种视频教程
查看>>
弄ROS可以不用装双系统或者买一台电脑啥的, 可以直接U盘启动
查看>>