2011-09-10 38 views
12

I`m sử dụng các giá trị mã bưu điện sau sử dụng JSoup:Cách đăng tệp bằng JSoup?

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

Và bây giờ tôi muốn gửi một tập tin, quá. Giống như một biểu mẫu có trường tệp. Điều này có khả thi không? Nếu là hơn như thế nào?

Trả lời

14

Điều này chỉ được hỗ trợ kể từ Jsoup 1.8.2 (ngày 13 tháng 4 năm 2015) thông qua phương thức data(String, String, InputStream) mới.

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(); 
// ... 

Trong các phiên bản cũ hơn, yêu cầu gửi multipart/form-data không được hỗ trợ. Đặt cược tốt nhất của bạn là sử dụng ứng dụng HTTP đủ điều kiện cho điều này, chẳng hạn như Apache HttpComponents Client. Cuối cùng, bạn có thể nhận được phản hồi của ứng dụng khách HTTP là String để bạn có thể đưa nó đến phương thức 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); 
// ... 
4

Các công trình trả lời chấp nhận và là chính xác tại thời điểm viết bài, nhưng kể từ đó JSoup đã phát triển và 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(); 
0

bài này dẫn tôi đến con đường đúng đắn nhưng tôi đã phải chỉnh đăng câu trả lời để làm cho trường hợp sử dụng của tôi hoạt động. Dưới đây là mã của tôi:

 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(); 

Sự khác biệt cơ bản là tôi lần đầu tiên đăng nhập vào trang web, giữ lại các cookie từ các phản ứng (conn) và sau đó sử dụng nó cho việc tải lên tiếp theo của tập tin.

Hy vọng nó sẽ giúp mọi người.

Các vấn đề liên quan