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

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

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

开发步骤如下:

1、新建一个web工程,导入struts2上传文件所需jar,如下图

目录结构

            

2、新建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 { String realpath
=
ServletActionContext.getServletContext().getRealPath(
"
/images
"
);
//
D:\apache-tomcat-6.0.18\webapps\struts2_upload\images
System.out.println(
"
realpath:
"
+
realpath);
if
(image
!=
null
) { File savefile
=
new
File(
new
File(realpath), imageFileName);
if
(
!
savefile.getParentFile().exists()) savefile.getParentFile().mkdirs(); FileUtils.copyFile(image, savefile); ActionContext.getContext().put(
"
message
"
,
"
文件上传成功
"
); }
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; } }

            

第二种方式

package    
com.ljq.action;
import
java.io.File;
import
java.io.FileInputStream;
import
java.io.FileOutputStream;
import
java.io.IOException;
import
org.apache.struts2.ServletActionContext;
import
com.opensymphony.xwork2.ActionSupport;@SuppressWarnings(
"
serial
"
)
public
class
UploadAction2
extends
ActionSupport {
//
封装上传文件域的属性
private
File image;
//
封装上传文件类型的属性
private
String imageContentType;
//
封装上传文件名的属性
private
String imageFileName;
//
接受依赖注入的属性
private
String savePath; @Override
public
String execute() { FileOutputStream fos
=
null
; FileInputStream fis
=
null
;
try
{
//
建立文件输出流
System.out.println(getSavePath()); fos
=
new
FileOutputStream(getSavePath()
+
"
\\
"
+
getImageFileName());
//
建立文件上传流
fis
=
new
FileInputStream(getImage());
byte
[] buffer
=
new
byte
[
1024
];
int
len
=
0
;
while
((len
=
fis.read(buffer))
>
0
) { fos.write(buffer,
0
, len); } }
catch
(Exception e) { System.out.println(
"
文件上传失败
"
); e.printStackTrace(); }
finally
{ close(fos, fis); }
return
SUCCESS; }
/**
* 返回上传文件的保存位置 * *
@return
*/
public
String getSavePath()
throws
Exception{
return
ServletActionContext.getServletContext().getRealPath(savePath); }
public
void
setSavePath(String savePath) {
this
.savePath
=
savePath; }
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; }
private
void
close(FileOutputStream fos, FileInputStream fis) {
if
(fis
!=
null
) {
try
{ fis.close(); }
catch
(IOException e) { System.out.println(
"
FileInputStream关闭失败
"
); e.printStackTrace(); } }
if
(fos
!=
null
) {
try
{ fos.close(); }
catch
(IOException e) { System.out.println(
"
FileOutputStream关闭失败
"
); e.printStackTrace(); } } }}

              

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
"
/>
<
constant name
=
"
struts.multipart.saveDir
"
value
=
"
d:/tmp
"
/>
<
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
=
"
upload2
"
extends
=
"
struts-default
"
>
<
action name
=
"
upload2
"
class
=
"
com.ljq.action.UploadAction2
"
method
=
"
execute
"
>
<
param name
=
"
savePath
"
>/
images
<
result name
=
"
success
"
>/
WEB
-
INF
/
page
/
message.jsp
<
result name
=
"
input
"
>/
upload
/
upload.jsp
<
interceptor
-
ref name
=
"
fileUpload
"
>
<
param name
=
"
allowedTypes
"
>
image
/
bmp,image
/
png,image
/
gif,image
/
jpeg
<
param name
=
"
maximumSize
"
>
1025956
<
interceptor
-
ref name
=
"
defaultStack
"
/>

           

上传表单页面

<%    
@ page language
=
"
java
"
import
=
"
java.util.*
"
pageEncoding
=
"
UTF-8
"
%>
<%
@taglib uri
=
"
/struts-tags
"
prefix
=
"
s
"
%>
<
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
"
>
文件:
<
input type
=
"
file
"
name
=
"
image
"
>
<
input type
=
"
submit
"
value
=
"
上传
"
/>
<
br
/>
<
s:fielderror
/>

           

显示结果页面

<%    
@ page language
=
"
java
"
import
=
"
java.util.*
"
pageEncoding
=
"
UTF-8
"
%>
<%
@ taglib uri
=
"
/struts-tags
"
prefix
=
"
s
"
%>
<
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
>
上传成功!
<
br
/><
br
/>
<
img src
=
"
${pageContext.request.contextPath}/
"
>
<
s:debug
>

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

你可能感兴趣的文章
在线视频聊天(客服)系统开发那点事儿
查看>>
SecurityError Error 2148 SWF 不能访问本地资源
查看>>
Flex4的可视化显示对象
查看>>
Flex:自定义滚动条样式/隐藏上下箭头
查看>>
烈焰SWF解密
查看>>
Qt 静态编译后的exe太大, 可以这样压缩.
查看>>
3D游戏常用技巧Normal Mapping (法线贴图)原理解析——基础篇
查看>>
乘法逆元
查看>>
STL源码分析----神奇的 list 的 sort 算法实现
查看>>
Linux下用math.h头文件
查看>>
Linux中用st_mode判断文件类型
查看>>
Ubuntu修改host遇到unable to resolve host
查看>>
路由选择算法
查看>>
Objective-C 基础入门(一)
查看>>
Objective-C 基础入门(三) 读写文件与回调
查看>>
C++ STL标准库与泛型编程(一)概述
查看>>
C++ STL标准库与泛型编程(四)Deque、Queue、Stack 深度探索
查看>>
C++ STL标准库 算法
查看>>
JVM内存模型_Minor GC笔记
查看>>
SpringCloud学习之PassCloud——(一)PassCloud源代码下载
查看>>