如何使用JSoup发布文件? [英] How to post files using JSoup?
问题描述
Document document = Jsoup.connect(http:/ / (user,user,password,12345,email,info@tutorialswindow.com)。 )
.method(Method.POST)
.execute()
.parse();
现在我也要提交一个文件。就像一个带有文件字段的表单一样。
这可能吗?如果是,比如何?
通过新的
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客户端 。您最终可以将HTTP客户端响应作为 String
来获取,以便将其提供给 Jsoup#parse()
方法。 / p>
字符串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);
// ...
I`m using the following code post values using JSoup:
Document document = Jsoup.connect("http://www......com/....php")
.data("user","user","password","12345","email","info@tutorialswindow.com")
.method(Method.POST)
.execute()
.parse();
And now I want to submit a file, too. Like a form with a file field. Is this possible ? If is than how ?
This is only supported since Jsoup 1.8.2 (Apr 13, 2015)
via the new data(String, String, InputStream)
method.
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();
// ...
In older versions, sending multipart/form-data
requests is not supported. Your best bet is using a fullworthy HTTP client for this, such as Apache HttpComponents Client. You can ultimately get the HTTP client response as String
so that you can feed it to Jsoup#parse()
method.
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);
// ...
这篇关于如何使用JSoup发布文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!