Java – how to publish files using jsoup?
•
Java
I use jsoup to publish values using the following code:
Document document = Jsoup.connect("http://www......com/....PHP")
.data("user","user","password","12345","email","info@tutorialswindow.com")
.method(Method.POST)
.execute()
.parse();
Now I also want to submit a document Like a form with file fields Is that possible? What if so?
Solution
This only supports jsup 1.8 2 (April 13, 2015)
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();
// ...
Sending multipart / form data requests is not supported in older versions Your best choice is to use a full HTTP client, such as Apache httpcomponents client You can end up with the HTTP client response as a string so that you can provide it to the jsup#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);
// ...
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码
