• How to upload a file via a HTTP multipart request in Java without using any external libraries


     

    There was this situation when there was a need for my applet to send some log files (generated by some desktop application) on the remote clients.

    To keep my applet lean, I chose to implement this file upload function by sending a HTTP multipart request when my applet loads on the remote client’s browser. Policies were in place to ensure that my applet was able to read the log files and send them back to a web server which will collect the log files.

    This post documents how I can upload a file by sending a HTTP multipart request in Java without using any external libraries. For the sake of brevity, I used the server endpoint that I had discussed earlier to accept the file from the codes that will be mentioned in this post.

    Having an idea of what the HTTP multipart request will look like

    One way to know how a HTTP multipart request will look like will be to capture the HTTP multipart request that browsers send web servers. To instruct my browser to show me how my Java program should send the HTTP multipart request, I first create the following HTML code:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    <html>
    <body>
    <form action="http://ipv4.fiddler/test/GetPostRequest.php"
    method="post"
    enctype="multipart/form-data">
    <p>
    <strong>My file description:</strong>
    <textarea name="myFileDescription" rows="2" cols="20">
    </textarea><br/> <br/>
    <strong>My file:</strong><br/>
    <input type="file" name="myFile">
    </p>
    <input type="submit" value = "Submit">
    </form>
    </body>
    </html>

    And then point my browser to the HTML file. I then use the HTML form to upload a file and have Fiddler examine the HTTP multipart request.

    Fiddler showed me the following:

    POST http://127.0.0.1/GetPostRequest.php 
    HTTP/1.1 Host: 127.0.0.1 
    Connection: keep-alive 
    Referer: http://localhost/GetPostRequest.php 
    Content-Length: 864 
    Cache-Control: max-age=0 
    Origin: http://localhost 
    User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/534.30 (KHTML, like Gecko) Chrome/12.0.742.91 Safari/534.30 
    Content-Type: multipart/form-data; boundary=----WebKitFormBoundaryX6nBO7q27yQ1JNbb 
    Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8 
    Accept-Encoding: gzip,deflate,sdch 
    Accept-Language: en-US,en;q=0.8 
    Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.3
     
    ------WebKitFormBoundaryX6nBO7q27yQ1JNbb 
    Content-Disposition: form-data; name="myFileDescription" 
    
    Log file for 20150208. 
    ------WebKitFormBoundaryX6nBO7q27yQ1JNbb 
    Content-Disposition: form-data; name="myFile"; filename="20150208.log" 
    Content-Type: text/plain
    
    file contents... 
    
    ------WebKitFormBoundaryX6nBO7q27yQ1JNbb--

    Constructing a HTTP multipart request to upload the file to the web server endpoint

    With the output from Fiddler, sending the HTTP multipart request with my Java program is straightforward. To avoid using external libraries, I use the following classes provided by the Java standard library:

    • java.io.BufferedReader
      java.io.BufferedWriter
      java.io.File
      java.io.FileInputStream
      java.io.InputStreamReader
      java.io.OutputStream
      java.io.OutputStreamWriter
      java.net.HttpURLConnection
      java.net.URL
      

        

    And wrote the following codes to upload my log file to my web server endpoint with a HTTP multipart request.

    // Connect to the web server endpoint
    URL serverUrl =
        new URL("http://localhost/test/GetPostRequest.php");
    HttpURLConnection urlConnection = (HttpURLConnection) serverUrl.openConnection();
     
    String boundaryString = "----SomeRandomText";
    String fileUrl = "/logs/20150208.log";
    File logFileToUpload = new File(fileUrl);
     
    // Indicate that we want to write to the HTTP request body
    urlConnection.setDoOutput(true);
    urlConnection.setRequestMethod("POST");
    urlConnection.addRequestProperty("Content-Type", "multipart/form-data; boundary=" + boundaryString);
     
    // Indicate that we want to write some data as the HTTP request body
    urlConnection.setDoOutput(true);
     
    OutputStream outputStreamToRequestBody = urlConnection.getOutputStream();
    BufferedWriter httpRequestBodyWriter =
        new BufferedWriter(new OutputStreamWriter(outputStreamToRequestBody));
     
    // Include value from the myFileDescription text area in the post data
    httpRequestBodyWriter.write("
    --" + boundaryString + "
    ");
    httpRequestBodyWriter.write("Content-Disposition: form-data; name="myFileDescription"");
    httpRequestBodyWriter.write("
    
    ");
    httpRequestBodyWriter.write("Log file for 20150208");
     
    // Include the section to describe the file
    httpRequestBodyWriter.write("
    --" + boundaryString + "
    ");
    httpRequestBodyWriter.write("Content-Disposition: form-data;"
            + "name="myFile";"
            + "filename=""+ logFileToUpload.getName() +"""
            + "
    Content-Type: text/plain
    
    ");
    httpRequestBodyWriter.flush();
     
    // Write the actual file contents
    FileInputStream inputStreamToLogFile = new FileInputStream(logFileToUpload);
     
    int bytesRead;
    byte[] dataBuffer = new byte[1024];
    while((bytesRead = inputStreamToLogFile.read(dataBuffer)) != -1) {
        outputStreamToRequestBody.write(dataBuffer, 0, bytesRead);
    }
     
    outputStreamToRequestBody.flush();
     
    // Mark the end of the multipart http request
    httpRequestBodyWriter.write("
    --" + boundaryString + "--
    ");
    httpRequestBodyWriter.flush();
     
    // Close the streams
    outputStreamToRequestBody.close();
    httpRequestBodyWriter.close();
    

      

    Getting the HTTP response from the web server

    After writing the payload for the HTTP multipart request to the output stream of the HttpURLConnection object, I would then read the response from my web server endpoint.

    By using the HttpURLConnection object to read the HTTP response, I also trigger the actual uploading of the log file via a HTTP multipart request to the web server.

    // Read response from web server, which will trigger the multipart HTTP request to be sent.
    BufferedReader httpResponseReader =
        new BufferedReader(new InputStreamReader(urlConnection.getInputStream()));
    String lineRead;
    while((lineRead = httpResponseReader.readLine()) != null) {
        System.out.println(lineRead);
    }
    

      

  • 相关阅读:
    24、合并两个有序链表
    23、反转链表
    22、删除链表的倒数第N个节点
    21、删除链表中的节点
    18、实现strStr()
    17、字符串转换整数 (atoi)
    15、有效的字母异位词
    16、验证回文字符串
    14、字符串中的第一个唯一字符
    mybatis入门(七)----延迟加载
  • 原文地址:https://www.cnblogs.com/hephec/p/4570577.html
Copyright © 2020-2023  润新知