`
bcyy
  • 浏览: 1823398 次
文章分类
社区版块
存档分类
最新评论

Struts2中使用Common-FileUpload实现文件上传(二)

 
阅读更多

web应用中,文件上传似乎是很常见的,但是采用传统的方法不但复杂而且难以控制,需要写很多代码,像控制文件大小、文件类型的过滤、存放目录等等。这些复杂的问题在Struts2中已经不存在了,struts2默认使用common-fileupload实现文件的上传。在struts.properties中我们可以看到:struts.multipart.parser=Jakarta。下面我们就以Common-FileUpload来实现文件上传。

首先,把commons-fileupload.jarcommons-io.jar拷贝到classpath路径下。

建立一个上传文件的页面,upload.jsp

<%@pagecontentType="text/html;charset=utf-8"%>
<%@taglibprefix="s"uri="/struts-tags"%>
<!DOCTYPEhtmlPUBLIC"-//W3C//DTDXHTML1.0Transitional//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<htmlxmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Struts2FileUpload</title>
</head>
<body>
<divstyle="color.red">
<s:fielderror/>
</div>
<s:formaction="upload"method="post"enctype="multipart/form-data">
<s:textfieldname="title"label="文件标题"/>
<s:filename="upload"label="选择文件"/>
<s:submitvalue="上传"/>
</s:form>
</body>
</html>


上传成功后的succ.jsp

<%@pagecontentType="text/html;charset=utf-8"%>
<%@taglibprefix="s"uri="/struts-tags"%>
<!DOCTYPEhtmlPUBLIC"-//W3C//DTDXHTML1.0Transitional//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<htmlxmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Struts2FileUpload</title>
</head>
<body>
<divstyle="padding:3px;border:solid1px#cccccc;text-align:center">
<imgsrc='upload/<s:propertyvalue="uploadFileName"/>'/>
<br/>
<s:propertyvalue="title"/>
</div>
</body>
</html>


然后编写UploadAction.java

/**
*
*
@author<ahref="mailto:flustar2008@163.com">flustar</a>
*
@version1.0
*Creationdate:Feb15,200810:24:36PM
*/

packagetest;

importjava.io.File;
importjava.io.FileInputStream;
importjava.io.FileOutputStream;

importorg.apache.struts2.ServletActionContext;

importcom.opensymphony.xwork2.ActionSupport;

publicclassUploadActionextendsActionSupport{
privatestaticfinallongserialVersionUID=-7887613751080170362L;
privateStringtitle;//设置上传文件的标题
privateFileupload;//封装上传文件
privateStringuploadFileName;//设置上传文件的文件名
privateStringuploadContentType;//上传文件的类型
privateStringsavePath;//上传文件的保存路径
publicStringgetTitle(){
returntitle;
}

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

publicFilegetUpload(){
returnupload;
}

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


publicStringgetUploadFileName(){
returnuploadFileName;
}

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

publicStringgetUploadContentType(){
returnuploadContentType;
}

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

publicStringgetSavePath(){
System.out.println(ServletActionContext.getServletContext().getRealPath(savePath));
returnServletActionContext.getServletContext().getRealPath(savePath);
}

publicvoidsetSavePath(StringsavePath){
this.savePath=savePath;
System.out.println(
"savePath:"+savePath);
}

publicStringexecute()throwsException{
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.xml

<?xmlversion="1.0"encoding="UTF-8"?>
<!DOCTYPEstrutsPUBLIC
"-//ApacheSoftwareFoundation//DTDStrutsConfiguration2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd"
>
<struts>
<packagename="blog"extends="struts-default">

<actionname="upload"class="test.UploadAction">
<!--Struts2内置的文件上传拦截器-->
<interceptor-refname="fileUpload">
<paramname="allowedTypes">image/bmp,image/x-png,image/gif,image/pjpeg</param>
<paramname="maximumSize">2048000</param>
</interceptor-ref>
<interceptor-refname="defaultStack"/>
<paramname="savePath">/upload</param>
<result>/succ.jsp</result>
<resultname="input">/upload.jsp</result>
</action>

</package>

</struts>


编写struts.properties

struts.custom.i18n.resources=mess

struts.multipart.parser
=jakarta

struts.multipart.maxSize
=10000000

编写国际化的资源文件mess.properties

struts.messages.error.content.type.not.allowed="Thetypeisnotbeallowed!"

struts.messages.error.file.too.large
="Thefileistoolarge!"

struts.messages.error.uploading
="unknownerror"

在这里我没有把调试的过程写出来,这完全没必要,网上已经有很多这方面的例子了,但是网上好多例子都有一个通病,那就是错误的信息实在是太多了,都是搜索引擎惹得祸。按照上面的步骤来做,很难成功!我按照上面的步骤来操作,发现两个比较普遍的错误:

1)不能上传pngjpg类型的图片。解决办法可以参考上面的struts.xml

2)上传过大的文件没有提示信息,而是直接抛出下面的异常:

org.apache.commons.fileupload.FileUploadBase$SizeLimitExceededException: the request was rejected because its size (2359629) exceeds the configured maximum (2097152)

为什么会出现这个错误?刚开始我还以为是Struts2的一个bug,于是我就开始研究是struts2内置的fileUpload拦截器,我研究了FileUploadInterceptor.java的源代码并调试了半天依然不能解决这个问题,我都被它快折磨死了。最后我想起了在我们编写struts.properties中有这么一句struts.multipart.parser=Jakarta,实际上这一句也可以不写因为这是struts2为了支持文件上传默认的。那么这个Jakarta到底是什么东西呢?实际上Jakarta实际上就是org.apache.struts2.dispatcher.multipart.JakartaMultiPartRequest类。打开JakartaMultiPartRequest.java的源代码你会发现这个类实现了MultiPartRequest接口,在这个类封装了对底层ServletFileUpload的操作,由于common-fileupload组件默认最大支持上传文件的大小为2M,当我们上传大于2M的文件时,就会出现上面的异常。是这个异常的发生导致了fileUpload拦截器没有机会执行,所以我看到的是页面没有任何变化,也没有任何提示信息,只是在控制台打印出了上面的异常。解决的办法在struts.properties文件中把struts.multipart.maxSize设置成一个比较大的值,也是就说maxSize远远要大于可能上传文件的大小和fileUpload拦截器中maxinumSize的值,可参见上面的struts.xmlstruts.properties文件。

转自:http://www.blogjava.net/flustar/archive/2008/02/16/fileupload.html

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics