在Struts 2中实现文件上传(2)

来源:互联网 发布:儿童舞蹈包淘宝 编辑:程序博客网 时间:2024/06/07 08:21
在Struts 2中实现文件上传(2)
来源:chinaitlab [2007-04-18] 作者:admin 编辑:admin

下面我们就来看看上传成功的页面:
<% @ page language = " java " contentType = " text/html; charset=utf-8 " pageEncoding = " utf-8 " %> 
<% @ taglib prefix = " s " uri = " /struts-tags " %> 
<! DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd" > 
< html xmlns ="http://www.w3.org/1999/xhtml" > 
< head > 
    < title > Struts 2 File Upload </ title > 
</ head > 
< body > 
    < div style ="padding: 3px; border: solid 1px #cccccc; text-align: center" > 
        < img src ='UploadImages/<s:property value ="imageFileName" /> ' />
        < br /> 
        < s:property value ="caption" /> 
    </ div > 
</ body > 
</ html > 清单4 ShowUpload.jsp 
ShowUpload.jsp获得imageFileName,将其UploadImages组成URL,从而将上传的像显示出来。
然后是Action的配置文件:
<? 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 > 
    < package name ="fileUploadDemo" extends ="struts-default" > 
        < action name ="fileUpload" class ="tutorial.FileUploadAction" > 
            < interceptor-ref name ="fileUploadStack" /> 
            < result name ="success" > /ShowUpload.jsp </ result > 
        </ action > 
    </ package > 
</ struts > 
fileUpload Action显式地应用fileUploadStack的拦截器。
最后是web.xml配置文件:
<? xml version="1.0" encoding="UTF-8" ?> 
< web-app id ="WebApp_9" version ="2.4" 
    xmlns ="http://java.sun.com/xml/ns/j2ee" 
    xmlns:xsi ="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation ="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd" > 
    < display-name > Struts 2 Fileupload </ display-name > 
    < filter > 
        < filter-name > struts-cleanup </ filter-name > 
        < filter-class > 
            org.apache.struts2.dispatcher.ActionContextCleanUp
        </ filter-class > 
    </ filter > 
    
    < filter > 
        < filter-name > struts2 </ filter-name > 
        < filter-class > 
            org.apache.struts2.dispatcher.FilterDispatcher
        </ filter-class > 
    </ filter > 
    
    < filter-mapping > 
        < filter-name > struts-cleanup </ filter-name > 
        < url-pattern > /* </ url-pattern > 
    </ filter-mapping > 
    < filter-mapping > 
        < filter-name > struts2 </ filter-name > 
        < url-pattern > /* </ url-pattern > 
    </ filter-mapping > 
    < welcome-file-list > 
        < welcome-file > index.html </ welcome-file > 
    </ welcome-file-list > 
</ web-app > 
更多配置
运行上述例子,如果您留心一点的话,应该会发现服务器控制台有如下输出:
Mar 20 , 2007 4 : 08 : 43 PM org.apache.struts2.dispatcher.Dispatcher getSaveDir
INFO: Unable to find 'struts.multipart.saveDir' property setting. Defaulting to javax.servlet.context.tempdir
Mar 20 , 2007 4 : 08 : 43 PM org.apache.struts2.interceptor.FileUploadInterceptor intercept
INFO: Removing file myFile C:/Program Files/Tomcat 5.5 /work/Catalina/localhost/Struts2_Fileupload/upload_251447c2_1116e355841__7ff7_00000006.tmp 清单9 服务器控制台输出 
上述信息告诉我们,struts.multipart.saveDir没有配置。struts.multipart.saveDir用于指定存放临时文件的文件夹,该配置写在struts.properties文件中。例如,如果在struts.properties文件加入如下代码:
struts.multipart.saveDir = /tmp 清单10 struts配置 
这样上传的文件就会临时保存到你根目录下的tmp文件夹中(一般为c:/tmp),如果此文件夹不存在,Struts 2会自动创建一个。

百事通

错误处理
上述例子实现的图片上传的功能,所以应该阻止用户上传非图片类型的文件。在Struts 2中如何实现这点呢?其实这也很简单,对上述例子作如下修改即可。
首先修改FileUpload.jsp,在<body>与<s:form>之间加入“<s:fielderror />”,用于在页面上输出错误信息。
然后修改struts.xml文件,将Action fileUpload的定义改为如下所示:
        < action name ="fileUpload" class ="tutorial.FileUploadAction" > 
            < interceptor-ref name ="fileUpload" > 
                < param name ="allowedTypes" > 
                    image/bmp,image/png,image/gif,image/jpeg
                </ param > 
            </ interceptor-ref > 
            < interceptor-ref name ="defaultStack" />            
            < result name ="input" > /FileUpload.jsp </ result > 
            < result name ="success" > /ShowUpload.jsp </ result > 
        </ action > 清单11 修改后的配置文件 
显而易见,起作用就是fileUpload拦截器的allowTypes参数。另外,配置还引入defaultStack它会帮我们添加验证等功能,所以在出错之后会跳转到名称为“input”的结果,也即是FileUpload.jsp。
多文件上传
与单文件上传相似,Struts 2实现多文件上传也很简单。你可以将多个<s:file />绑定Action的数组或列表。如下例所示。
< s:form action ="doMultipleUploadUsingList" method ="POST" enctype ="multipart/form-data" > 
    < s:file label ="File (1)" name ="upload" /> 
    < s:file label ="File (2)" name ="upload" /> 
    < s:file label ="FIle (3)" name ="upload" /> 
    < s:submit /> 
</ s:form > 清单14 多文件上传JSP代码片段 
如果你希望绑定到数组,Action的代码应类似:
     private File[] uploads;
     private String[] uploadFileNames;
     private String[] uploadContentTypes;
     public File[] getUpload()  { return this .uploads; } 
      public void setUpload(File[] upload)  { this .uploads = upload; } 
 
      public String[] getUploadFileName()  { return this .uploadFileNames; } 
      public void setUploadFileName(String[] uploadFileName)  { this .uploadFileNames = uploadFileName; } 
 
      public String[] getUploadContentType()  { return this .uploadContentTypes; } 
      public void setUploadContentType(String[] uploadContentType)  { this .uploadContentTypes = uploadContentType; } 清单15 多文件上传数组绑定Action代码片段 
如果你想绑定到列表,则应类似:
     private List < File > uploads = new ArrayList < File > ();
     private List < String > uploadFileNames = new ArrayList < String > ();
     private List < String > uploadContentTypes = new ArrayList < String > ();
     public List < File > getUpload()  {
         return this .uploads;
    } 
      public void setUpload(List < File > uploads)  {
         this .uploads = uploads;
    } 
 
      public List < String > getUploadFileName()  {
         return this .uploadFileNames;
    } 
      public void setUploadFileName(List < String > uploadFileNames)  {
         this .uploadFileNames = uploadFileNames;
    } 
 
      public List < String > getUploadContentType()  {
         return this .uploadContentTypes;
    } 
      public void setUploadContentType(List < String > contentTypes)  {
         this .uploadContentTypes = contentTypes;
    } 
原创粉丝点击