upload.jsp
<%@page contentType="text/html;charset=utf-8" %>
<head>
<title>fbysss UploadBean 示例</title>
<!--meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"-->
<!--meta http-equiv="Content-Type" content="text/html; charset=gb2312"-->
</head>
<FORM name="form1" METHOD="POST" ACTION="adddata" ENCTYPE="multipart/form-data">
附件
<input name="attach" type="FILE" id="attach" size="50" />
<input name="ok" type= "submit" value="提交">
</form>
web.xml
<servlet>
<servlet-name>AddDataServlet</servlet-name>
<servlet-class>com.upload.action.AddDataServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>AddDataServlet</servlet-name>
<url-pattern>/adddata</url-pattern>
</servlet-mapping>
AddDataServlet.java
package com.upload.action;
import java.io.File;
import java.io.IOException;
import java.util.Iterator;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileItemFactory;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
public class AddDataServlet extends HttpServlet{
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doPost(request, response);
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String tempDirectory = "d:/temp/"; //要在最后加上斜杠:temp/,缓存文件目录
try {
int sizeThreshold = 1024 * 64; //写满该大小的缓存后,存入硬盘中。
File repositoryFile = new File(tempDirectory);
FileItemFactory factory = new DiskFileItemFactory(sizeThreshold, repositoryFile);
ServletFileUpload upload = new ServletFileUpload(factory);
upload.setHeaderEncoding("utf-8"); //设置字符编码
upload.setSizeMax(50 * 1024 * 1024); // set every upload file'size less than 50M
List items = upload.parseRequest(request); //这里开始执行上传
Iterator iter = items.iterator();
while (iter.hasNext()) {
FileItem item = (FileItem) iter.next(); //FileItem就是表示一个表单域。
if(item.isFormField()){ //isFormField方法用于判断FileItem是否代表一个普通表单域(即非file表单域)
System.out.println("***"+item.getFieldName());
}else {
//String fieldName = item.getFieldName(); //获取表单域name属性的值
//String fileName = item.getName(); //返回该文件在客户机上的文件名。e.g: e:\dianying\\video\1.wmv
//System.out.println("*****"+fieldName);
//System.out.println("*****"+fileName);
String path = item.getName();
String fileName = path.substring(path.lastIndexOf("\\"));
File uploadedFile = new File("d:/video/" + fileName);
item.write(uploadedFile);
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
}