Struts下传多个及N个文件的例子

Struts上传多个及N个文件的例子

一。 web.xml文件

Xml代码 Struts下传多个及N个文件的例子  Struts下传多个及N个文件的例子Struts下传多个及N个文件的例子
  1. <? xml   version = "1.0"   encoding = "UTF-8" ?>   
  2. <!DOCTYPE web-app   
  3.   PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"   
  4.   "http://java.sun.com/dtd/web-app_2_3.dtd" >   
  5.     
  6. < web-app >   
  7.      < servlet >   
  8.          < servlet-name > actionServlet </ servlet-name >   
  9.          < servlet-class > org.apache.struts.action.ActionServlet </ servlet-class >   
  10.          < init-param >   
  11.               < param-name > config </ param-name >   
  12.               < param-value > /WEB-INF/struts-config.xml </ param-value >   
  13.          </ init-param >   
  14.          < init-param >   
  15.               < param-name > debug </ param-name >   
  16.               < param-value > 2 </ param-value >   
  17.          </ init-param >   
  18.          < load-on-startup > 0 </ load-on-startup >   
  19.      </ servlet >   
  20.      <!-- struts actionServlet mapping-->   
  21.      < servlet-mapping >   
  22.          < servlet-name > actionServlet </ servlet-name >     
  23.          < url-pattern > *.do </ url-pattern >   
  24.      </ servlet-mapping >   
  25.      <!-- welcome file list -->   
  26.      < welcome-file-list >   
  27.           < welcome-file > index.jsp </ welcome-file >   
  28.      </ welcome-file-list >   
  29.      <!-- error handle -->   
  30.      < error-page >   
  31.           < error-code > 404 </ error-code >   
  32.           < location > /error.jsp </ location >   
  33.      </ error-page >   
  34. </ web-app >   
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE web-app
  PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
  "http://java.sun.com/dtd/web-app_2_3.dtd">
 
<web-app>
    <servlet>
        <servlet-name>actionServlet</servlet-name>
        <servlet-class>org.apache.struts.action.ActionServlet</servlet-class>
        <init-param>
             <param-name>config</param-name>
             <param-value>/WEB-INF/struts-config.xml</param-value>
        </init-param>
        <init-param>
             <param-name>debug</param-name>
             <param-value>2</param-value>
        </init-param>
        <load-on-startup>0</load-on-startup>
    </servlet>
    <!-- struts actionServlet mapping-->
    <servlet-mapping>
        <servlet-name>actionServlet</servlet-name> 
        <url-pattern>*.do</url-pattern>
    </servlet-mapping>
    <!-- welcome file list -->
    <welcome-file-list>
         <welcome-file>index.jsp</welcome-file>
    </welcome-file-list>
    <!-- error handle -->
    <error-page>
         <error-code>404</error-code>
         <location>/error.jsp</location>
    </error-page>
</web-app>

 二。 struts-config.xml文件

Xml代码 Struts下传多个及N个文件的例子  Struts下传多个及N个文件的例子Struts下传多个及N个文件的例子
  1. <? xml   version = "1.0"   encoding = "ISO-8859-1"   ?>        
  2. <!DOCTYPE struts-config PUBLIC   
  3.           "-//Apache Software Foundation//DTD Struts Configuration 1.2//EN"   
  4.           "http://struts.apache.org/dtds/struts-config_1_2.dtd" >   
  5. < struts-config >   
  6.       <!-- form beans -->   
  7.       < form-beans >   
  8.           < form-bean   name = "uploadForm"   type = "org.apache.struts.action.DynaActionForm" >   
  9.              < form-property   name = "type"   type  = "java.lang.String[]" />   
  10.              < form-property   name = "name"   type  = "java.lang.String[]" />   
  11.                < form-property   name = "file0"   type  = "org.apache.struts.upload.FormFile" />   
  12.              < form-property   name = "file1"   type  = "org.apache.struts.upload.FormFile" />   
  13.              < form-property   name = "file2"   type  = "org.apache.struts.upload.FormFile" />   
  14.              < form-property   name = "file3"   type  = "org.apache.struts.upload.FormFile" />   
  15.              < form-property   name = "file4"   type  = "org.apache.struts.upload.FormFile" />   
  16.              < form-property   name = "file5"   type  = "org.apache.struts.upload.FormFile" />   
  17.              < form-property   name = "file6"   type  = "org.apache.struts.upload.FormFile" />   
  18.              < form-property   name = "file7"   type  = "org.apache.struts.upload.FormFile" />   
  19.              < form-property   name = "file8"   type  = "org.apache.struts.upload.FormFile" />   
  20.              < form-property   name = "file9"   type  = "org.apache.struts.upload.FormFile" />   
  21.           </ form-bean >   
  22.       </ form-beans >   
  23.       <!-- forward -->   
  24.       < global-forwards >   
  25.            < forward   name "successed"   path = "/index.jsp" > </ forward >        
  26.            < forward   name "failed"   path = "/error.jsp" > </ forward >        
  27.       </ global-forwards >   
  28.       <!-- action handle-->   
  29.       < action-mappings >   
  30.            < action   path = "/uploaded"   type = "com.fangchuang.action.UploadAction"     
  31.                    name = "uploadForm"   scope = "request"   input = "/upload.jsp" >   
  32.                  < forward   name = "uploaded"   path = "/upload.jsp" > </ forward >   
  33.            </ action >   
  34.       </ action-mappings >   
  35. </ struts-config >   
