Results 1 to 3 of 3
  1. #1
    Join Date
    Jan 2011
    Location
    Kolkata
    Posts
    3
    Rep Power
    0

    Default Http Input Stream read method

    I have a requirement to write a custom file server in java and send requests to it via a html page. Now I have tried it with first opening up a SerrverSocket at the 5000 port, and I have an html fom sending it a multipart request. I intend to capture the entire http reuest primarily and then use the apache commons library to separate out byte array from the multipart request. Via the read method I cant determine the EOF the stream. after the last byte is read the read method blocks till the html window is closed. can anyone let me any other strategy to know to handle this current requirement.(after extracting the byte array I need to send back a response to the html page).

    please find below sample java code ----
    ServerSocket serverSocket = new ServerSocket(5000);
    String FOLDER_NAME = "uploaded_files";
    while(true) {
    try{
    Socket socket = serverSocket.accept();
    InputStream is = socket.getInputStream();
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    //BufferedReader reader = new BufferedReader(new InputStreamReader(is));

    DataInputStream reader = new DataInputStream(is);

    String currentLine;
    int cnt = 0;
    boolean postRequest = false;
    String dataBoundary = null;
    String fileName = null;
    String yourName = null;
    while((currentLine = reader.readLine()) != null) {
    if(currentLine.contains("POST")) {
    postRequest = true;
    System.out.println("POST REQ AS EXPECTED VERY NICE");
    continue;
    }
    if(!postRequest) {
    System.out.println("NO POST REQ THIS BREAKING FLOW");
    break;
    } else {
    if(currentLine.contains("Content-Type: multipart/form-data; boundary=")) {
    System.out.println("found a boundary value header");
    dataBoundary = currentLine.substring((currentLine.indexOf("bounda ry=") + "boundary=".length()), (currentLine.length() -1));
    System.out.println("boundary value = ".concat(dataBoundary));
    continue;
    }
    if(dataBoundary != null && currentLine.contains(dataBoundary)) {
    cnt++;
    }
    if(cnt == 1) {
    //move 3 lines
    if(currentLine.contains("Content-Disposition: form-data; name=\"yourName\"")){
    reader.readLine();//skip a line
    }
    System.out.println("Your name = ".concat(yourName = reader.readLine()));
    continue;
    } else if(cnt == 2) {
    if(currentLine.contains("Content-Disposition: form-data; name=\"sentFile\"; filename=\"")){
    fileName = currentLine.substring(currentLine.indexOf("filenam e=") + "filename=".length() + 1, currentLine.length() - 1);
    System.out.println("File Name = ".concat(fileName));
    reader.readLine();//skip a line , this would depict a content type header
    reader.readLine();//skip a line, this would indicate a blank line to mark the start of data.
    continue;
    } else {
    // write the content to os
    if(currentLine != null && !currentLine.contains(dataBoundary)) {
    baos.write(currentLine.concat("\r").getBytes());
    //baos.write(currentLine.getBytes());
    }
    }
    } else if( cnt == 3) {
    System.out.println(("cnt [" + cnt).concat( "], current line [").concat(currentLine).concat("]"));
    break;
    }
    }
    }
    if(fileName == null ||yourName == null) {
    System.out.println("FileServer.main() dont bother about this" );
    } else {
    //send a response back
    PrintWriter pw = new PrintWriter(new OutputStreamWriter(socket.getOutputStream()));
    pw.write(responseMessage(yourName, fileName));
    pw.flush();
    //close output stream
    pw.close();
    //handle the request bytearray.
    FileOutputStream fos = new FileOutputStream(FOLDER_NAME + "/" + fileName);
    //fos.write(baos.toByteArray(), 0, baos.toByteArray().length - 1);
    fos.write(baos.toByteArray(), 0, baos.toByteArray().length - 1);
    fos.close();
    }
    //close input stream
    reader.close();
    socket.close();
    baos.close();
    } catch(Exception ex) {
    ex.printStackTrace();
    }

    please find the content of the html page ---
    <form method="post" id="fileForm" name="fileForm" enctype="multipart/form-data" action="http://localhost:5000/dontcare.do" >

    Enter your file<input type="file" name="custom_file" />

    <br/>

    Click to Submit<input type="submit" value="button" onclick="submitDoc();"/>

    </form>


    Thanks & Regards
    MishraC

  2. #2
    travishein's Avatar
    travishein is offline Senior Member
    Join Date
    Sep 2009
    Location
    Canada
    Posts
    684
    Rep Power
    5

    Default

    why wouldn't you just use a servlet ?

  3. #3
    Join Date
    Jan 2011
    Location
    Kolkata
    Posts
    3
    Rep Power
    0

    Default

    See I know a servlet deployed to a tomcat and apache commons file upload library is an alternative option, but its an option i cant use. i kinda have the following restrictions/ requirements.

    1. I work with a BPM product, which unfortunately has a bug in its file uploading mechanism
    2. I cant access the web.xml of the BPM suite, which has an unified portal web app to cater to all BPM projects, not in deployment environments atleast.
    3. I can use java classes by "cataloging" them, in the BPM poject.

Similar Threads

  1. Object Input Stream EOFException
    By FlyNn in forum New To Java
    Replies: 1
    Last Post: 12-18-2010, 12:33 PM
  2. Input stream error
    By Johnny68 in forum New To Java
    Replies: 10
    Last Post: 08-05-2010, 06:20 PM
  3. Stream closed on a ClassLoader input
    By RaistlinMajeren in forum Advanced Java
    Replies: 15
    Last Post: 06-03-2010, 07:18 AM
  4. standard input stream storing to a generic method?
    By vendetta in forum New To Java
    Replies: 3
    Last Post: 01-29-2010, 08:13 PM
  5. TCP/IP Client program halts at the read method of the stream
    By PradeepBadiger in forum Networking
    Replies: 0
    Last Post: 03-26-2009, 03:14 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •