play ウェブサイトで、必要なものを実装するための次のコードを見つけることができます。ドキュメントは 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.*
はマルチパート フォームのアップロードを直接サポートしていません。RequestBuilder.addBodyPartで基礎となるクライアントAsyncHttpClient
を使用できます。次のコードは、あなたが望むものを満たすことができます
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()
幸運を祈ります