You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@commons.apache.org by Francesco Fazzini <ff...@gmail.com> on 2010/11/17 15:42:32 UTC

[Net] problem in FTP file retrieving

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

Re: [Net] problem in FTP file retrieving

Posted by Francesco Fazzini <ff...@gmail.com>.
Hi Daniel,

I put the "ftpClient.completePendingCommand();"
after each "inpustream" closing. Now "retreveFileStream" works as well the
other API "retrieveFile".
Thanks.

Francesco
2010/11/18 Daniel F. Savarese <df...@savarese.org>

>
> In message <AA...@mail.gmail.com>>,
> Fran
> cesco Fazzini writes:
> >After the last server reply, the program is still running. The program
> >execution is pending during "ftpClient.completePendingCommand();"
> >invocation.
>
> You must call completePendingCommand after completing the file transfer.
> See the ftp.java example in the source distribution.
>
> daniel
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@commons.apache.org
> For additional commands, e-mail: user-help@commons.apache.org
>
>

Re: [Net] problem in FTP file retrieving

Posted by Francesco Fazzini <ff...@gmail.com>.
 Hi Pablo,

I had already tried, but the program execution doesn't go on after the first
download:

<17-11-2010 15:36:06> <INFO> - 220 FTP Server ready.
<17-11-2010 15:36:07> <INFO> - 230 User anonymous logged in.
now downloading... first file
<17-11-2010 15:36:07> <INFO> - 150 Opening ASCII mode data connection
for "filename" (1007617 bytes)

After the last server reply, the program is still running. The program
execution is pending during "ftpClient.completePendingCommand();"
invocation.
 I waited for 10 minutes, then I killed the process.

Thanks.

Francesco
2010/11/17 Pablo Pinto <pp...@cyc.cl>

> Hi,
>
> I think you need to add the following after retrieveFile:
>
> ftpClient.completePendingCommand();
>
> Best Regards,
> Pablo
>
> 2010/11/17 Francesco Fazzini <ff...@gmail.com>
>
> > 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
> >
>
>
>
> --
> Pablo Pinto
> Computación y Comunicaciones S.A.
> Almirante Lorenzo Gotuzzo 124, Oficina 1500
> Teléfonos : 9135704 - 9135700
>

Re: [Net] problem in FTP file retrieving

Posted by Pablo Pinto <pp...@cyc.cl>.
Hi,

I think you need to add the following after retrieveFile:

ftpClient.completePendingCommand();

Best Regards,
Pablo

2010/11/17 Francesco Fazzini <ff...@gmail.com>

> 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
>



-- 
Pablo Pinto
Computación y Comunicaciones S.A.
Almirante Lorenzo Gotuzzo 124, Oficina 1500
Teléfonos : 9135704 - 9135700