自Jsoup 1.8.2(于2015年4月13日发布)起,可以通过新的data(String, String, InputStream)
方法来支持此功能。
String url = "http://www......com/....php";
File file = new File("/path/to/file.ext");
Document document = Jsoup.connect(url)
.data("user", "user")
.data("password", "12345")
.data("email", "info@tutorialswindow.com")
.data("file", file.getName(), new FileInputStream(file))
.post();
// ...
在旧版本中,不支持发送multipart/form-data
请求。你最好使用一个完整的HTTP客户端来进行此操作,例如Apache HttpComponents Client。最终,您可以将HTTP客户端响应作为String
获取,以便将其馈送到Jsoup#parse()
方法。
String url = "http://www......com/....php";
File file = new File("/path/to/file.ext");
MultipartEntity entity = new MultipartEntity();
entity.addPart("user", new StringBody("user"));
entity.addPart("password", new StringBody("12345"));
entity.addPart("email", new StringBody("info@tutorialswindow.com"));
entity.addPart("file", new InputStreamBody(new FileInputStream(file), file.getName()));
HttpPost post = new HttpPost(url);
post.setEntity(entity);
HttpClient client = new DefaultHttpClient();
HttpResponse response = client.execute(post);
String html = EntityUtils.toString(response.getEntity());
Document document = Jsoup.parse(html, url);