`
xsuo
  • 浏览: 120085 次
  • 性别: Icon_minigender_1
  • 来自: 武汉
社区版块
存档分类
最新评论

《struts2权威指南》学习笔记之struts2文件上传

阅读更多

struts2没有提供自己的请求解析器,也就是说,struts2不会自己区处理multipart/form-data的请求,它需要调用其他请求解析器,将HTTP请求中的表单域解析出来,但struts2在原有的上传解析器上作了进一步封装,更进一步简化了文件上传

Struts2的struts.properties配置文件中,配置struts2的上传文件解析器

struts.multipart.parser=jakarta (srtuts2默认),也可以设置为常用的cos,pell等

配置上传页面:

<htmlxmlns="http://www.w3.org/1999/xhtml">
<head>
<metahttp-equiv="Content-Type"content="text/html;charset=GBK"/>
<title>简单的文件上传</title>
</head>
<body>
<formaction="upload.action"method="post"enctype="multipart/form-data">
文件标题:
<inputtype="text"name="title"/><br>
选择文件:
<inputtype="file"name="upload"/><br>
<inputvalue="上传"type="submit"/>
</form>
</body>
</html>

配置上传action

packagelee;

importcom.opensymphony.xwork2.Action;
importorg.apache.struts2.ServletActionContext;
importjava.io.File;
importjava.io.*;

importcom.opensymphony.xwork2.ActionSupport;



publicclassUploadActionextendsActionSupport
...{
privateStringtitle;
privateFileupload;
privateStringuploadContentType;
privateStringuploadFileName;

//接受依赖注入的属性
privateStringsavePath;
//接受依赖注入的方法
publicvoidsetSavePath(Stringvalue)
...{
this.savePath=value;
}


privateStringgetSavePath()throwsException
...{
returnServletActionContext.getRequest().getRealPath(savePath);
}


publicvoidsetTitle(Stringtitle)...{
this.title=title;
}


publicvoidsetUpload(Fileupload)...{
this.upload=upload;
}


publicvoidsetUploadContentType(StringuploadContentType)...{
this.uploadContentType=uploadContentType;
}


publicvoidsetUploadFileName(StringuploadFileName)...{
this.uploadFileName=uploadFileName;
}


publicStringgetTitle()...{
return(this.title);
}


publicFilegetUpload()...{
return(this.upload);
}


publicStringgetUploadContentType()...{
return(this.uploadContentType);
}


publicStringgetUploadFileName()...{
return(this.uploadFileName);
}

@Override
publicStringexecute()throwsException
...{
System.out.println(
"开始上传单个文件-----------------------");
System.out.println(getSavePath());
System.out.println(
"=========="+getUploadFileName());
System.out.println(
"=========="+getUploadContentType());
System.out.println(
"=========="+getUpload());
//以服务器的文件保存地址和原文件名建立上传文件输出流
FileOutputStreamfos=newFileOutputStream(getSavePath()+"\"+getUploadFileName());
FileInputStreamfis
=newFileInputStream(getUpload());
byte[]buffer=newbyte[1024];
intlen=0;
while((len=fis.read(buffer))>0)
...{
fos.write(buffer,
0,len);
}

returnSUCCESS;
}

}

struts配置文件

<?xmlversion="1.0"encoding="UTF-8"?>

<!DOCTYPEstrutsPUBLIC
"-//ApacheSoftwareFoundation//DTDStrutsConfiguration2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd"
>

<struts>

<constantname="struts.custom.i18n.resources"value="globalMessages"/>
<constantname="struts.i18n.encoding"value="GBK"/>

<packagename="lee"extends="struts-default">

<actionname="upload"class="lee.UploadAction">
<paramname="savePath">/upload</param>
<result>/succ.jsp</result>
</action>
</package>
</struts>

web.xml

<?xmlversion="1.0"encoding="UTF-8"?>
<web-appversion="2.5"
xmlns
="http://java.sun.com/xml/ns/javaee"
xmlns:xsi
="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation
="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
>
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>


<filter>
<filter-name>struts-cleanup</filter-name>
<filter-class>org.apache.struts2.dispatcher.ActionContextCleanUp</filter-class>
</filter>
<filter-mapping>
<filter-name>struts-cleanup</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>

这里web.xml多配置了一个ActionContextCleanUp的配置,这个类是一个filter,他的作用是方便strut2与sitemesh整合,与文件上传本没有关系,但不加载这个filter,可能在上传出出现莫名的异常,加入后就稳定了,可能是strut2的bug吧

文件上传工程页面:

<%...@pagelanguage="java"contentType="text/html;charset=GBK"%>
<%...@taglibprefix="s"uri="/struts-tags"%>
<html>
<head>
<title>上传成功</title>
</head>
<body>
上传成功!
<br>
文件标题:
<s:propertyvalue="+title"/><br>
文件为:
<imgsrc="<s:propertyvalue="'upload/'+uploadFileName"/>"/><br>
</body>
</html>

运行,选择一个jpg图片,提交,就可以看到上传的页面显示出来了

通常,我们希望限定上传文件的类型,比如说只能上传图片,还需要进一步改造

首先改造Action,增加一个属性和一个方法,属性表示允许上传的文件类型,方法进行验证,是否允许上传

packagelee;

importcom.opensymphony.xwork2.Action;
importcom.opensymphony.xwork2.ActionContext;

importorg.apache.struts2.ServletActionContext;
importjava.io.*;

importcom.opensymphony.xwork2.ActionSupport;

/***//**
*
@authoryeeku.H.leekongyeeku@163.com
*
@version1.0
*<br>Copyright(C),2005-2008,yeeku.H.Lee
*<br>Thisprogramisprotectedbycopyrightlaws.
*<br>ProgramName:
*<br>Date:
*/


publicclassUploadActionextendsActionSupport
...{
privateStringtitle;
privateFileupload;
privateStringuploadContentType;
privateStringuploadFileName;
privateStringallowTypes;

//接受依赖注入的属性
privateStringsavePath;
//接受依赖注入的方法
publicvoidsetSavePath(Stringvalue)
...{
this.savePath=value;
}


privateStringgetSavePath()throwsException
...{
returnServletActionContext.getRequest().getRealPath(savePath);
}


publicvoidsetTitle(Stringtitle)...{
this.title=title;
}


publicvoidsetUpload(Fileupload)...{
this.upload=upload;
}


publicvoidsetUploadContentType(StringuploadContentType)...{
this.uploadContentType=uploadContentType;
}


publicvoidsetUploadFileName(StringuploadFileName)border-right: #808080 1px solid; border-top: #808080 1px solid; display: none; border-left: #808080 1px solid; border-bottom: #808080 1px solid; b
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics