protected void doPost(HttpServletRequest request, HttpServletResponse response)
      throws ServletException, IOException {
    super.doPost(request, response);

    try {
      if (!ServletFileUpload.isMultipartContent(request)) {
        response.sendError(HttpServletResponse.SC_BAD_REQUEST);
        return;
      }

      ServletFileUpload sfU = new ServletFileUpload();
      FileItemIterator items = sfU.getItemIterator(request);
      while (items.hasNext()) {
        FileItemStream item = items.next();
        if (!item.isFormField()) {
          InputStream stream = item.openStream();
          Document doc = editor.toDocument(stream);
          stream.close();
          handleDocument(request, response, doc);
          return;
        }
      }

      response.sendError(HttpServletResponse.SC_BAD_REQUEST, "No XML uploaded");
    } catch (FileUploadException fuE) {
      response.sendError(HttpServletResponse.SC_BAD_REQUEST, fuE.getMessage());
    } catch (JDOMException jE) {
      response.sendError(HttpServletResponse.SC_BAD_REQUEST, jE.getMessage());
    }
  }
예제 #2
0
파일: Uploader.java 프로젝트: iceleeyo/fmh
  public void upload() throws Exception {
    boolean isMultipart = ServletFileUpload.isMultipartContent(this.request);
    if (!isMultipart) {
      this.state = this.errorInfo.get("NOFILE");
      return;
    }

    if (this.inputStream == null) {
      this.state = this.errorInfo.get("FILE");
      return;
    }

    // 存储title
    this.title = this.getParameter("pictitle");

    try {
      String savePath = this.getFolder(this.savePath);

      if (!this.checkFileType(this.originalName)) {
        this.state = this.errorInfo.get("TYPE");
        return;
      }

      this.fileName = this.getName(this.originalName);
      this.type = this.getFileExt(this.fileName);
      this.url = savePath + "/" + this.fileName;

      FileOutputStream fos = new FileOutputStream(this.getPhysicalPath(this.url));
      BufferedInputStream bis = new BufferedInputStream(this.inputStream);
      byte[] buff = new byte[128];
      int count = -1;

      while ((count = bis.read(buff)) != -1) {

        fos.write(buff, 0, count);
      }

      bis.close();
      fos.close();

      this.state = this.errorInfo.get("SUCCESS");
    } catch (Exception e) {
      e.printStackTrace();
      this.state = this.errorInfo.get("IO");
    }
  }