<?xml version="1.0" encoding="ISO-8859-1" ?>    
<!DOCTYPE struts-config PUBLIC
          "-//Apache Software Foundation//DTD Struts Configuration 1.2//EN"
          "http://struts.apache.org/dtds/struts-config_1_2.dtd">
<struts-config>
     <!-- form beans -->
     <form-beans>
         <form-bean name="uploadForm" type="org.apache.struts.action.DynaActionForm">
            <form-property name="type" type ="java.lang.String[]"/>
            <form-property name="name" type ="java.lang.String[]"/>
              <form-property name="file0" type ="org.apache.struts.upload.FormFile"/>
            <form-property name="file1" type ="org.apache.struts.upload.FormFile"/>
            <form-property name="file2" type ="org.apache.struts.upload.FormFile"/>
            <form-property name="file3" type ="org.apache.struts.upload.FormFile"/>
            <form-property name="file4" type ="org.apache.struts.upload.FormFile"/>
            <form-property name="file5" type ="org.apache.struts.upload.FormFile"/>
            <form-property name="file6" type ="org.apache.struts.upload.FormFile"/>
            <form-property name="file7" type ="org.apache.struts.upload.FormFile"/>
            <form-property name="file8" type ="org.apache.struts.upload.FormFile"/>
            <form-property name="file9" type ="org.apache.struts.upload.FormFile"/>
         </form-bean>
     </form-beans>
     <!-- forward -->
     <global-forwards>
          <forward name= "successed" path="/index.jsp"></forward>    
          <forward name= "failed" path="/error.jsp"></forward>    
     </global-forwards>
     <!-- action handle-->
     <action-mappings>
          <action path="/uploaded" type="com.fangchuang.action.UploadAction" 
                  name="uploadForm" scope="request" input="/upload.jsp">
                <forward name="uploaded" path="/upload.jsp"></forward>
          </action>
     </action-mappings>
</struts-config>

 三。 upload.jsp文件

