2016-09-23 3 views
0

こんにちは私は、Javaでmultipart/form-dataでファイルを送信するための多くのサンプルコードを見てきました。 しかし、彼らはWriterとOutputStreamの両方を使用しています。 なぜ、それらのうちの1つだけを使用することはできませんか?ここで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

関連する問題