を使用して、あなたが望むものを実装するために、次のコードを見つけることができます。文書は2.5.Xのプレイバージョンです 注
import play.mvc.Http.MultipartFormData.*;
//the file you want to post
Source<ByteString, ?> file = FileIO.fromFile(new File("hello.txt"));
//generate the right format for posting
FilePart<Source<ByteString, ?>> fp = new FilePart<>("hello", "hello.txt", "text/plain", file);
DataPart dp = new DataPart("key", "value");// the data you want to post
ws.url(url).post(Source.from(Arrays.asList(fp, dp)));
更新:あなたが知っておくべき 最初はws
がcom.ning.http.AsyncHttpClient
上に構築されていることです。 Play Documentを参照して、 ws
のplay 2.4.*
は、マルチパート形式のアップロードを直接サポートしていません。基になるクライアントAsyncHttpClient
をRequestBuilder.addBodyPartと使用することができます。次のコードは、あなたが
import com.ning.http.client.AsyncHttpClient
import com.ning.http.client.multipart.FilePart
AsyncHttpClient myClient = ws.getUnderlying();
FilePart myFilePart = new FilePart("myFile", new java.io.File("test.txt"))
myClient.preparePut("http://localhost:9000/index").addBodyPart(filePart).execute.get()
は幸運
を持って欲しいもの履行することができますが、同じリクエストでフォームデータファイルを送信しようとしていますか?それとも、ファイルを提出したいだけですか? – Salem
"同じ要求でフォームデータとファイルを送信しようとしています" - はい。 郵便配達員とちょうど同じ – Anirudh