Html代码 Struts下传多个及N个文件的例子  Struts下传多个及N个文件的例子Struts下传多个及N个文件的例子
  1. < %@ page  language = "java"   import = "java.util.*"   pageEncoding = "gb2312"   errorPage = "error.jsp" % >   
  2.   
  3. < %@ taglib  uri = "/WEB-INF/struts-html.tld"   prefix = "html" % >   
  4. < %@ taglib  uri = "/WEB-INF/struts-bean.tld"   prefix = "bean" % >   
  5. < %@ taglib  uri = "/WEB-INF/struts-logic.tld"   prefix = "logic" % >   
  6. < %@ taglib  uri = "/WEB-INF/struts-tiles.tld"   prefix = "title" % >   
  7.   
  8. <!-- 上传多个文件的jsp文件   
  9.      //可以随便转载,请保留出自http://www.javaresearch.org 作者anEngineer   
  10. -- >   
  11. < html >   
  12. < head >   
  13. < title > 上传多个文件的jsp文件 </ title >   
  14. < meta   http-equiv = "pragma"   content = "no-cache" >   
  15. < meta   http-equiv = "cache-control"   content = "no-cache" >   
  16. < meta   http-equiv = "expires"   content = "0" >   
  17. < meta   http-equiv = "keywords"   content = "struts,多个文件,上传" >   
  18. < meta   http-equiv = "description"   content = "This is a test" >   
  19. </ head >   
  20. < body >   
  21.   
  22. < html:form   action = "uploaded.do"   enctype = "multipart/form-data"   method = "post" >   
  23.      < table   border = "1"   width = "80%"   align = "center" >   
  24.          < tr   bgColor =#62bcff >   
  25.              < td   align = "left" > 文件类型 </ td >   
  26.              < td   align = "left" > 文件描述 </ td >   
  27.              < td   align = "left" > 文件名 </ td >   
  28.          </ tr >   
  29.   
  30.          < tr >   
  31.              < td   align = "left" > < select   name = "type" >   
  32.                  < option   value = "1"  selected > 湖南省 </ option >   
  33.                  < option   value = "2" > 湖北省 </ option >   
  34.                  < option   value = "3" > 广东省 </ option >   
  35.                  < option   value = "4" > 北京市 </ option >   
  36.                  < option   value = "5" > 上海市 </ option >   
  37.              </ select > </ td >   
  38.   
  39.              < td   align = "left" > < input   type = "text"   name = "name"   value = "" > </ td >   
  40.   
  41.              < td   align = "left" > < input   type = "file"   name = "file0"   size = "35"   value = "" >   
  42.              </ td >   
  43.          </ tr >   
  44.   
  45.   
  46.          < tr >   
  47.              < td   align = "left" > < select   name = "type" >   
  48.                  < option   value ="1" selected > 湖南省 </ option >   
  49.                  < option   value = "2" > 湖北省 </ option >   
  50.                  < option   value = "3" > 广东省 </ option >   
  51.                  < option   value = "4" > 北京市 </ option >   
  52.                  < option   value = "5" > 上海市 </ option >   
  53.              </ select > </ td >   
  54.   
  55.              < td   align = "left" > < input   type = "text"   name = "name"   value = "" > </ td >   
  56.   
  57.              < td   align = "left" > < input   type = "file"   name = "file1"   size = "35"   value = "" >   
  58.              </ td >   
  59.          </ tr >   
  60.          < tr >   
  61.              < td   align = "left" > < select   name = "type" >   
  62.                  < option   value = "1" > 湖南省 </ option >   
  63.                  < option   value ="2" selected > 湖北省 </ option >   
  64.                  < option   value = "3" > 广东省 </ option >   
  65.                  < option   value = "4" > 北京市 </ option >   
  66.                  < option   value = "5" > 上海市 </ option >   
  67.              </ select > </ td >   
  68.   
  69.              < td   align = "left" > < input   type = "text"   name = "name"   value = "" > </ td >   
  70.   
  71.              < td   align = "left" > < input   type = "file"   name = "file2"   size = "35"   value = "" >   
  72.              </ td >   
  73.          </ tr >   
  74.          < tr >   
  75.              < td   align = "left" > < select   name = "type" >   
  76.                  < option   value = "1" > 湖南省 </ option >   
  77.                  < option   value ="2" selected > 湖北省 </ option >   
  78.                  < option   value = "3" > 广东省 </ option >   
  79.                  < option   value = "4" > 北京市 </ option >   
  80.                  < option   value = "5" > 上海市 </ option >   
  81.              </ select > </ td >   
  82.   
  83.              < td   align = "left" > < input   type = "text"   name = "name"   value = "" > </ td >   
  84.   
  85.              < td   align = "left" > < input   type = "file"   name = "file3"   size = "35"   value = "" >   
  86.              </ td >   
  87.          </ tr >   
  88.          < tr >   
  89.              < td   align = "left" > < select   name = "type" >   
  90.                  < option   value = "1" > 湖南省 </ option >   
  91.                  < option   value = "2" > 湖北省 </ option >   
  92.                  < option   value ="3" selected > 广东省 </ option >   
  93.                  < option   value = "4" > 北京市 </ option >   
  94.                  < option   value = "5" > 上海市 </ option >   
  95.              </ select > </ td >   
  96.   
  97.              < td   align = "left" > < input   type = "text"   name = "name"   value = "" > </ td >   
  98.   
  99.              < td   align = "left" > < input   type = "file"   name = "file4"   size = "35"   value = "" >   
  100.              </ td >   
  101.          </ tr >   
  102.          < tr >   
  103.              < td   align = "left" > < select   name = "type" >   
  104.                  < option   value = "1" > 湖南省 </ option >   
  105.                  < option   value = "2" > 湖北省 </ option >   
  106.                  < option   value ="3" selected > 广东省 </ option >   
  107.                  < option   value = "4" > 北京市 </ option >   
  108.                  < option   value = "5" > 上海市 </ option >   
  109.              </ select > </ td >   
  110.   
  111.              < td   align = "left" > < input   type = "text"   name = "name"   value = "" > </ td >   
  112.   
  113.              < td   align = "left" > < input   type = "file"   name = "file5"   size = "35"   value = "" >   
  114.              </ td >   
  115.          </ tr >   
  116.          < tr >   
  117.              < td   align = "left" > < select   name = "type" >   
  118.                  < option   value = "1" > 湖南省 </ option >   
  119.                  < option   value = "2" > 湖北省 </ option >   
  120.                  < option   value = "3" > 广东省 </ option >   
  121.                  < option   value ="4" selected > 北京市 </ option >   
  122.                  < option   value = "5" > 上海市 </ option >   
  123.              </ select > </ td >   
  124.   
  125.              < td   align = "left" > < input   type = "text"   name = "name"   value = "" > </ td >   
  126.   
  127.              < td   align = "left" > < input   type = "file"   name = "file6"   size = "35"   value = "" >   
  128.              </ td >   
  129.          </ tr >   
  130.          < tr >   
  131.              < td   align = "left" > < select   name = "type" >   
  132.                  < option   value = "1" > 湖南省 </ option >   
  133.                  < option   value = "2" > 湖北省 </ option >   
  134.                  < option   value = "3" > 广东省 </ option >   
  135.                  < option   value ="4" selected > 北京市 </ option >   
  136.                  < option   value = "5" > 上海市 </ option >   
  137.              </ select > </ td >   
  138.   
  139.              < td   align = "left" > < input   type = "text"   name = "name"   value = "" > </ td >   
  140.   
  141.              < td   align = "left" > < input   type = "file"   name = "file7"   size = "35"   value = "" >   
  142.              </ td >   
  143.          </ tr >   
  144.          < tr >   
  145.              < td   align = "left" > < select   name = "type" >   
  146.                  < option   value = "1" > 湖南省 </ option >   
  147.                  < option   value = "2" > 湖北省 </ option >   
  148.                  < option   value = "3" > 广东省 </ option >   
  149.                  < option   value = "4" > 北京市 </ option >   
  150.                  < option   value ="5" selected > 上海市 </ option >   
  151.              </ select > </ td >   
  152.   
  153.              < td   align = "left" > < input   type = "text"   name = "name"   value = "" > </ td >   
  154.   
  155.              < td   align = "left" > < input   type = "file"   name = "file8"   size = "35"   value = "" >   
  156.              </ td >   
  157.          </ tr >   
  158.   
  159.   
  160.          < tr >   
  161.              < td   align = "left" > < select   name = "type" >   
  162.                  < option   value = "1" > 湖南省 </ option >   
  163.                  < option   value = "2" > 湖北省 </ option >   
  164.                  < option   value = "3" > 广东省 </ option >   
  165.                  < option   value = "4" > 北京市 </ option >   
  166.                  < option   value ="5" selected > 上海市 </ option >   
  167.              </ select > </ td >   
  168.   
  169.              < td   align = "left" > < input   type = "text"   name = "name"   value = "" > </ td >   
  170.   
  171.              < td   align = "left" > < input   type = "file"   name = "file9"   size = "35"   value = "" >   
  172.              </ td >   
  173.          </ tr >   
  174.   
  175.          < tr >   
  176.              < td   align = "left" >   </ td >   
  177.              < td   align = "left" >   </ td >   
  178.              < td   align = "left" > < input   type = "submit"   value = "上传文件"   value = "submit"   
  179.                  onclick = "return(confirm('你确认要上传文件吗?'))" > </ td >   
  180.          </ tr >   
  181.   
  182.      </ table >   
  183. </ html:form >   
  184.   
  185. </ body >   
  186. </ html >   
