【问题标题】:How to post files using JSoup?如何使用 JSoup 发布文件?
【发布时间】:2011-09-10 09:13:59
【问题描述】:

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

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

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

【问题讨论】:

    标签: java post file-upload jsoup


    【解决方案1】:

    仅从 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。您最终可以获得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("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 1.12.1 发送 multipart/form-data 请求甚至可以在表单数据中不涉及文件的情况下进行。请参阅此处的示例:stackoverflow.com/a/63100270/363573
    【解决方案2】:

    接受的答案有效并且在撰写本文时是正确的,但从那时起 JSoup 已经发展并且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","info@tutorialswindow.com")            
        .data("file1", "filename", fs1)
        .method(Method.POST)
        .execute();
    

    【讨论】:

    【解决方案3】:

    这篇文章让我走上了正确的道路,但我不得不调整发布的答案以使我的用例正常工作。这是我的代码:

            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),然后将其用于文件的后续上传。

    希望对大家有所帮助。

    【讨论】:

      猜你喜欢
      • 2014-06-12
      • 1970-01-01
      • 2014-06-17
      • 1970-01-01
      • 1970-01-01
      • 2012-04-18
      • 2017-12-05
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多