如何使用 JSoup 发布文件?

发布于 2024-12-03 21:38:04 字数 494 浏览 2 评论 0原文

我正在使用 JSoup 使用以下代码发布值:

Document document = Jsoup.connect("http://www......com/....php")
                    .data("user","user","password","12345","email","[email protected]")
                    .method(Method.POST)
                    .execute()
                    .parse();

现在我也想提交一个文件。就像带有文件字段的表单一样。 这可能吗?如果是比怎么样?

I`m using the following code post values using JSoup:

Document document = Jsoup.connect("http://www......com/....php")
                    .data("user","user","password","12345","email","[email protected]")
                    .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 ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

不顾 2024-12-10 21:38:04

仅自 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", "[email protected]")
    .data("file", file.getName(), new FileInputStream(file))
    .post();
// ...

在旧版本中,不支持发送 multipart/form-data 请求。最好的选择是使用一个完整的 HTTP 客户端,例如 Apache HttpComponents Client。您最终可以获得 String 形式的 HTTP 客户端响应,以便可以将其提供给 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("[email protected]"));
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);
// ...

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", "[email protected]")
    .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("[email protected]"));
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);
// ...
痴梦一场 2024-12-10 21:38:04

接受的答案有效并且在撰写本文时是正确的,但从那时起 JSoup 已经发展并且 自版本 1.8.2 起,可以将文件作为多部分表单的一部分发送

File file1 = new File("/path/to/file");
FileInputStream fs1 = new FileInputStream(file1);

Connection.Response response = Jsoup.connect("http://www......com/....php")
    .data("user","user","password","12345","email","[email protected]")            
    .data("file1", "filename", fs1)
    .method(Method.POST)
    .execute();

The accepted answer works and was correct at the time of writing, but since then JSoup has evolved and since version 1.8.2 it is possible to send files as part of multipart forms:

File file1 = new File("/path/to/file");
FileInputStream fs1 = new FileInputStream(file1);

Connection.Response response = Jsoup.connect("http://www......com/....php")
    .data("user","user","password","12345","email","[email protected]")            
    .data("file1", "filename", fs1)
    .method(Method.POST)
    .execute();
清泪尽 2024-12-10 21:38:04

这篇文章引导我走上了正确的道路,但我必须调整发布的答案才能使我的用例发挥作用。这是我的代码:

        FileInputStream fs = new FileInputStream(fileToSend);
        Connection conn = Jsoup.connect(baseUrl + authUrl)
                .data("username",username)
                .data("password",password);
        Document document = conn.post();

        System.out.println("Login successfully! Session Cookie: " + conn.response().cookies());


        System.out.println("Attempting to upload file...");
        document = Jsoup.connect(baseUrl + uploadUrl)
                .data("file",fileToSend.getName(),fs)
                .cookies(conn.response().cookies())
                .post();

基本区别是我首先登录站点,保留响应中的 cookie (conn),然后将其用于后续的文件上传。

希望对大家有帮助。

This post led me to the right path but I had to tweak the posted answers to make my use case work. Here's my code:

        FileInputStream fs = new FileInputStream(fileToSend);
        Connection conn = Jsoup.connect(baseUrl + authUrl)
                .data("username",username)
                .data("password",password);
        Document document = conn.post();

        System.out.println("Login successfully! Session Cookie: " + conn.response().cookies());


        System.out.println("Attempting to upload file...");
        document = Jsoup.connect(baseUrl + uploadUrl)
                .data("file",fileToSend.getName(),fs)
                .cookies(conn.response().cookies())
                .post();

The basic difference is that I first login to the site, retain the cookie from the response (conn) and then use it for the subsequent upload of the file.

Hope it helps guys.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文