<%@ page language="java" import="java.util.*" pageEncoding="gb2312" errorPage="error.jsp"%>

<%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html"%>
<%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean"%>
<%@ taglib uri="/WEB-INF/struts-logic.tld" prefix="logic"%>
<%@ taglib uri="/WEB-INF/struts-tiles.tld" prefix="title"%>

<!-- 上传多个文件的jsp文件
     //可以随便转载,请保留出自http://www.javaresearch.org 作者anEngineer
-->
<html>
<head>
<title>上传多个文件的jsp文件</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="struts,多个文件,上传">
<meta http-equiv="description" content="This is a test">
</head>
<body>

<html:form action="uploaded.do" enctype="multipart/form-data" method="post">
    <table border="1" width="80%" align="center">
        <tr bgColor=#62bcff>
            <td align="left">文件类型</td>
            <td align="left">文件描述</td>
            <td align="left">文件名</td>
        </tr>

        <tr>
            <td align="left"><select name="type">
                <option value="1" selected>湖南省</option>
                <option value="2">湖北省</option>
                <option value="3">广东省</option>
                <option value="4">北京市</option>
                <option value="5">上海市</option>
            </select></td>

            <td align="left"><input type="text" name="name" value=""></td>

            <td align="left"><input type="file" name="file0" size="35" value="">
            </td>
        </tr>


        <tr>
            <td align="left"><select name="type">
                <option value="1"selected>湖南省</option>
                <option value="2">湖北省</option>
                <option value="3">广东省</option>
                <option value="4">北京市</option>
                <option value="5">上海市</option>
            </select></td>

            <td align="left"><input type="text" name="name" value=""></td>

            <td align="left"><input type="file" name="file1" size="35" value="">
            </td>
        </tr>
        <tr>
            <td align="left"><select name="type">
                <option value="1">湖南省</option>
                <option value="2"selected>湖北省</option>
                <option value="3">广东省</option>
                <option value="4">北京市</option>
                <option value="5">上海市</option>
            </select></td>

            <td align="left"><input type="text" name="name" value=""></td>

            <td align="left"><input type="file" name="file2" size="35" value="">
            </td>
        </tr>
        <tr>
            <td align="left"><select name="type">
                <option value="1">湖南省</option>
                <option value="2"selected>湖北省</option>
                <option value="3">广东省</option>
                <option value="4">北京市</option>
                <option value="5">上海市</option>
            </select></td>

            <td align="left"><input type="text" name="name" value=""></td>

            <td align="left"><input type="file" name="file3" size="35" value="">
            </td>
        </tr>
        <tr>
            <td align="left"><select name="type">
                <option value="1">湖南省</option>
                <option value="2">湖北省</option>
                <option value="3"selected>广东省</option>
                <option value="4">北京市</option>
                <option value="5">上海市</option>
            </select></td>

            <td align="left"><input type="text" name="name" value=""></td>

            <td align="left"><input type="file" name="file4" size="35" value="">
            </td>
        </tr>
        <tr>
            <td align="left"><select name="type">
                <option value="1">湖南省</option>
                <option value="2">湖北省</option>
                <option value="3"selected>广东省</option>
                <option value="4">北京市</option>
                <option value="5">上海市</option>
            </select></td>

            <td align="left"><input type="text" name="name" value=""></td>

            <td align="left"><input type="file" name="file5" size="35" value="">
            </td>
        </tr>
        <tr>
            <td align="left"><select name="type">
                <option value="1">湖南省</option>
                <option value="2">湖北省</option>
                <option value="3">广东省</option>
                <option value="4"selected>北京市</option>
                <option value="5">上海市</option>
            </select></td>

            <td align="left"><input type="text" name="name" value=""></td>

            <td align="left"><input type="file" name="file6" size="35" value="">
            </td>
        </tr>
        <tr>
            <td align="left"><select name="type">
                <option value="1">湖南省</option>
                <option value="2">湖北省</option>
                <option value="3">广东省</option>
                <option value="4"selected>北京市</option>
                <option value="5">上海市</option>
            </select></td>

            <td align="left"><input type="text" name="name" value=""></td>

            <td align="left"><input type="file" name="file7" size="35" value="">
            </td>
        </tr>
        <tr>
            <td align="left"><select name="type">
                <option value="1">湖南省</option>
                <option value="2">湖北省</option>
                <option value="3">广东省</option>
                <option value="4">北京市</option>
                <option value="5"selected>上海市</option>
            </select></td>

            <td align="left"><input type="text" name="name" value=""></td>

            <td align="left"><input type="file" name="file8" size="35" value="">
            </td>
        </tr>


        <tr>
            <td align="left"><select name="type">
                <option value="1">湖南省</option>
                <option value="2">湖北省</option>
                <option value="3">广东省</option>
                <option value="4">北京市</option>
                <option value="5"selected>上海市</option>
            </select></td>

            <td align="left"><input type="text" name="name" value=""></td>

            <td align="left"><input type="file" name="file9" size="35" value="">
            </td>
        </tr>

        <tr>
            <td align="left"> </td>
            <td align="left"> </td>
            <td align="left"><input type="submit" value="上传文件" value="submit"
                onclick="return(confirm('你确认要上传文件吗?'))"></td>
        </tr>

    </table>
