//處理上傳頁面(如:result.jsp)
1 <%@ page language="java" pageEncoding="gbk" contentType="text/html; charset=gbk"%> 2 <%@ page import="java.util.*"%> 3 <%@ page import="java.io.*"%> 4 <%@ page import="com.oreilly.servlet.multipart.*"%> 5 <%@ page import="com.oreilly.servlet.MultipartRequest"%> 6 <% 7 //saveText:上傳的檔案存放的目標路徑 8 String saveText = "c:\\1010"; 9 //每個上傳檔案大小(3是每次最多上傳的檔案個數;20是每個檔案大小;1024*1024是進位)10 int maxSize = 3*20*1024*1024;11 //上傳檔案:參數一是請求方式;參數二是存放目標路徑;參數三是上傳檔案大小;參數四是簡體中文12 MultipartRequest multi = new MultipartRequest(request,saveText,maxSize,"gb2312");13 //通過集合的枚舉類型(Enumeration),列印14 Enumeration files = multi.getParameterNames(); //由multi中,獲得所有上傳檔案15 while(files.hasMoreElements())16 {17 String files_name = (String)files.nextElement(); //由枚舉類型變數files中,迴圈獲得所有上傳檔案files_name18 File f = multi.getFile(files_name); //由所有上傳檔案files_name中,獲得每個上傳檔案f19 if(f!=null)20 {21 //獲得每個上傳檔案的名稱file_name22 String file_name = multi.getFilesystemName(files_name);23 String last_name = saveText+"\\"+file_name;24 out.print("上傳的檔案是:"+last_name);25 out.print("<hr>");26 }27 }28 %>
使用jsp實現檔案的上傳檔案;
1 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 2 <html> 3 <head> 4 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 5 <title>upload</title> 6 </head> 7 <body> 8 <form name="uploadForm" action="upload" method="post" enctype="MULTIPART/FORM-DATA"> 9 <table>10 <tr>11 <td><div align="right">Upload File:</div></td>12 <td><input type="file" name="uploadfile" size="30"></td>13 </tr>14 <tr>15 <td><input type="submit" name="submit" value = "upload"></td>16 </tr>17 18 </table>19 </form>20 </body>21 </html>