使用jsp实现文件的上传
//处理上传页面(如: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_name 18 File f = multi.getFile(files_name); //由所有上传文件files_name中,获得每个上传文件f 19 if(f!=null) 20 { 21 //获得每个上传文件的名称file_name 22 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>