</html:form>

</body>
</html>

 四。 还有两个简单jsp文件index.jsp,error.jsp可以自己写。

五。 action文件

Java代码 Struts下传多个及N个文件的例子  Struts下传多个及N个文件的例子Struts下传多个及N个文件的例子
  1. package  com.fangchuang.action;   
  2. /**  
  3.  * @author anEngineer 处理上传文件action  
  4.  */   
  5. public   class  UploadAction  extends  Action {   
  6.      public  ActionForward execute(   
  7.             ActionMapping mapping,    
  8.             ActionForm form,   
  9.             HttpServletRequest request,    
  10.             HttpServletResponse response) {   
  11.          //可以随便转载,请保留出自http://www.javaresearch.org 作者anEngineer   
  12.         DynaActionForm fileForm = (DynaActionForm) form;   
  13.         String[] types = (String[]) fileForm.get( "type" );   
  14.         String[] names = (String[]) fileForm.get( "name" );   
  15.   
  16.         FormFile file0 = (FormFile) fileForm.get( "file0" );   
  17.         FormFile file1 = (FormFile) fileForm.get( "file1" );   
  18.         FormFile file2 = (FormFile) fileForm.get( "file2" );   
  19.         FormFile file3 = (FormFile) fileForm.get( "file3" );   
  20.         FormFile file4 = (FormFile) fileForm.get( "file4" );   
  21.         FormFile file5 = (FormFile) fileForm.get( "file5" );   
  22.         FormFile file6 = (FormFile) fileForm.get( "file6" );   
  23.         FormFile file7 = (FormFile) fileForm.get( "file7" );   
  24.         FormFile file8 = (FormFile) fileForm.get( "file8" );   
  25.         FormFile file9 = (FormFile) fileForm.get( "file9" );   
  26.   
  27.         Map<String, FormFile> fileMap =  new  HashMap<String, FormFile>();   
  28.         fileMap.put( "file0"  +  "*"  + types[ 0 ] +  "*"  + names[ 0 ], file0);   
  29.         fileMap.put( "file1"  +  "*"  + types[ 1 ] +  "*"  + names[ 1 ], file1);   
  30.         fileMap.put( "file2"  +  "*"  + types[ 2 ] +  "*"  + names[ 2 ], file2);   
  31.         fileMap.put( "file3"  +  "*"  + types[ 3 ] +  "*"  + names[ 3 ], file3);   
  32.         fileMap.put( "file4"  +  "*"  + types[ 4 ] +  "*"  + names[ 4 ], file4);   
  33.         fileMap.put( "file5"  +  "*"  + types[ 5 ] +  "*"  + names[ 5 ], file5);   
  34.         fileMap.put( "file6"  +  "*"  + types[ 6 ] +  "*"  + names[ 6 ], file6);   
  35.         fileMap.put( "file7"  +  "*"  + types[ 7 ] +  "*"  + names[ 7 ], file7);   
  36.         fileMap.put( "file8"  +  "*"  + types[ 8 ] +  "*"  + names[ 8 ], file8);   
  37.         fileMap.put( "file9"  +  "*"  + types[ 9 ] +  "*"  + names[ 9 ], file9);   
  38.   
  39.         Set fileSet = fileMap.entrySet();   
  40.         Iterator iter = fileSet.iterator();   
  41.   
  42.          // 取当前系统路径E:\Tomcat5\webapps\strutsUpload\ 其中strutsUpload为当前context   
  43.         String filePath =  this .getServlet().getServletContext().getRealPath( "/" );   
  44.          // 保存文件的文件夹   
  45.         File savePath =  new  File(filePath +  "UploadFiles\\" );   
  46.         filePath = filePath+  "UploadFiles\\" ;   
  47.          if  (!savePath.exists()) {   
  48.             savePath.mkdir();   
  49.         }   
  50.          while  (iter.hasNext()) {   
  51.             Map.Entry unit = (Map.Entry) iter.next();   
  52.             String key = (String) unit.getKey();   
  53.             FormFile file = (FormFile) unit.getValue();       
  54.              //文件大小符合要求,且是图片文件   
  55.              if  ((file.getFileSize() >=  1 )&& DealPhoto.isPhoto(file)) {   
  56.                  //图片类别   
  57.                 String photoType = key.substring(key.indexOf( "*" ) +  1 , key   
  58.                         .lastIndexOf( "*" ));   
  59.                  //图片描述   
  60.                 String photoName = key.substring(key.lastIndexOf( "*" ) +  1 , key   
  61.                         .length());   
  62.                    
  63.                  //存数据库操作,在数据库中保存文件的名称,类型,及在服务器上的相对路径   
  64.                  //   
  65.                    
  66.                  //判断是否重名   
  67.                  if (DealPhoto.isFileExist(file.getFileName(),filePath))   
  68.                     DealPhoto.rename(file.getFileName(),filePath);   
  69.                  try  {   
  70.                  InputStream stream = file.getInputStream(); // 把文件读入   
  71.                   // 建立一个上传文件的输出流    
  72.                  OutputStream bos =  new  FileOutputStream(filePath+file.getFileName());   
  73.                   int  bytesRead =  0 ;   
  74.                   byte [] buffer =  new   byte [ 8192 ];   
  75.                   while  ((bytesRead = stream.read(buffer,  0 8192 )) != - 1 ) {   
  76.                   bos.write(buffer,  0 , bytesRead); // 将文件写入服务器   
  77.                  }   
  78.                  bos.close();   
  79.                  stream.close();   
  80.                 }  catch  (Exception e) {   
  81.                     e.printStackTrace();   
  82.                 }   
  83.             }   
  84.   
  85.         }   
  86.          return  mapping.findForward( "uploaded" );   
  87.          //还有其他可以改正的地方,如错误信息提示,把照片处理函数放到一个公用类,写文件操作等   
  88.          //可以随便转载,请保留出自http://www.javaresearch.org 作者anEngineer    
  89.     }   
  90. }  
