blind 发表于 2013-2-7 16:14:02

大文件上传

package com.fuyou;import java.io.File;import java.io.IOException;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.FileItemFactory;import org.apache.commons.fileupload.FileUploadException;import org.apache.commons.fileupload.disk.DiskFileItemFactory;import org.apache.commons.fileupload.servlet.ServletFileUpload;public class Fileupload extends HttpServlet {    /**   * Constructor of the object.   */    public Fileupload() {      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   */    public void doPost(HttpServletRequest request, HttpServletResponse response)            throws ServletException, IOException {    System.out.println("requestCo"+"=="+request.getContentType());      response.setContentType("text/html;charset=gb2312");      PrintWriter out = response.getWriter();      // 设置保存的文件的目录      String uploadDir = getServletContext().getRealPath("/upload");      System.out.println(uploadDir);      if (uploadDir == null) {            out.println("无法访问存储目录!");            return;      }      File fUploadDir = new File(uploadDir);      if (!fUploadDir.exists()) {            if(!fUploadDir.mkdir()){            out.println("无法创建存储目录!");            return;            }      }      if (!ServletFileUpload.isMultipartContent(request)) {            out.println("只能外理mul的类型的数据");            return;      }   FileItemFactory factory = new DiskFileItemFactory();      ServletFileUploadfu = new ServletFileUpload(factory);//关键类      fu.setSizeMax(1024 * 1024 * 400);      // FileItemFactory factory = new DiskFileItemFactory();      DiskFileItemFactory s = new DiskFileItemFactory();//设置临时文件最大值,如果大于这个值,就建一临时文件      s.setSizeThreshold(1024*1024);      fu.setHeaderEncoding("gb2312");      List fileItem = null;      try {            fileItem = fu.parseRequest(request);      } catch (FileUploadException e) {            out.println("解析出错了");            e.printStackTrace(out);            return;      }                  Iterator i = fileItem.iterator();      while (i.hasNext()) {            FileItem fi = (FileItem) i.next();            if (fi.isFormField()) {                String content = fi.getString("gb2312");                String fieldName = fi.getFieldName();                System.out.println(fieldName+content);            } else {                String pathSrc = fi.getName();                if (pathSrc.trim().equals("")) {                  continue;                }                int start = pathSrc.lastIndexOf("\\");                String fileName = pathSrc.substring(start + 1);                File pathDest = new File(uploadDir, fileName);                try {                  fi.write(pathDest);                  String fieldName = fi.getFieldName();                  request.setAttribute(fileName, fileName);                } catch (Exception e) {                  out.println("存储文件出现问题");                  e.printStackTrace(out);                  return;                }finally{                  fi.delete();                }            }      }    }    /**   * Initialization of the servlet. <br>   *   * @throws ServletException   *             if an error occurs   */    public void init() throws ServletException {      // Put your code here    }} 迟点再来示例一个使用dwr来显示上传进度的。
页: [1]
查看完整版本: 大文件上传