素材牛VIP会员
Java 9 Httpclient模拟文件上传时如何设置name?
 su***se  分类:Java代码  人气:822  回帖:1  发布于6年前 收藏

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

<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

 标签:java9java

讨论这个帖子(1)垃圾回帖将一律封号处理……

Lv3 码奴
雨***梦 页面重构设计 6年前#1

同时看了你在sof上的提问:

https://stackoverflow.com/que...

查了下最新的Javadoc,感到无解。

换句话说,Java 9 的 HttpClient 似乎还不支持用简洁的方式POST表单或文件。

要用 Java 9 的 HttpClient,就看你怎么理解这句话了:

Incubating Feature. Will be removed in a future release.

 文明上网,理性发言!   😉 阿里云幸运券,戳我领取