package com.fangchuang.action;
/**
 * @author anEngineer 处理上传文件action
 */
public class UploadAction extends Action {
    public ActionForward execute(
            ActionMapping mapping, 
            ActionForm form,
            HttpServletRequest request, 
            HttpServletResponse response) {
        //可以随便转载,请保留出自http://www.javaresearch.org 作者anEngineer
        DynaActionForm fileForm = (DynaActionForm) form;
        String[] types = (String[]) fileForm.get("type");
        String[] names = (String[]) fileForm.get("name");

        FormFile file0 = (FormFile) fileForm.get("file0");
        FormFile file1 = (FormFile) fileForm.get("file1");
        FormFile file2 = (FormFile) fileForm.get("file2");
        FormFile file3 = (FormFile) fileForm.get("file3");
        FormFile file4 = (FormFile) fileForm.get("file4");
        FormFile file5 = (FormFile) fileForm.get("file5");
        FormFile file6 = (FormFile) fileForm.get("file6");
        FormFile file7 = (FormFile) fileForm.get("file7");
        FormFile file8 = (FormFile) fileForm.get("file8");
        FormFile file9 = (FormFile) fileForm.get("file9");

        Map<String, FormFile> fileMap = new HashMap<String, FormFile>();
        fileMap.put("file0" + "*" + types[0] + "*" + names[0], file0);
        fileMap.put("file1" + "*" + types[1] + "*" + names[1], file1);
        fileMap.put("file2" + "*" + types[2] + "*" + names[2], file2);
        fileMap.put("file3" + "*" + types[3] + "*" + names[3], file3);
        fileMap.put("file4" + "*" + types[4] + "*" + names[4], file4);
        fileMap.put("file5" + "*" + types[5] + "*" + names[5], file5);
        fileMap.put("file6" + "*" + types[6] + "*" + names[6], file6);
        fileMap.put("file7" + "*" + types[7] + "*" + names[7], file7);
        fileMap.put("file8" + "*" + types[8] + "*" + names[8], file8);
        fileMap.put("file9" + "*" + types[9] + "*" + names[9], file9);

        Set fileSet = fileMap.entrySet();
        Iterator iter = fileSet.iterator();

        // 取当前系统路径E:\Tomcat5\webapps\strutsUpload\ 其中strutsUpload为当前context
        String filePath = this.getServlet().getServletContext().getRealPath("/");
        // 保存文件的文件夹
        File savePath = new File(filePath + "UploadFiles\\");
        filePath = filePath+ "UploadFiles\\";
        if (!savePath.exists()) {
            savePath.mkdir();
        }
        while (iter.hasNext()) {
            Map.Entry unit = (Map.Entry) iter.next();
            String key = (String) unit.getKey();
            FormFile file = (FormFile) unit.getValue();    
            //文件大小符合要求,且是图片文件
            if ((file.getFileSize() >= 1)&& DealPhoto.isPhoto(file)) {
                //图片类别
                String photoType = key.substring(key.indexOf("*") + 1, key
                        .lastIndexOf("*"));
                //图片描述
                String photoName = key.substring(key.lastIndexOf("*") + 1, key
                        .length());
                
                //存数据库操作,在数据库中保存文件的名称,类型,及在服务器上的相对路径
                //
                
                //判断是否重名
                if(DealPhoto.isFileExist(file.getFileName(),filePath))
                    DealPhoto.rename(file.getFileName(),filePath);
                try {
                 InputStream stream = file.getInputStream();// 把文件读入
                 // 建立一个上传文件的输出流 
                 OutputStream bos = new FileOutputStream(filePath+file.getFileName());
                 int bytesRead = 0;
                 byte[] buffer = new byte[8192];
                 while ((bytesRead = stream.read(buffer, 0, 8192)) != -1) {
                  bos.write(buffer, 0, bytesRead);// 将文件写入服务器
                 }
                 bos.close();
                 stream.close();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }

        }
        return mapping.findForward("uploaded");
        //还有其他可以改正的地方,如错误信息提示,把照片处理函数放到一个公用类,写文件操作等
        //可以随便转载,请保留出自http://www.javaresearch.org 作者anEngineer 
    }
}

