2016-03-22 26 views
0

FTP'ye birden çok dizin yüklemeye çalışıyorum ama 4-5 dakika yükledikten sonra bakıyorum 'Yazılım neden bağlantı iptalinde: recv başarısız oldu' istisna sadece birkaç dosya yüklendi. FTP Zaman aşımı ayarlarını kontrol ettim, 10 dakikaya ayarlandı ve hem enterLocalPassiveMode() hem de sendNoOp() yöntemini denedim ama işe yaramadı, lütfen bunu düzeltmeme yardım edin.FTP Dosya yükleme İstisna 'Yazılım bağlantı nedeniyle iptal edildi: recv başarısız oldu' -JAVA

public class FTPUpload { 


    public void initFtpupload(String localparentDir,String batchno) //not in use 
    { 
     try { 
      readProperty(); 
      CreateFile(); 
      localDirPath=localparentDir; 
      lotno=batchno; 
      FTPUpload ftpobj = new FTPUpload(ftpIp, ftpPort, ftpUname, ftpPassword); 
      System.out.println("Local Dir PAth:"+localparentDir); 
      System.out.println("Local dir path: "+localDirPath); 
      ftp.changeWorkingDirectory(ftpRootDir); 
      if(true) 
      { 
       IterateDirectory(localDirPath); 
      } 
      ftpobj.disconnect(); 
     } catch (Exception e) { 
      String error=stackTraceToString(e); 
      genLog("Exception in InitFtpUpload: "+e); 
      System.out.println("Exception:" + e); 
      genLog("Exception Trace:" + error); 
     } 
    } 

    // Constructor to connect to the FTP Server 
    public FTPUpload(String host, int port, String username, String password) throws Exception { 

     java.util.Date dtCurDate = new java.util.Date(); 
     SimpleDateFormat sdfDate = new SimpleDateFormat("dd_MM_yyyy_hh_mm_ss"); 
     String strCurDate = sdfDate.format(dtCurDate); 
     String strLogPath = System.getProperty("user.dir") + File.separator + "Log"; 
     String strLogFilePath = strLogPath + File.separator + "FTPConnectionLog_" + strCurDate + ".log"; 
     File logfile=new File(strLogFilePath); 

     ftp = new FTPClient(); 
     //ftp.addProtocolCommandListener(new PrintCommandListener(new PrintWriter(System.out))); 
     ftp.addProtocolCommandListener(new PrintCommandListener(new PrintWriter(logfile))); 
     int reply; 
     ftp.connect(host, port); 
     System.out.println("FTP URL is:" + ftp.getDefaultPort()); 
     reply = ftp.getReplyCode(); 
     if (!FTPReply.isPositiveCompletion(reply)) { 
      ftp.disconnect(); 
      genLog("Exception in connecting to FTP Server"); 
      throw new Exception("Exception in connecting to FTP Server"); 
     } 
     ftp.login(username, password); 
     ftp.setFileType(FTP.BINARY_FILE_TYPE); 
     ftp.enterLocalPassiveMode(); 
     genLog("Connected"); 
     System.out.println("Connected "); 

    } 

