上传页面
<%
@ page language
=
"
java
"
contentType
=
"
text/html; charset=GB18030
"
pageEncoding = " GB18030 " %>
<! DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd" >
< html >
< head >
< meta http-equiv ="Content-Type" content ="text/html; charset=GB18030" >
< title > Insert title here </ title >
</ head >
< body >
< form name ="form1" enctype ="multipart/form-data" method ="post" action ="file.jsp" >
上传文件1: < input type ="file" name ="file1" size ="20" maxlength ="20" >< br >
文件描述1: < input type ="text" name ="file1" size ="20" maxlength ="20" >< br >< br >
上传文件2: < input type ="file" name ="file2" size ="20" maxlength ="20" >< br >
文件描述2: < input type ="text" name ="file2" size ="20" maxlength ="20" >< br >< br >
上传文件3: < input type ="file" name ="file3" size ="20" maxlength ="20" >< br >
文件描述3: < input type ="text" name ="file3" size ="20" maxlength ="20" >< br >< br >
< input type ="submit" value ="submit" />
< input type ="reset" value ="reset" />
</ form >
</ body >
</ html >
pageEncoding = " GB18030 " %>
<! DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd" >
< html >
< head >
< meta http-equiv ="Content-Type" content ="text/html; charset=GB18030" >
< title > Insert title here </ title >
</ head >
< body >
< form name ="form1" enctype ="multipart/form-data" method ="post" action ="file.jsp" >
上传文件1: < input type ="file" name ="file1" size ="20" maxlength ="20" >< br >
文件描述1: < input type ="text" name ="file1" size ="20" maxlength ="20" >< br >< br >
上传文件2: < input type ="file" name ="file2" size ="20" maxlength ="20" >< br >
文件描述2: < input type ="text" name ="file2" size ="20" maxlength ="20" >< br >< br >
上传文件3: < input type ="file" name ="file3" size ="20" maxlength ="20" >< br >
文件描述3: < input type ="text" name ="file3" size ="20" maxlength ="20" >< br >< br >
< input type ="submit" value ="submit" />
< input type ="reset" value ="reset" />
</ form >
</ body >
</ html >
处理页面
<%
@ page language
=
"
java
"
contentType
=
"
text/html; charset=GB18030
"
pageEncoding = " GB18030 " %>
<% @ page import = " java.io.*,java.util.*,com.oreilly.servlet.MultipartRequest " %>
<% @ taglib prefix = " fmt " uri = " http://java.sun.com/jsp/jstl/fmt " %>
<! DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd" >
< html >
< head >
< meta http-equiv ="Content-Type" content ="text/html; charset=GB18030" >
< title > Insert title here </ title >
</ head >
< fmt:requestEncoding value ="gb2312" />
< body >
<%
String saveDirectory = " /upload " ; // 设置上传目录
String contextPath = request.getSession().getServletContext().getRealPath(saveDirectory);
System.out.println(contextPath);
File a = new File(contextPath);
System.out.println(a.isDirectory());
if (!a.isDirectory()){
a.mkdir();
}
int maxPostSize = 5 * 1024 * 1024 ; // 设定大小为5M
String filename = null ;
String contentType = null ;
String description = null ;
int count = 0 ;
MultipartRequest multi = new MultipartRequest(request,contextPath,maxPostSize);
// 取得所有上传文件输入类型名称及描述
Enumeration filesname = multi.getFileNames();
Enumeration filesdesc = multi.getParameterNames();
while (filesname.hasMoreElements()){
String name = ( String )filesname.nextElement();
String dc = ( String )filesdesc.nextElement();
filename = multi.getFilesystemName(name);
contentType = multi.getContentType(name);
description = multi.getParameter(dc);
if (filename! = null ){
count ++ ;
%>
您上传第 <% = count %> 个文件: < br >
文件名: <% = filename %> < br >
文件类型: <% = contentType %> < br >
文件描述: <% = description %> < br >< br >
<%
}
}
%>
您总共上传 <% = count %> 个文件
</ body >
</ html >
pageEncoding = " GB18030 " %>
<% @ page import = " java.io.*,java.util.*,com.oreilly.servlet.MultipartRequest " %>
<% @ taglib prefix = " fmt " uri = " http://java.sun.com/jsp/jstl/fmt " %>
<! DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd" >
< html >
< head >
< meta http-equiv ="Content-Type" content ="text/html; charset=GB18030" >
< title > Insert title here </ title >
</ head >
< fmt:requestEncoding value ="gb2312" />
< body >
<%
String saveDirectory = " /upload " ; // 设置上传目录
String contextPath = request.getSession().getServletContext().getRealPath(saveDirectory);
System.out.println(contextPath);
File a = new File(contextPath);
System.out.println(a.isDirectory());
if (!a.isDirectory()){
a.mkdir();
}
int maxPostSize = 5 * 1024 * 1024 ; // 设定大小为5M
String filename = null ;
String contentType = null ;
String description = null ;
int count = 0 ;
MultipartRequest multi = new MultipartRequest(request,contextPath,maxPostSize);
// 取得所有上传文件输入类型名称及描述
Enumeration filesname = multi.getFileNames();
Enumeration filesdesc = multi.getParameterNames();
while (filesname.hasMoreElements()){
String name = ( String )filesname.nextElement();
String dc = ( String )filesdesc.nextElement();
filename = multi.getFilesystemName(name);
contentType = multi.getContentType(name);
description = multi.getParameter(dc);
if (filename! = null ){
count ++ ;
%>
您上传第 <% = count %> 个文件: < br >
文件名: <% = filename %> < br >
文件类型: <% = contentType %> < br >
文件描述: <% = description %> < br >< br >
<%
}
}
%>
您总共上传 <% = count %> 个文件
</ body >
</ html >
需要注意两点:
(1)上传目录必须存在
(2)以上代码暂时不支持中文,支持中文只需要改动
String enCoding="gb2312";
MultipartRequest multi=new MultipartRequest(request,contextPath,maxPostSize,enCoding);