commons-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Francesco Fazzini <ffrancesc...@gmail.com>
Subject [Net] problem in FTP file retrieving
Date Wed, 17 Nov 2010 14:42:32 GMT
Hello,

I am using "commons-net" version "2.0". I need to retrieve more than one
file using the same FTP connection.
I tried with 2 different API: boolean retrieveFile(String remote,
OutputStream local), InputStream retrieveFileStream(String remote) on more
than one FTP server(so it is reproducible from my side)
only the first works correctly(I took a look to the library source code,
they should have a different implementation)...

The question is: What is missing in my code in order that both could work
correctly? Do I have to manage something else? Is something wrong?

//WORKS WELL

//OUTPUT
<15-11-2010 13:12:20> <INFO> - anonymous
<15-11-2010 13:12:20> <INFO> -
<15-11-2010 13:12:20> <INFO> - login succesful... true
<15-11-2010 13:12:20> <INFO> - 230 User anonymous logged in.
<15-11-2010 13:12:20> <INFO> - 230 User anonymous logged in.
now downloading...first file
<15-11-2010 13:12:23> <INFO> - 226 Transfer complete.
now downloading...second file
<15-11-2010 13:12:25> <INFO> - 226 Transfer complete.
now downloading...third file
<15-11-2010 13:12:28> <INFO> - 226 Transfer complete.
now downloading...fourth file
<15-11-2010 13:12:30> <INFO> - 226 Transfer complete.
now downloading...fifth file
<15-11-2010 13:12:33> <INFO> - 226 Transfer complete.

// snippet code used
  OutputStream outputStream = null;
  try{
   ftpClient.enterLocalPassiveMode();
   logger.info(ftpClient.getReplyString());
   //String remoteFile = UriUtil.getRemoteFilePathFromUri(uri);
   outputStream = new FileOutputStream(newLocalFilePath);
   for(String remoteFilePath : remoteFilePaths){
    System.out.println("now downloading... " + remoteFilePath);
     ftpClient.retrieveFile(remoteFilePath,outputStream);
    logger.info(ftpClient.getReplyString());
   }
  }
  finally {
   outputStream.close();
  }


//DOESN' T WORK

//OUTPUT

<15-11-2010 13:41:25> <INFO> - anonymous
<15-11-2010 13:41:25> <INFO> -
<15-11-2010 13:41:26> <INFO> - login succesful... true
<15-11-2010 13:41:26> <INFO> - 230 User anonymous logged in.
<15-11-2010 13:41:26> <INFO> - 230 User anonymous logged in.
now downloading... first file
<15-11-2010 13:41:26> <INFO> - 150 Opening ASCII mode data connection for
/goldenPath/hg18/UCSCGenes/uniProtToUcscGenes.txt (1007617 bytes)
now downloading...second file
<15-11-2010 13:41:29> <INFO> - 226 Transfer complete.
now downloading... third file
<15-11-2010 13:41:29> <ERROR> - Connection refused
java.net.ConnectException: Connection refused
        at java.net.PlainSocketImpl.socketConnect(Native Method)
        at java.net.PlainSocketImpl.doConnect(PlainSocketImpl.java:333)
        at
java.net.PlainSocketImpl.connectToAddress(PlainSocketImpl.java:195)
        at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:182)
        at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:366)
        at java.net.Socket.connect(Socket.java:529)
        at java.net.Socket.connect(Socket.java:478)
        at java.net.Socket.<init>(Socket.java:375)
        at java.net.Socket.<init>(Socket.java:189)
        at
javax.net.DefaultSocketFactory.createSocket(SocketFactory.java:206)
        at
org.apache.commons.net.ftp.FTPClient._openDataConnection_(FTPClient.java:502)
        at
org.apache.commons.net.ftp.FTPClient.retrieveFileStream(FTPClient.java:1333)


//code snippet used

InputStream inputStream = null;
  OutputStream outputStream = null;
  try{
   ftpClient.enterLocalPassiveMode();
   logger.info(ftpClient.getReplyString());
   //String remoteFile = UriUtil.getRemoteFilePathFromUri(uri);
   outputStream = new FileOutputStream(newLocalFilePath);
   for(String remoteFilePath : remoteFilePaths){
    System.out.println("now downloading... " + remoteFilePath);
    inputStream = ftpClient.retrieveFileStream(remoteFilePath);
    logger.info(ftpClient.getReplyString());
    if(inputStream != null){
     isDownloaded=true;
     int bytesRead = -1;
     final byte[] buffer = new byte[4096];
     while ((bytesRead = inputStream.read(buffer)) != -1) {
      outputStream.write(buffer, 0, bytesRead);
     }
     inputStream.close();
    }
   }
  }
  finally {
   outputStream.close();
  }


Thanks in advance.

Best regards,
Francesco

Mime
  • Unnamed multipart/alternative (inline, None, 0 bytes)
View raw message