Java 9 Httpclient模拟文件上传时如何设置name?

比如我们有这么一个表单:

<form action="/example/html5/demo_form.asp" method="post" 
enctype=”multipart/form-data”>
   <input type="file" name="img" />
   <input type="text" name=username" value="foo"/>
   <input type="submit" />
</form>

我想使用Java9的Httpclient模拟其提交,代码如下:

HttpClient client = HttpClient.newHttpClient();
HttpRequest request = HttpRequest.
        newBuilder(new URI("http:///example/html5/demo_form.asp"))
       .method("post",HttpRequest.BodyProcessor.fromString("foo"))
       .method("post", HttpRequest.BodyProcessor.fromFile(Paths.get("/path/to/your/file")))
       .build();
HttpResponse response = client.send(request, HttpResponse.BodyHandler.asString());
System.out.println(response.body());

不知道如何设置这俩字段的name

尚方宝剑之说
浏览 380回答 1
1回答
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java