您查看的文章来源于http://www.oklinux.cn
最近一个基于Struts的项目中要用到上传多张图片到系统中,我在网上找了一下,发现大多数都是转载的例子,对项目的作用不大,故写下了这个例子,希望对有用到的朋友有所帮助。要求JDK1.5以上,lib库要包含如下jar文件:antlr.jar,commons-beanutils.jar,commons-collections.jar,commons-digester.jar,commons-fileupload.jar,commons-logging.jar,commons-validator.jar,jakarta-oro.jar,struts.jar这些都是Struts中的jar文件以及Struts中的标签库文件。
//可以转载,请保留出自http://www.javaresearch.org,作者anEngineer
一。web.xml文件
< ?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 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文件
< %@ 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>