2011-09-10 12 views
12

मैं `JSoup का उपयोग कर निम्न कोड पद मानों का उपयोग:JSoup का उपयोग कर फ़ाइलों को कैसे पोस्ट करें?

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

और अब मैं एक फ़ाइल, भी प्रस्तुत करना चाहते हैं। फ़ाइल फ़ील्ड के साथ एक फॉर्म की तरह। क्या यह संभव है? अगर से है तो कैसे?

उत्तर

14

यह केवल data(String, String, InputStream) विधि के माध्यम से Jsoup 1.8.2 (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", "[email protected]") 
    .data("file", file.getName(), new FileInputStream(file)) 
    .post(); 
// ... 

पुराने संस्करणों में, multipart/form-data अनुरोध भेजना समर्थित नहीं है। आपकी सर्वश्रेष्ठ शर्त इस के लिए एक पूर्ण HTTP HTTP क्लाइंट का उपयोग कर रही है, जैसे कि Apache HttpComponents Client। आप अंततः HTTP क्लाइंट प्रतिक्रिया String के रूप में प्राप्त कर सकते हैं ताकि आप इसे 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

स्वीकार किए जाते हैं जवाब काम करता है और लेखन के समय सही था, लेकिन उसके बाद से 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","[email protected]")    
    .data("file1", "filename", fs1) 
    .method(Method.POST) 
    .execute(); 
0

इस पोस्ट में मुझे सही रास्ते के लिए नेतृत्व किया है, लेकिन मैं बदलाव करने के लिए किया था मेरे उपयोग केस काम करने के लिए उत्तर पोस्ट किया। यहाँ मेरी कोड है:

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

बुनियादी अंतर यह है कि मैं पहली बार, साइट के लिए लॉग इन प्रतिक्रिया (conn) से कुकी को बनाए रखने और फिर फ़ाइल के बाद अपलोड करने के लिए इसका इस्तेमाल करते हैं है।

आशा है कि यह लोगों की मदद करे।

संबंधित मुद्दे