前端代码:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<form action="UploadServlet" method="post" enctype="multipart/form-data">
姓名:<input type="text" id="username" name="username"/>
</br>
年龄:<input type="text" id="age" name="age"/>
</br>
简历:<input type="file" id="resume" name="resume"/>
</br>
<input type="submit" value="提交">
<input type="reset" value="重置">
</form>
</body>
</html>
后台Servlet逻辑:
package com.epoint.demo;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
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.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
/**
* Servlet implementation class UploadServlet
*/
@WebServlet("/UploadServlet")
public class UploadServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public UploadServlet() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
//response.getWriter().append("Served at: ").append(request.getContextPath());
//获取FileItem的集合,需要先把form表单的enctype="multipart/form-data"才行
//1.创建DiskFileItemFactory工厂类
DiskFileItemFactory factory = new DiskFileItemFactory();
//2.设置内存可存放上传文件大小,超出则写入临时文件
factory.setSizeThreshold(1024 *500);
//3.设置临时文件位置
File file = new File("D:/mytest");
factory.setRepository(file);
//4.创建ServletFileUpload上传工具
ServletFileUpload upload = new ServletFileUpload(factory);
//5.设置上传文件的总大小(1M)
upload.setSizeMax(1024 * 1000);
//6.解析Request对象,把表单中的每一个输入项包装成一个filrItem对象
try {
List<FileItem> items = upload.parseRequest(request);
//遍历获取到的fileItem集合
for (FileItem fileItem : items) {
//判断当前fileItem是form表单域还是文件域
if (fileItem.isFormField()) { //是表单域(普通属性,不是文件)
String fieldName = fileItem.getFieldName(); //表单属性名
String string = fileItem.getString(); //表单数据
System.out.println(fileItem + " : " +string);
}else { //是文件
String fileName = fileItem.getName(); //获取文件名
String contentType = fileItem.getContentType(); //文件类型
long size = fileItem.getSize(); //文件大小
System.out.println(fileName);
System.out.println(contentType);
System.out.println(size);
//保存上传的文件
InputStream in = fileItem.getInputStream(); //读取上传文件流
byte[] buffer=new byte[1024];
int len=0;
fileName="D:/test/"+fileName; //文件完整路径
FileOutputStream out = new FileOutputStream(fileName);
while((len=in.read(buffer))!=-1) { //文件没有读完
out.write(buffer, 0, len);
}
out.close();
in.close();
}
}
} catch (FileUploadException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
}
}