Search code examples
javasocketsbrowseruploadserversocket

Java Server using SOCKET to download a file upload to it via a browser on the same machine1


I have a java server using sockets. I have an html file which contains a form that can be used to upload a file and send it. How can the server download that file without blocking. The browser never closes the connection and it just hangs in there I have beeping using readLine() from the input stream but it blocks. is there any way around this?

I appreciate any help here. Thanks


Solution

  • please clarify your requirement, it seems total chaos in your steps.

    1. did your program run as client side or server side?
    2. could you show your code? and show what blocked you?
    3. It is much better if you can draw a working-flow of your program .

    Thanks for your supplement, do you want to upload a files through HTTP(writing in socket way). For this requirement, you could check the link link for how HTTP works [RFC 1867] (https://www.rfc-editor.org/rfc/rfc1867)

    Below is the package of how socket way do: Suppose the server supplies the following HTML:

     <FORM ACTION="http://server.dom/cgi/handle"
           ENCTYPE="multipart/form-data"
           METHOD=POST>
     What is your name? <INPUT TYPE=TEXT NAME=submitter>
     What files are you sending? <INPUT TYPE=FILE NAME=pics>
     </FORM>
    

    and the user types "Joe Blow" in the name field, and selects a text file "file1.txt" for the answer to 'What files are you sending?'

    The client might send back the following data:

        Content-type: multipart/form-data, boundary=AaB03x
    
        --AaB03x
        content-disposition: form-data; name="field1"
    
        Joe Blow
        --AaB03x
        content-disposition: form-data; name="pics"; filename="file1.txt"
        Content-Type: text/plain
    
         ... contents of file1.txt ...
        --AaB03x--
    

    If the user also indicated an image file "file2.gif" for the answer to 'What files are you sending?', the client might client might send back the following data:

        Content-type: multipart/form-data, boundary=AaB03x
    
        --AaB03x
        content-disposition: form-data; name="field1"
    
        Joe Blow
        --AaB03x
        content-disposition: form-data; name="pics"
        Content-type: multipart/mixed, boundary=BbC04y
    
        --BbC04y
        Content-disposition: attachment; filename="file1.txt"
    

    Nebel & Masinter Experimental [Page 9]

    RFC 1867 Form-based File Upload in HTML November 1995

        Content-Type: text/plain
    
        ... contents of file1.txt ...
        --BbC04y
        Content-disposition: attachment; filename="file2.gif"
        Content-type: image/gif
        Content-Transfer-Encoding: binary
    
          ...contents of file2.gif...
        --BbC04y--
        --AaB03x--