当前位置 主页 > 服务器问题 > Linux/apache问题 >
写一个Apache文件上传与文件下载的案例:以供今后学习
web.xml配置如下:
<span style="font-family:SimSun;font-size:14px;"><?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" 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>FileUploadAndDownload</display-name> <welcome-file-list> <welcome-file>index.html</welcome-file> <welcome-file>index.htm</welcome-file> <welcome-file>index.jsp</welcome-file> <welcome-file>default.html</welcome-file> <welcome-file>default.htm</welcome-file> <welcome-file>default.jsp</welcome-file> </welcome-file-list> <!-- 上传配置 --> <servlet> <servlet-name>uploadHandleServlet</servlet-name> <servlet-class>com.zeng.controller.UploadHandleServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>uploadHandleServlet</servlet-name> <url-pattern>/upload/uploadHandleServlet</url-pattern> </servlet-mapping> <servlet> <servlet-name>listFileServlet</servlet-name> <servlet-class>com.zeng.controller.ListFileServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>listFileServlet</servlet-name> <url-pattern>/listFileServlet</url-pattern> </servlet-mapping> <servlet> <servlet-name>downLoadServlet</servlet-name> <servlet-class>com.zeng.controller.DownLoadServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>downLoadServlet</servlet-name> <url-pattern>/download/downLoadServlet</url-pattern> </servlet-mapping> </web-app></span>
2.upload.jsp文件
<span style="font-family:SimSun;font-size:14px;"><%@page language="java" pageEncoding="UTF-8"%> <!DOCTYPE HTML> <html> <head> <title>文件上传</title> </head> <body> <!-- ${pageContext.request.contextPath} : 作用是取出部署的应用程序名 缺点:操作不便,其他工具无法正确解释${pageContext.request.contextPath} 如果Servlet的配置路径为 /upload/uploadHandleServlet action跳转路径为: ${pageContext.request.contextPath}/upload/uploadHandleServlet 上传文件时,必须 1.enctype="multipart/form-data" 2.method=post --> <form action="${pageContext.request.contextPath}/upload/uploadHandleServlet" enctype="multipart/form-data" method="post"> 上传用户:<input type="text" name="username"><br/> 上传文件1:<input type="file" name="file1"><br/> 上传文件2:<input type="file" name="file2"><br/> <input type="submit" value="提交"> </form> </body> </html></span>