欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Javaweb 多文件上传

程序员文章站 2022-05-08 17:02:13
...

FileUpload核心API介绍 的基础上 , 添加一个upload4.jsp的页面 .

服务器端的页面不需要修改 .

代码实现 :

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    
    <title>多文件上传</title>
    <script type="text/javascript">
    	
    function addFile(){                //添加上传文件的组件
    	var div=document.getElementById("content");
    	div.innerHTML+="<div><input type='file' name='f'><input type='button' value='remove file' onclick='removeFile(this)'></div>";
    }
    
    function removeFile(btn){
    	document.getElementById("content").removeChild(btn.parentNode);    //删除原来的组件
    }
    </script>
  </head>
  
  <body>
	<input type="button" value="add File" onclick="addFile();">
	<br>
	<br>
	<form action="${pageContext.request.contextPath}/upload4" method="post" encType="multipart/form-data">
		<input type="file" name="f"><br>
		<div id="content">
		</div>
		<input type="submit" value="上传">
	</form>
  </body>
</html>

    ${pageContext.request.contextPath}/upload4 提交到了upload3servlet的服务器文件.

   下面是 web.xml的文件.

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5">
  <display-name></display-name>
  <servlet>
    <servlet-name>Upload1Servlet</servlet-name>
    <servlet-class>cn.itcast.web.servlet.Upload1Servlet</servlet-class>
  </servlet>
  <servlet>
    <servlet-name>Upload2Servlet</servlet-name>
    <servlet-class>cn.itcast.web.servlet.Upload2Servlet</servlet-class>
  </servlet>
  <servlet>
    <servlet-name>Upload3Servlet</servlet-name>
    <servlet-class>cn.itcast.web.servlet.Upload3Servlet</servlet-class>
  </servlet>
  <servlet>
    <servlet-name>Upload4Servlet</servlet-name>
    <servlet-class>cn.itcast.web.servlet.Upload4Servlet</servlet-class>
  </servlet>

  <servlet-mapping>
    <servlet-name>Upload1Servlet</servlet-name>
    <url-pattern>/upload1</url-pattern>
  </servlet-mapping>
  <servlet-mapping>
    <servlet-name>Upload2Servlet</servlet-name>
    <url-pattern>/upload2</url-pattern>
  </servlet-mapping>
  <servlet-mapping>
    <servlet-name>Upload3Servlet</servlet-name>
    <url-pattern>/upload3</url-pattern>
  </servlet-mapping>
  <servlet-mapping>
    <servlet-name>Upload4Servlet</servlet-name>
    <url-pattern>/upload4</url-pattern>
  </servlet-mapping>
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
</web-app>

 

 

 

相关标签: 原创