1、Servlet實現文件上傳,須要添加第三方提供的jar包css
接着把這兩個jar包放到 lib文件夾下:html
二:java
文件上傳的表單提交方式必須是POST方式,web
編碼類型:enctype="multipart/form-data",默認是 application/x-www-form-urlencodedapache
好比:<form action="FileUpLoad"enctype="multipart/form-data"method="post">數組
3、舉例:緩存
1.fileupload.jspapp
- <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
- <%
- String path = request.getContextPath();
- String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
- %>
-
- <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
- <html>
- <head>
- <base href="<%=basePath%>">
-
- <title>My JSP 'fileupload.jsp' starting page</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="keyword1,keyword2,keyword3">
- <meta http-equiv="description" content="This is my page">
-
-
- </head>
-
- <body>
-
- <form action="FileUpLoad" enctype="multipart/form-data" method="post" >
-
- 用戶名:<input type="text" name="usename"> <br/>
- 上傳文件:<input type="file" name="file1"><br/>
- 上傳文件: <input type="file" name="file2"><br/>
- <input type="submit" value="提交"/>
-
- </form>
-
-
-
- </body>
- </html>
2.實際處理文件上傳的 FileUpLoad.javajsp
System.out.println("獲取上傳文件的總共的容量:"+item.getSize()); oop
3.filedemo.jsp
- <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
- <%
- String path = request.getContextPath();
- String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
- %>
-
- <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
- <html>
- <head>
- <base href="<%=basePath%>">
-
- <title>My JSP 'filedemo.jsp' starting page</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="keyword1,keyword2,keyword3">
- <meta http-equiv="description" content="This is my page">
-
-
- </head>
-
- <body>
-
- 用戶名:${requestScope.usename } <br/>
- 文件:${requestScope.file1 }<br/>
- ${requestScope.file2 }<br/>
-
- <img alt="go" src="upload/<%=(String)request.getAttribute("file1")%> " />
-
-
-
- </body>
- </html>
4結果頁面: