2011-11-16 17 views
13

वापस आएं कृपया इसे समझें। कोड बिना किसी अपवाद के ठीक से चलता है।FtpClient storeFile हमेशा झूठी

 FTPClient ftp = new FTPClient(); 
     ftp.connect(server); 
     if(!ftp.login(username, password)) 
     { 
      ftp.logout(); 
      return false; 
     } 
     int reply = ftp.getReplyCode(); 
     if (!FTPReply.isPositiveCompletion(reply)) 
     { 
      ftp.disconnect(); 
      return false; 
     } 
     InputStream in = new FileInputStream(localfile); 
     ftp.setFileType(ftp.BINARY_FILE_TYPE, ftp.BINARY_FILE_TYPE); 
     ftp.setFileTransferMode(ftp.BINARY_FILE_TYPE); 
     Store = ftp.storeFile(destinationfile, in); 
     in.close(); 
     ftp.logout(); 
     ftp.disconnect(); 
    } 
    catch (Exception ex) 
    { 
     ex.printStackTrace(); 
     return false; 
    } 
    return Store; 

Buttttttttt

वापसी कथन हमेशा अवास्तविक लौटाते और फ़ाइल सर्वर पर अपलोड नहीं किया है। कृपया कोई इस पर मदद करें।

आपकी जानकारी के लिए, 1) मैं एक ऑफिस नेटवर्क में हूं। ---> क्या हमें कोई प्रॉक्सी जोड़ने की ज़रूरत है?


  File file = new File("C:\\Users\\sg0214273\\Desktop\\seagate\\seagate.txt"); 
      FileInputStream input = new FileInputStream(file); 
      client.setFileType(FTP.BINARY_FILE_TYPE); 
      if (!client.storeFile(file.getName(), input)) { 
      System.out.println("upload failed!"); 
      } 
      reply = client.getReplyCode(); 

      if(!FTPReply.isPositiveCompletion(reply)) { 
       System.out.println("upload failed!"); 
      } 



     Login success... 
     230 User ******** logged in. 
     upload failed!-----> is form boolean return value of storefile 
     upload failed!---------> is from replycode... 
     Logout from FTP server... 

कृपया मदद

+0

ftp.getReplyCode() वापस क्या करता है? Ftp.storeFile के बाद? –

+0

@ ग्रे नंबर।पहली वाक्य को देखें –

उत्तर

17

सटीक विफलता संदेश FtpClient#getReplyCode() फोन करके पाया जा सकता है। उस पृष्ठ से (मेरे जोर):

तुरंत जोड़ने के बाद ही वास्तविक समय आप जबाब कोड की जाँच करने के लिए (क्योंकि कनेक्ट प्रकार शून्य की है) की जरूरत है। के लिए सम्मेलन FTPClient में सभी एफ़टीपी कमांड विधियां ऐसी है कि वे या तो एक बूलियन मान या कुछ अन्य मान लौटाते हैं। बूलियन विधियां एफ़टीपी सर्वर से सफल समापन उत्तर पर सही होती हैं और पर झूठी प्रतिक्रिया होती है जिसके परिणामस्वरूप त्रुटि स्थिति या विफलता होती है। विधियों बूलियन के अलावा किसी अन्य मूल्य को वापस करने से एफ़टीपी कमांड द्वारा उत्पादित उच्च स्तरीय डेटा वाले मूल्य को वापस लौटाता है, या शून्य अगर त्रुटि त्रुटि या विफलता के परिणामस्वरूप शून्य हो जाती है। यदि आप सटीक एफ़टीपी उत्तर कोड को सफलता या विफलता के कारण एक्सेस करना चाहते हैं, तो आपको सफलता या विफलता के बाद getReplyCode पर कॉल करना होगा।

यह देखने के लिए कि रिटर्न कोड का अर्थ क्या है, आप Wikipedia: List of FTP server return codes देख सकते हैं।

+0

immedialty उत्तर के लिए धन्यवाद। समस्या बनी रहती है। फ़ाइल फ़ाइल = नई फ़ाइल ("सी: \\ उपयोगकर्ता \\ sg0214273 \\ डेस्कटॉप \\ seagate \\ seagate.txt"); \t फ़ाइल इनपुटस्ट्रीम इनपुट = नई फ़ाइल इनपुट स्ट्रीम (फ़ाइल); \t client.setFileType (FTP.BINARY_FILE_TYPE); \t यदि (! Client.storeFile (file.getName(), इनपुट)) { \t System.out.println ("अपलोड विफल हुआ!"); \t} \t उत्तर = client.getReplyCode(); \t \t अगर (FTPReply.isPositiveCompletion (जबाब)!) { \t \t \t println ("विफल रहा है अपलोड करें!"); \t \t} आउटपुट अपलोड विफल रहा! अपलोड विफल! – RaviKiran

