1>首先创建一个HTML页面。注意,凡是要上载文件的表单都必须设置enctype属性,且属性的值必须是multipart/form-data,同时请求方法必须是POST.
<form name="myform" action="fileuploaddemo.jsp" method="post" enctype="multipart/form-data">
2>commons-fileupload-1.1.jar和commons-io-1.1.jar 两个包都要引入,
因为在parseRequest(request)的类有关继承于DiskFileItem类。
3>示例
1.示例1。
DiskFileUpload类会对mulipart类的表单中的所有字段进行处理,不只是file字段。getName()得到文件名,getString()得到表单数据内容,isFormField()可判断是否为普通的表单项。
demo1.html
demo1.html
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=GB18030">
<title>File upload</title>
</head>
<body>
//必须是multipart的表单数据。
<form name="myform" action="demo1.jsp" method="post" enctype="multipart/form-data">
Your name:<br>
<input type="text" name="name" size="15"><br>
File:<br>
<input type="file" name="myfile"><br><br>
<input type="submit" name="submit" value="Commit">
</form>
</body>
</html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=GB18030">
<title>File upload</title>
</head>
<body>
//必须是multipart的表单数据。
<form name="myform" action="demo1.jsp" method="post" enctype="multipart/form-data">
Your name:<br>
<input type="text" name="name" size="15"><br>
File:<br>
<input type="file" name="myfile"><br><br>
<input type="submit" name="submit" value="Commit">
</form>
</body>
</html>
demo1.jsp
<%@ page language="java" contentType="text/html; charset=GB18030"
pageEncoding="GB18030"%>
<%@ page import="org.apache.commons.fileupload.*"%>
<%@ page import="java.util.*"%>
pageEncoding="GB18030"%>
<%@ page import="org.apache.commons.fileupload.*"%>
<%@ page import="java.util.*"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<%
boolean isMultipart=FileUpload.isMultipartContent(request);//检查输入请求是否为multipart表单数据。
if(isMultipart==true){
DiskFileUpload upload=new DiskFileUpload();//为该请求创建一个DiskFileUpload对象,通过它来解析请求。执行解析后,所有的表单项目都保存在一个List中。
List items=upload.parseRequest(request);
Iterator itr=items.iterator();
while(itr.hasNext()){
FileItem item=(FileItem)itr.next();
//检查当前项目是普通表单项目还是上传文件。
if(item.isFormField()){//如果是普通表单项目,显示表单内容。
String fieldName=item.getFieldName();
if(fieldName.equals("name")) //对应demo1.html中type="text" name="name"
out.println("the field name is"+ item.getString());//显示表单内容。
out.println("<br>");
}
else{//如果是上传文件,显示文件名。
out.println("the upload fielname is"+item.getName());
out.println("<br>");
}
}
}
else{
out.println("the enctype must be multipart/form-data");
}
%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=GB18030">
<title>File upload</title>
</head>
<body>
</body>
</html>
<%
boolean isMultipart=FileUpload.isMultipartContent(request);//检查输入请求是否为multipart表单数据。
if(isMultipart==true){
DiskFileUpload upload=new DiskFileUpload();//为该请求创建一个DiskFileUpload对象,通过它来解析请求。执行解析后,所有的表单项目都保存在一个List中。
List items=upload.parseRequest(request);
Iterator itr=items.iterator();
while(itr.hasNext()){
FileItem item=(FileItem)itr.next();
//检查当前项目是普通表单项目还是上传文件。
if(item.isFormField()){//如果是普通表单项目,显示表单内容。
String fieldName=item.getFieldName();
if(fieldName.equals("name")) //对应demo1.html中type="text" name="name"
out.println("the field name is"+ item.getString());//显示表单内容。
out.println("<br>");
}
else{//如果是上传文件,显示文件名。
out.println("the upload fielname is"+item.getName());
out.println("<br>");
}
}
}
else{
out.println("the enctype must be multipart/form-data");
}
%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=GB18030">
<title>File upload</title>
</head>
<body>
</body>
</html>
2.示例2。
上传两个文件到指定的目录。
demo2.html
demo2.html
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=GB18030">
<title>File upload</title>
</head>
<body>
<form name="myform" action="demo2.jsp" method="post" enctype="multipart/form-data">
File1:<br>
<input type="file" name="myfile1"><br>
File2:<br>
<input type="file" name="myfile2"><br><br>
<input type="submit" name="submit" value="Commit">
</form>
</body>
</html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=GB18030">
<title>File upload</title>
</head>
<body>
<form name="myform" action="demo2.jsp" method="post" enctype="multipart/form-data">
File1:<br>
<input type="file" name="myfile1"><br>
File2:<br>
<input type="file" name="myfile2"><br><br>
<input type="submit" name="submit" value="Commit">
</form>
</body>
</html>
demo2.jsp
<%@ page language="java" contentType="text/html; charset=GB18030"
pageEncoding="GB18030"%>
<%@ page import="org.apache.commons.fileupload.*"%>
<%@ page import="java.util.*"%>
<%@ page import="java.io.*"%>
pageEncoding="GB18030"%>
<%@ page import="org.apache.commons.fileupload.*"%>
<%@ page import="java.util.*"%>
<%@ page import="java.io.*"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<%String uploadPath="D:\\Dvp\\workspace4Lomboz\\Test4Jsp\\uploadfiles";
boolean isMultipart=FileUpload.isMultipartContent(request);
if(isMultipart==true){
try{
DiskFileUpload upload=new DiskFileUpload();
List items=upload.parseRequest(request);//得到所有的文件
Iterator itr=items.iterator();
while(itr.hasNext()){//依次处理每个文件
FileItem item=(FileItem)itr.next();
String fileName=item.getName();//获得文件名,包括路径
if(fileName!=null){
File fullFile=new File(item.getName());
File savedFile=new File(uploadPath,fullFile.getName());
item.write(savedFile);
}
}
out.println("upload succeed");
}
catch(Exception e){
e.printStackTrace();
}
}
else{
out.println("the enctype must be multipart/form-data");
}
%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=GB18030">
<title>File upload</title>
</head>
<body>
</body>
</html>
<%String uploadPath="D:\\Dvp\\workspace4Lomboz\\Test4Jsp\\uploadfiles";
boolean isMultipart=FileUpload.isMultipartContent(request);
if(isMultipart==true){
try{
DiskFileUpload upload=new DiskFileUpload();
List items=upload.parseRequest(request);//得到所有的文件
Iterator itr=items.iterator();
while(itr.hasNext()){//依次处理每个文件
FileItem item=(FileItem)itr.next();
String fileName=item.getName();//获得文件名,包括路径
if(fileName!=null){
File fullFile=new File(item.getName());
File savedFile=new File(uploadPath,fullFile.getName());
item.write(savedFile);
}
}
out.println("upload succeed");
}
catch(Exception e){
e.printStackTrace();
}
}
else{
out.println("the enctype must be multipart/form-data");
}
%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=GB18030">
<title>File upload</title>
</head>
<body>
</body>
</html>
3.示例3。
上传一个文件到指定的目录,并限定文件大小。
demo3.html
demo3.html
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=GB18030">
<title>File upload</title>
</head>
<body>
<form name="myform" action="demo3.jsp" method="post" enctype="multipart/form-data">
File:<br>
<input type="file" name="myfile"><br><br>
<input type="submit" name="submit" value="Commit">
</form>
</body>
</html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=GB18030">
<title>File upload</title>
</head>
<body>
<form name="myform" action="demo3.jsp" method="post" enctype="multipart/form-data">
File:<br>
<input type="file" name="myfile"><br><br>
<input type="submit" name="submit" value="Commit">
</form>
</body>
</html>
demo3.jsp
<%@ page language="java" contentType="text/html; charset=GB18030"
pageEncoding="GB18030"%>
<%@ page import="org.apache.commons.fileupload.*"%>
<%@ page import="java.util.*"%>
<%@ page import="java.io.*"%>
pageEncoding="GB18030"%>
<%@ page import="org.apache.commons.fileupload.*"%>
<%@ page import="java.util.*"%>
<%@ page import="java.io.*"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<%File uploadPath=new File("D:\\upload");//上传文件目录
if(!uploadPath.exists()){
uploadPath.mkdirs();
}
String tempPath="d:\\upload\\temp\\"; // 临时文件目录
File tempPathFile=new File("d:\\upload\\temp");
if(!tempPathFile.exists()){
tempPathFile.mkdirs();
}
try{
DiskFileUpload fu=new DiskFileUpload();
fu.setSizeMax(4194304); // 设置最大文件尺寸,这里是4MB
fu.setSizeThreshold(4096); // 设置缓冲区大小,这里是4kb
fu.setRepositoryPath(tempPath);//设置缓冲区目录
List fileItems=fu.parseRequest(request);
Iterator i=fileItems.iterator();
while(i.hasNext()){
FileItem fi=(FileItem)i.next();
String fileName=fi.getName();
if(fileName!=null){
File fullFile=new File(fi.getName());
File savedFile=new File(uploadPath,fullFile.getName());
fi.write(savedFile);
}
}
out.println("upload succeed");
}
catch(Exception e){
e.printStackTrace();
}
%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=GB18030">
<title>File upload</title>
</head>
<body>
</body>
</html>
<%File uploadPath=new File("D:\\upload");//上传文件目录
if(!uploadPath.exists()){
uploadPath.mkdirs();
}
String tempPath="d:\\upload\\temp\\"; // 临时文件目录
File tempPathFile=new File("d:\\upload\\temp");
if(!tempPathFile.exists()){
tempPathFile.mkdirs();
}
try{
DiskFileUpload fu=new DiskFileUpload();
fu.setSizeMax(4194304); // 设置最大文件尺寸,这里是4MB
fu.setSizeThreshold(4096); // 设置缓冲区大小,这里是4kb
fu.setRepositoryPath(tempPath);//设置缓冲区目录
List fileItems=fu.parseRequest(request);
Iterator i=fileItems.iterator();
while(i.hasNext()){
FileItem fi=(FileItem)i.next();
String fileName=fi.getName();
if(fileName!=null){
File fullFile=new File(fi.getName());
File savedFile=new File(uploadPath,fullFile.getName());
fi.write(savedFile);
}
}
out.println("upload succeed");
}
catch(Exception e){
e.printStackTrace();
}
%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=GB18030">
<title>File upload</title>
</head>
<body>
</body>
</html>
4.示例4。
利用Servlet来实现文件上传。
Upload.java
Upload.java
package com.zj.sample;
import java.io.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
import org.apache.commons.fileupload.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
import org.apache.commons.fileupload.*;
public class Upload extends HttpServlet {
private String uploadPath="D:\\upload\\"; // 上传文件的目录
private String tempPath= "d:\\upload\\temp\\"; // 临时文件目录
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws IOException, ServletException {
try {
DiskFileUpload fu = new DiskFileUpload();
fu.setSizeMax(4194304);
fu.setSizeThreshold(4096);
fu.setRepositoryPath(tempPath);
List fileItems = fu.parseRequest(request);
Iterator i = fileItems.iterator();
while (i.hasNext()) {
FileItem fi=(FileItem)i.next();
String fileName=fi.getName();
if(fileName!=null){
File fullFile=new File(fi.getName());
File savedFile=new File(uploadPath,fullFile.getName());
fi.write(savedFile);
}
}
System.out.println("upload succeed");
}
catch (Exception e) {
// 可以跳转出错页面
e.printStackTrace();
}
}
private String uploadPath="D:\\upload\\"; // 上传文件的目录
private String tempPath= "d:\\upload\\temp\\"; // 临时文件目录
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws IOException, ServletException {
try {
DiskFileUpload fu = new DiskFileUpload();
fu.setSizeMax(4194304);
fu.setSizeThreshold(4096);
fu.setRepositoryPath(tempPath);
List fileItems = fu.parseRequest(request);
Iterator i = fileItems.iterator();
while (i.hasNext()) {
FileItem fi=(FileItem)i.next();
String fileName=fi.getName();
if(fileName!=null){
File fullFile=new File(fi.getName());
File savedFile=new File(uploadPath,fullFile.getName());
fi.write(savedFile);
}
}
System.out.println("upload succeed");
}
catch (Exception e) {
// 可以跳转出错页面
e.printStackTrace();
}
}
public void init() throws ServletException {
File uploadPath = new File("D:\\upload");
if (!uploadPath.exists()) {
uploadPath.mkdirs();
}
File tempPathFile = new File("d:\\upload\\temp");
if (!tempPathFile.exists()) {
tempPathFile.mkdirs();
}
}
}
File uploadPath = new File("D:\\upload");
if (!uploadPath.exists()) {
uploadPath.mkdirs();
}
File tempPathFile = new File("d:\\upload\\temp");
if (!tempPathFile.exists()) {
tempPathFile.mkdirs();
}
}
}
demo4.html
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=GB18030">
<title>File upload</title>
</head>
<body>
// action="fileupload"对应web.xml中<servlet-mapping>中<url-pattern>的设置。
<form name="myform" action="fileupload" method="post" enctype="multipart/form-data">
File:<br>
<input type="file" name="myfile"><br><br>
<input type="submit" name="submit" value="Commit">
</form>
</body>
</html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=GB18030">
<title>File upload</title>
</head>
<body>
// action="fileupload"对应web.xml中<servlet-mapping>中<url-pattern>的设置。
<form name="myform" action="fileupload" method="post" enctype="multipart/form-data">
File:<br>
<input type="file" name="myfile"><br><br>
<input type="submit" name="submit" value="Commit">
</form>
</body>
</html>
web.xml
<servlet>
<servlet-name>Upload</servlet-name>
<servlet-class>com.zj.sample.Upload</servlet-class>
</servlet>
<servlet-name>Upload</servlet-name>
<servlet-class>com.zj.sample.Upload</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Upload</servlet-name>
<url-pattern>/fileupload</url-pattern>
<servlet-name>Upload</servlet-name>
<url-pattern>/fileupload</url-pattern>