2016-09-23 64 views
0

嗨我見過很多示例代碼工作發送文件在Java中的multipart/form-data。 但他們已經使用了Writer和OutputStream。 他們爲什麼不能用它們中的一個?爲什麼在java中使用multipart/form-data發送文件時,我們必須同時使用Writer和OutputStream?

這裏是它們已將

import java.io.*; 
import java.net.HttpURLConnection; 
import java.net.URL; 

public class MainClass_External2 { 
    public static void main(String[] args){ 
     try{ 
      // Connect to the web server endpoint 
      URL serverUrl = 
       new URL("http://posttestserver.com/post.php?dir=example"); 
      HttpURLConnection urlConnection = (HttpURLConnection)serverUrl.openConnection(); 

      String boundaryString = "----SomeRandomText"; 
      String fileUrl = "abc.txt"; 
      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("\n\n--" + boundaryString + "\n"); 
      httpRequestBodyWriter.write("Content-Disposition: form-data; name=\"myFileDescription\""); 
      httpRequestBodyWriter.write("\n\n"); 
      httpRequestBodyWriter.write("Log file for 20150208"); 

// Include the section to describe the file 
      httpRequestBodyWriter.write("\n--" + boundaryString + "\n"); 
      httpRequestBodyWriter.write("Content-Disposition: form-data;" 
        + "name=\"myFile\";" 
        + "filename=\""+ logFileToUpload.getName() +"\"" 
        + "\nContent-Type: text/plain\n\n"); 
      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); 
      } 

// Mark the end of the multipart http request 
      httpRequestBodyWriter.write("\n--" + boundaryString + "--\n"); 
      httpRequestBodyWriter.flush(); 

// Close the streams 
      outputStreamToRequestBody.close(); 
      httpRequestBodyWriter.close(); 

      // 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); 
      } 
     }catch (Exception e){ 
      e.printStackTrace(); 
     } 
    } 
} 

回答

2

基本上示例代碼,所述響應包含文本和二進制數據,所以同時使用WriterOutputStream非常有意義。

作家只是包裝輸出流,並用於寫文本。輸出流本身用於寫入二進制數據。

爲什麼他們不能使用只使用其中之一?

僅使用OutputStream會使寫入文本更加痛苦。當需要編寫二進制數據時,僅使用Writer將不合適。

+0

感謝幫助 – Mohanakrrishna

相關問題