2016-06-29 8 views
0

私はHttpURLConnectionを使ってpostメソッドでファイルを送信しています。私はファイルと共に 'student_id'というパラメータを送信しています。各投稿要求で1つのファイルを送信するとき、コードは正常に動作しています。 しかし、どのようにすべてのファイルが同じ 'student_id'に属している1つの投稿要求で複数のファイルを送信するために、以下のコードを更新できますか?1つの投稿に複数のファイルを送信するJAXのHttpURLConnectionのリクエスト

 try{ 
      File textFile2 = new File("info.txt"); 

     URL url = new URL("htttp://wwww.students.com"); 
     HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();   
     urlConnection.setChunkedStreamingMode(0); 
     urlConnection.setDoOutput(true); 


     String boundary = Long.toHexString(System.currentTimeMillis()); // Just generate some unique random value. 

     urlConnection.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + boundary);   
     OutputStream output = urlConnection.getOutputStream(); 
     PrintWriter writer = new PrintWriter(new OutputStreamWriter(output, charset), true); 

     writer.append("--" + boundary).append(CRLF); 
     writer.append("Content-Disposition: form-data; name=\"student_id\"").append(CRLF); 
     writer.append("Content-Type: text/plain; charset=" + charset).append(CRLF); 
     writer.append(CRLF).append("25").append(CRLF).flush(); 


     writer.append("--" + boundary).append(CRLF); 
     writer.append("Content-Disposition: form-data; name=\"newfile\"; filename=\"" + textFile2.getName() + "\"").append(CRLF); 
     writer.append("Content-Type: text/plain; charset=" + charset).append(CRLF); // Text file itself must be saved in this charset! 
     writer.append(CRLF).flush();                
     Files.copy(textFile2.toPath(), output);//copies all bytes in a file to the output stream 
     output.flush(); // Important before continuing with writer! 
     writer.append(CRLF).flush(); 

     writer.append("--" + boundary + "--").append(CRLF).flush(); 
     InputStream responseStream; 

     } catch (MalformedURLException e) { 
      e.printStackTrace(); 
     } catch (IOException e) { 

      e.printStackTrace(); 
     } 

私は「NEWFILE」でパラメータを指定して「複数」を追加しようとしましたが、それは

 writer.append("--" + boundary).append(CRLF); 
    writer.append("Content-Disposition: form-data; name=\"newfile\" multiple; filename=\"" + textFile2.getName() + "\"").append(CRLF); 
    writer.append("Content-Type: text/plain; charset=" + charset).append(CRLF); // Text file itself must be saved in this charset! 
    writer.append(CRLF).flush();                
    Files.copy(textFile2.toPath(), output);//copies all bytes in a file to the output stream 
    output.flush(); // Important before continuing with writer! 
    writer.append(CRLF).flush(); 

    writer.append("--" + boundary + "--").append(CRLF).flush(); 
+0

元のOutputStreamに直接書き込むOutputStreamのラッパーを混在させると、問題が発生することがあります。 PrintWriterの代わりにPrintStreamを使用し、そのPrintStreamをFiles.copyに渡します。 OutputStreamをPrintStreamやOutputStreamWriterのようにラップしたら、そのOutputStreamを再び参照しないでください。 – VGR

+0

可能であれば、あなたが提案しているものの例を教えてください。 – ryh12

+0

'PrintWriter writer = ...'を 'PrintStream writer = new PrintStream(output、true、charset);に変更してください。 Files.copy文を 'Files.copy(textFile2.toPath()、writer);に変更してください。そうすれば、すべての行がまったく同じ出力オブジェクトに書き込まれます。 output.flush()への呼び出しを削除します。 'writer.close()'を呼び出すことを忘れないでください。 – VGR

答えて

0

が動作していないが、あなたがして1つのフォームフィールドmultipart/form-dataリクエストを投稿しようとしているようですnameパラメータはstudent_idであり、複数のファイルをアップロードする場合はファイル部分です。

各ファイルを別々の部分で提供することによって複数のファイルを送信することができますが、すべてが同じnameパラメータを使用します。例えば

、あなたがnewfilenameパラメータで最初のファイル部分を送信することにより、textFile1のファイルをアップロードすることができます

その後
writer.append("--" + boundary).append(CRLF); 
writer.append("Content-Disposition: form-data; name=\"newfile\"; filename=\"" + textFile1.getName()+ "\"").append(CRLF); 
writer.append("Content-Type: text/plain; charset=" + charset).append(CRLF); 
writer.append("Content-Transfer-Encoding: binary").append(CRLF); 
writer.append(CRLF); 
writer.flush(); 

FileInputStream inputStream = new FileInputStream(textFile1); 
byte[] buffer = new byte[4096]; 
int bytesRead = -1; 
while ((bytesRead = inputStream.read(buffer)) != -1) { 
    outputStream.write(buffer, 0, bytesRead); 
} 
outputStream.flush(); 
inputStream.close(); 

writer.append(CRLF); 
writer.flush(); 

、あなたはファイルの一部を送信することにより、textFile2の別のファイルをアップロードすることができます同じnameパラメータはnewfile

writer.append("--" + boundary).append(CRLF); 
writer.append("Content-Disposition: form-data; name=\"newfile\"; filename=\"" + textFile2.getName()+ "\"").append(CRLF); 
writer.append("Content-Type: text/plain; charset=" + charset).append(CRLF); 
writer.append("Content-Transfer-Encoding: binary").append(CRLF); 
writer.append(CRLF); 
writer.flush(); 

FileInputStream inputStream = new FileInputStream(textFile2); 
byte[] buffer = new byte[4096]; 
int bytesRead = -1; 
while ((bytesRead = inputStream.read(buffer)) != -1) { 
    outputStream.write(buffer, 0, bytesRead); 
} 
outputStream.flush(); 
inputStream.close(); 

writer.append(CRLF); 
writer.flush(); 

ご覧のとおり、アップロードするファイルを除いてコードはほぼ同じです。コードをメソッドに入れて、ファイルの各部分をに送信することをお勧めします。

+0

私はstudent_idという2つのフォームフィールドを持つmultipart/form-dataリクエストを送信しようとしています.2番目のフィールドはファイルを保持する 'newfile'です。したがって、名前フィールド 'newfile'をフォーム – ryh12

+0

@ ryh12私はそれを例で更新しました。 – Wilson

+0

私はあなたの方法を試しましたが、最初のファイルだけを追加して、他のファイルを無視しています。@Wilson – ryh12

関連する問題