 六。 照片处理类文件

Java代码 Struts下传多个及N个文件的例子  Struts下传多个及N个文件的例子Struts下传多个及N个文件的例子
  1. package  com.fangchuang.run;   
  2. /**  
  3.  * 图片处理  
  4.  * @author anEngineer  
  5.  *  
  6.  */   
  7. public   class  DealPhoto {   
  8.      /**  
  9.      * 判断照片类型 .jpg .png .gif 目前只支持这三种格式  
  10.      * @param file  
  11.      * @return  
  12.      */   
  13.      public   static   boolean  isPhoto(FormFile file) {   
  14.         String fileName = getString(file.getFileName());   
  15.          if  (fileName.equals( "" ))   
  16.              return   false ;   
  17.          if  ((fileName.toLowerCase().endsWith( ".jpg" ))   
  18.                 || (fileName.toLowerCase().endsWith( ".gif" ))   
  19.                 || (fileName.toLowerCase().endsWith( ".png" )))   
  20.              return   true ;   
  21.          else   
  22.              return   false ;   
  23.     }   
  24.   
  25.      /**  
  26.      *   
  27.      * @param str  
  28.      * @return  
  29.      */   
  30.      public   static  String getString(String str) {   
  31.          if  (str ==  null )   
  32.             str =  "" ;   
  33.          if  (str.equals( "null" ))   
  34.             str =  "" ;   
  35.         str = str.trim();   
  36.          return  str;   
  37.     }   
  38.   
  39.      /**  
  40.      * 判断文件是否存在  
  41.      * @param fileName  
  42.      * @param dir  
  43.      * @return  
  44.      */   
  45.      public   static   boolean  isFileExist(String fileName, String dir) {   
  46.         File files =  new  File(dir + fileName);   
  47.          return  (files.exists()) ?  true  :  false ;   
  48.     }   
  49.   
  50.      /**  
  51.      * 重命名  
  52.      * @param fileName  
  53.      * @param dir  
  54.      */   
  55.      public   static   void  rename(String fileName, String dir) {   
  56.         String extendFile =  "" ;   
  57.          if  (isJpg(fileName))   
  58.             extendFile =  ".jpg" ;   
  59.          else   if  (isGif(fileName))   
  60.             extendFile =  ".gif" ;   
  61.          else   if  (isPng(fileName))   
  62.             extendFile =  ".png" ;   
  63.          else   
  64.             extendFile =  ".jpg" ;   
  65.         Random random =  new  Random();   
  66.          int  add = random.nextInt( 10000 );   
  67.         String ret = fileName + add + extendFile;   
  68.          while  (isFileExist(ret, dir)) {   
  69.             add = random.nextInt( 10000 );   
  70.             ret = fileName + add + extendFile;   
  71.         }   
  72.         File file =  new  File(dir + fileName);   
  73.         File reFile =  new  File(dir + ret);   
  74.         file.renameTo(reFile);   
  75.     }   
  76.   
  77.      public   static   boolean  isGif(String file) {   
  78.          if  (file.toLowerCase().endsWith( ".gif" )) {   
  79.              return   true ;   
  80.         }  else  {   
  81.              return   false ;   
  82.         }   
  83.     }   
  84.   
  85.      public   static   boolean  isJpg(String file) {   
  86.          if  (file.toLowerCase().endsWith( ".jpg" )) {   
  87.              return   true ;   
  88.         }  else  {   
  89.              return   false ;   
  90.         }   
  91.     }   
  92.   
  93.      public   static   boolean  isPng(String file) {   
  94.          if  (file.toLowerCase().endsWith( ".png" )) {   
  95.              return   true ;   
  96.         }  else  {   
  97.              return   false ;   
  98.         }   
  99.     }