Example #1
0
  /**
   * Processes one request for a file or directory.
   *
   * @param request the pathname for the requested file
   * @param headers
   * @param out output stream for the client connection
   * @throws IOException
   */
  private void handleFileRequestPOST(
      String request, List<String> headers, PrintStream out, InputStream in) {
    System.out.println("File given: " + request);

    try {
      File f = new File(CONTENT_BASE_DIR_NAME + request);
      if (!checkBelowAndHandle(out, f)) return;
      if (f.exists()) {
        out.println("HTTP/1.0 409 Conflict\r\n\r\n");
        out.print(
            "<html><body>It seems your file already exists...<br />"
                + "you wouldn't be trying ot overwrite someones stuff, would ya?</html></body>");
        out.flush();
        return;
      }

      if (f.isDirectory()) {
        handleDirectoryRequest(out, f);
      } else {
        // catch FileNotFoundException if file doesn't exist
        FileOutputStream fos = new FileOutputStream(f);

        out.println("HTTP/1.0 201 Created\r\n\r\n");
        // Need to write post contents to file
        while (true) {
          if (in.available() != 0) {
            int next = in.read();
            if (next == -1) break;
            fos.write(next);
          }
        }
        fos.flush();
        fos.close();
        out.flush();
        out.close();
        in.close();
        return;
      }

    } catch (FileNotFoundException e) {
      System.out.println("File not found: " + request);
      out.println("HTTP/1.0 404 Not Found\r\n\r\n");
      out.flush();
    } catch (IOException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }
  }
Example #2
0
  /**
   * Processes one request for a file or directory.
   *
   * @param request the pathname for the requested file
   * @param headers
   * @param out output stream for the client connection
   * @throws IOException
   */
  private void handleFileRequestGET(
      String request, List<String> headers, PrintStream out, InputStream in) {
    System.out.println("File requested: " + request);

    try {
      File f = new File(CONTENT_BASE_DIR_NAME + request);

      if (!checkBelowAndHandle(out, f)) return;

      if (f.isDirectory()) {
        handleDirectoryRequest(out, f);
      } else {
        // catch FileNotFoundException if file doesn't exist
        FileInputStream fis = new FileInputStream(f);

        out.print("HTTP/1.0 200 OK\r\n");
        out.print("Content-Type: " + SimpleHttpServer2.guessMimeType(f) + "\r\n");
        out.print("Content-Length: " + f.length() + "\r\n\r\n");
        out.flush();

        // copy contents of file to output stream
        // loop uses array versions of read/write methods to
        // copy multiple bytes with each call
        byte[] data = new byte[64 * 1024];
        int bytesRead;
        while ((bytesRead = fis.read(data)) != -1) {
          out.write(data, 0, bytesRead);
        }
        out.flush();
        out.close();
        in.close();
        return;
      }
    } catch (FileNotFoundException e) {
      System.out.println("File not found: " + request);
      out.println("HTTP/1.0 404 Not Found\r\n\r\n");
      out.println("<html><body>404 File Not Found, not my problem.  Sorry.</body></html>");
      out.flush();
    } catch (IOException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }
  }