+1

हां, लेकिन getReplyCode के * मूल्य * क्या है। यह एफ़टीपी उत्तर कोड है: http://en.wikipedia.org/wiki/List_of_FTP_server_return_codes देखें –

+0

getReplyCode का मान लॉगिन के बाद 230 है और स्टोरफाइल के बाद 550 है ... (immedialtly बाद में) – RaviKiran

5

विषय काफी पुराना है लेकिन शायद मैं किसी अन्य की मदद करूंगा। मैंने तुलना की कि फाइलज़िला क्या एफ़टीपी सर्वर भेजता है और मेरे कार्यक्रम ने किया। मैं(), यह काम करने के लिए ftp.pasv() को कोई अच्छा कोड :)

और डिबगिंग केवल getReplyCode() की तुलना में getReplyString() का उपयोग करने के लिए बेहतर है के लिए

2

आप संशोधित ftp.enterLocalPassiveMode उपयोग करने की आवश्यकता निष्क्रिय मोड में स्विच करने से पहले आप इस प्रकार storeFile() के साथ फाइल हस्तांतरण:

... 
ftp.setFileTransferMode(ftp.BINARY_FILE_TYPE); 
ftp.enterLocalPassiveMode();//Switch to passive mode 
Store = ftp.storeFile(destinationfile, in); 
in.close(); 
... 

आशा है कि मदद करता है।

+0

मुझे क्लाइंट कनेक्टिंग के रूप में अपाचे कॉमन्स एफ़टीपी का उपयोग करने में समस्या आई है विंडोज 7 डीवी मशीन से आईआईएस 7.5 पर एक परीक्षण विंडोज 7 एफटीपी सर्वर से। Ftp.storefile ने 501 का उत्तर कोड लौटाया (जो सिंटैक्स त्रुटि होने का दावा करता है)। फिक्स ftp.enterRemotePassiveMode() का उपयोग कर सर्वर और क्लाइंट दोनों के लिए निष्क्रिय मोड सेट करना था; ftp.enterLocalPassiveMode(); – MartinS

1

इस कोड के लिए अपाचे पुस्तकालय जोड़ने के लिए कृपया इस Impoted वर्ग

import org.apache.commons.net.ftp.FTP; 
import org.apache.commons.net.ftp.FTPClient; 
import org.apache.commons.net.ftp.FTPFile; 
import org.apache.commons.net.ftp.FTPReply; 

बाकी आयात वर्ग java.io से है या java.net

public boolean upload(String server,String username,String password,File localfile){ 
     boolean Store=false; 
     try{ 
     FTPClient ftp = new FTPClient(); 
       // ftp.connect(server); 
    /* you can use either code which is written above above or below code as ftp port 20 is used for the data transfer and port 21 is used for command and controlls */ 
      ftp.connect(InetAddress.getByName(server),21); 
    //here 'server' is your domain name of ftp server or url 
       if(!ftp.login(username, password)) 
       { 
        ftp.logout(); 
        return false; 
       } 
      ftp.sendNoOp();//used so server timeout exception will not rise 
       int reply = ftp.getReplyCode(); 
       if (!FTPReply.isPositiveCompletion(reply)) 
       { 
        ftp.disconnect(); 
        return false; 
       } 
       ftp.enterLocalPassiveMode(); /* just include this line here and your code will work fine */ 
       InputStream in = new FileInputStream(localfile); 
       // ftp.setFileType(ftp.BINARY_FILE_TYPE, ftp.BINARY_FILE_TYPE); 
       ftp.setFileType(FTP.BINARY_FILE_TYPE); 
       // ftp.setFileTransferMode(ftp.BINARY_FILE_TYPE); 
       Store = ftp.storeFile(destinationfile, in); 
       in.close(); 
      //ftp.disconnect(); 
    //here logout will close the connection for you 
       ftp.logout(); 

      } 
      catch (Exception ex) 
      { 
       ex.printStackTrace(); 
       return false; 
      } 
     return Store; 
    } 
+0

धन्यवाद नौसेना। ftp.enterLocalPassiveMode(); मेरे लिए वास्तव में सहायक है। – yaryan997

0

ftp.enterLocalPassiveMode इस्तेमाल करने की कोशिश कर रहे हैं(); ftp.storeFile से पहले (destinationfile, में);