package com.hongfei.servlet;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
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.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import com.hongfei.lister.UploadLister;
@SuppressWarnings("serial")
public class Upload extends HttpServlet {
/**
* Constructor of the object.
*/
public Upload() {
super();
}
/**
* Destruction of the servlet. <br>
*/
public void destroy() {
super.destroy(); // Just puts "destroy" string in log
// Put your code here
}
/**
* The doGet method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to get.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out
.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");
out.println("<HTML>");
out.println(" <HEAD><TITLE>A Servlet</TITLE></HEAD>");
out.println(" <BODY>");
out.print(" This is ");
out.print(this.getClass());
out.println(", using the GET method");
out.println(" </BODY>");
out.println("</HTML>");
out.flush();
out.close();
}
/**
* The doPost method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to post.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
@SuppressWarnings({ "deprecation", "unchecked" })
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
com.hongfei.entity.Upload upload = new com.hongfei.entity.Upload();
UploadLister lister = new UploadLister(upload);
ServletFileUpload servletFileUpload = new ServletFileUpload(new DiskFileItemFactory());
//只是上传监听器
servletFileUpload.setProgressListener(lister);
request.getSession().setAttribute("upload", upload);
List list = null;
try {
list = servletFileUpload.parseRequest(request);
} catch (FileUploadException e) {
e.printStackTrace();
}
for(Iterator iter = list.iterator(); iter.hasNext();){
//得到文件对象
FileItem fileItem = (FileItem)iter.next();
//是表单才进行处理
if(fileItem.isFormField()){
break;
}
//同意linux和windows的路径分隔符
String name = fileItem.getName().replaceAll("/", "\\");
//得到文件名
int index = name.lastIndexOf("\\");
String fileFileName = "";
if(index == -1){
fileFileName = name;
}else{
fileFileName = name.substring(index + 1);
}
InputStream fileInputStream = fileItem.getInputStream();
String path = request.getRealPath("/upload");
//也可不用自己写实现方法直接使用,fileItem.write(uploadFile);
File uploadFile = new File(path,fileFileName);
//首先要确认路径是否存在
uploadFile.getParentFile().mkdirs();
//检查文件是否已经存在
if(!uploadFile.exists()){
//建立文件
uploadFile.createNewFile();
}
FileOutputStream out2 = new FileOutputStream(uploadFile);
//开始copy文件
@SuppressWarnings("unused")
int len = 0;//每次读取的字节数
byte[] bytes = new byte[1024];
while((len = fileInputStream.read(bytes, 0, bytes.length)) != -1){
out2.write(bytes);
}
out2.flush();
out2.close();
fileInputStream.close();
}
out.flush();
out.close();
}
/**
* Initialization of the servlet. <br>
*
* @throws ServletException if an error occurs
*/
public void init() throws ServletException {
// Put your code here
}
}