    // list the files in a specified directory on the FTP 
    public static boolean IterateDirectory(String localRootDirectoryPath) throws IOException { 
     // lists files and directories in the current working directory 
     //for test 
     ftp.changeWorkingDirectory(ftpRootDir); 
     System.out.println("Inside IterateDirectory "); 
      FTPFile dirs[]=ftp.listFiles(); 
      for(FTPFile f:dirs) 
      { 
       System.out.println("####"+f); 
      } 
      //upto this... 
     boolean verificationFilename = false; 
     String newFtpDirectoy = ""; 
     File localfile = new File(localRootDirectoryPath); 
     File files[] = localfile.listFiles(); 
     for (File file : files) { 
      String details = file.getName(); 
      if (file.isDirectory()) { 
       IterateDirectory(file.getPath()); 
       iflag=false; 
      } else { 
       if (iflag == false) { 
        localRootDirectoryPath = file.getParent(); 
        newFtpDirectoy = localRootDirectoryPath.substring(localRootDirectoryPath.indexOf("HUB")); 
        newFtpDirectoy = newFtpDirectoy.replaceAll("\\\\", "/"); 
        if (true) { 
          makeDirectories(ftp, newFtpDirectoy); 
         String localParenDir = file.getParent(); 
         genLog("ftpDirPath: "+ftpDirPath); 
         genLog("New FTP Dir: "+newFtpDirectoy); 
         genLog("Local parent dir: "+localParenDir); 
         System.out.println("ftpDirPath: "+ftpDirPath); 
         System.out.println("New FTP Dir: "+newFtpDirectoy); 
         System.out.println("Local parent dir: "+localParenDir); 
         uploadDirectory(ftp, ftpDirPath, localParenDir, newFtpDirectoy); 
         mailist.add(localRootDirectoryPath); 
         File f=new File(localRootDirectoryPath); 
         String finalPath=f.getParent(); 
         if(!mailistlocal.contains(finalPath)){ 
          mailistlocal.add(finalPath); 
         } 
        } 
        iflag = true; 
       } 
      } 
     } 
     return true; 
    } 

    // Disconnect the connection to FTP 

    public void disconnect() { 
     if (this.ftp.isConnected()) { 
      try { 
       this.ftp.logout(); 
       this.ftp.disconnect(); 
      } catch (IOException f) { 
       // do nothing as file is already saved to server 
      } 
      System.out.println("FTP Disconnected successfully"); 
      genLog("FTP Disconnected successfully"); 
     } 
    } 


      } 
     } 
    } 

    } 
+0

Sen SFTP kullanarak düşünebilirsiniz. Buraya bir göz atın, http://www.jcraft.com/jsch/ – atom

cevap

0

Kendi kütüphanenizi oluşturmak yerine mevcut bir kütüphaneyi kullanmayı düşünebilirsiniz. https://commons.apache.org/proper/commons-net/

https://commons.apache.org/proper/commons-net/javadocs/api-1.4.1/org/apache/commons/net/ftp/FTPClient.html

EDIT'e (Katma çalışma örneği):

Bu kodu bulundu Kavanoz dosyasını kullanarak benim için çalıştı: Örneğin https://commons.apache.org/proper/commons-net/download_net.cgi

import java.io.File; 
import java.io.FileInputStream; 
import java.io.InputStream; 

import org.apache.commons.net.tftp.TFTP; 
import org.apache.commons.net.tftp.TFTPClient; 

public class FtpExample { 

    public static void main(String[] args) throws Exception { 
     String fileName = "C:\\TEMP\\MyTestFile.txt"; 
     String hostName = "speedtest.tele2.net"; 
     String remoteFileName = "upload/MyTestFile.txt"; 
     File file = new File(fileName); 
     System.out.println("Sending file: " + file.getName() + "(exists: " + file.exists() + ")"); 
     sendFile(file, hostName, remoteFileName); 
     System.out.println("Done!"); 
    } 

    private static void sendFile(File file, String hostName, String remoteFileName) { 
     TFTPClient tftp = null; 
     InputStream inputStream = null; 
     try { 
      inputStream = new FileInputStream(file); 
      tftp = new TFTPClient(); 
      tftp.open(); 
      tftp.sendFile(remoteFileName, TFTP.BINARY_MODE, inputStream, hostName); 
     } catch (Exception exp) { 
      throw new RuntimeException(exp); 
     } finally { 
      try { 
       if (tftp != null) { 
        tftp.close(); 
       } 
      } finally { 
       if (inputStream != null) { 
        try { 
         inputStream.close(); 
        } catch (Exception exp) { 
         throw new RuntimeException(exp); 
        } 
       } 
      } 
     } 
    } 

} 
+0

Kullandığım kitaplık (org.apache.commons.net.ftp) @John – Shoban

İlgili konular