import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.Date;
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.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
/**
* Servlet implementation class UploadServlet
*/
@SuppressWarnings({"serial", "unchecked"})
public class UploadServlet extends HttpServlet {
/**
* @see HttpServlet#HttpServlet()
*/
public UploadServlet() {
super();
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
* response)
*/
protected void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
doPost(request, response);
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
* response)
*/
protected void doPost(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("UTF-8");
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
String uploaddir = getServletContext().getInitParameter("uploaddir");
// 获取上传文件的保存目录
String dirPath = getServletContext().getRealPath(uploaddir);
dirPath = dirPath + "\\" + new SimpleDateFormat("yyyy\\MM\\dd").format(new Date());
out.println("dirPath: " + dirPath + "<br>");
request.setAttribute("dirPath", dirPath);
// 创建文件保存目录对象
File dirFile = new File(dirPath);
if(!dirFile.exists()){
// 如果该目录不存在,则创建目录
dirFile.mkdirs();
}
// Check that we have a file upload request
boolean isMultipart = ServletFileUpload.isMultipartContent(request);
out.println("isMultipart: " + isMultipart + "<br>");
// Create a factory for disk-based file items
DiskFileItemFactory factory = new DiskFileItemFactory();
// Create a new file upload handler
ServletFileUpload upload = new ServletFileUpload(factory);
try {
// Parse the request
List<FileItem> fileItems = upload.parseRequest(request);
// 遍历fileItems
for(FileItem item : fileItems){
// 处理普通的表单元素
if(item.isFormField()){
String name = item.getFieldName();
String value = item.getString("UTF-8");
out.println("name: " + name + "<br>");
out.println("value: " + value + "<br>");
}else{
// 处理file类型的表单元素
String fieldName = item.getFieldName();
String fileName = item.getName();//************
String contentType = item.getContentType();
boolean isInMemory = item.isInMemory();
long sizeInBytes = item.getSize();
out.println("fieldName: " + fieldName + "<br>");
out.println("fileName: " + fileName + "<br>");
out.println("contentType: " + contentType + "<br>");
out.println("isInMemory: " + isInMemory + "<br>");
out.println("sizeInBytes: " + sizeInBytes + "<br>");
request.setAttribute("fileName", fileName);
request.setAttribute("contentType", contentType);
int location = fileName.lastIndexOf("\\") != -1 ? fileName.lastIndexOf("\\") : fileName.lastIndexOf("/");
fileName = fileName.substring(location + 1);
out.println("after fileName: " + fileName + "<br>");
File saveFile = new File(dirFile, fileName);
/*InputStream inputStream = item.getInputStream();
FileOutputStream outputStream = new FileOutputStream(saveFile);
byte [] buffer = new byte[1024];
int len = 0;
while(( len = inputStream.read(buffer)) != -1){
outputStream.write(buffer, 0, len);
}
outputStream.flush();
outputStream.close();
inputStream.close();*/
item.write(saveFile);
}
}
request.getRequestDispatcher("result.jsp").forward(request, response);
} catch (Exception e) {
e.printStackTrace();
}
}
}