Dan Boyce
Dan Boyce

Reputation: 11

FTP Client issue

I'm having a tough time figuring something out. (I'm pretty new to all this.) I wrote this java pgm to ftp a large file to a destination server. Here's the code (codes been modified a bit for display):

public static void ftpUpload(String path, String upfileName, String dirName) throws Exception
{
    FTPClient client = new FTPClient();
    client.addProtocolCommandListener((ProtocolCommandListener) new PrintCommandListener(new PrintWriter(System.out)));
    client.enterLocalPassiveMode();

    FileInputStream fis = null;

    int reply;

    try {
        client.connect(ftpserver);
        client.login(ftpuserid, ftppasswd);
        reply = client.getReplyCode();

        if(FTPReply.isPositiveCompletion(reply)){
            client.changeWorkingDirectory(ftpdirectoryName + "/" + dirName);

            boolean mkDir = client.makeDirectory(getCurrentMMMYY().toLowerCase());

            client.changeWorkingDirectory(getCurrentMMMYY().toLowerCase());

              //Create an InputStream of the file to be uploaded
            fis = new FileInputStream(path + upfileName);

            //Store file to server
            client.storeFile(upfileName, fis);

        }    
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        try {
            if (fis != null) {
                fis.close();
            }
            client.logout();
            //client.disconnect();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}

Something weird is happening on files I'm sending... One of my files on the origination server is 82575786 in size, and when I ftp this file it almost sends the entire file. It actually sends 82574867. (missing 919) Another file on the origination server is 717885, and when I ftp this file it almost sends the entire file. It actually sends 717522. (missing 363)

I pulled the log to see if something crashed, but it didn't show anything wrong with the transfer. Here are the 2 log entries showing the transfer.

[08/09/11 20:21:13:618 EDT] 00000043 SystemOut O 221-You have transferred 717522 bytes in 1 files. 221-You have transferred 82574867 bytes in 1 files.

Anyone's help would greatly be appreciated. Thanks Dan.

Upvotes: 1

Views: 5293

Answers (2)

Daniele Di Vito
Daniele Di Vito

Reputation: 11

To upload a binary File you have to use the FTP.BINARY_FILE_TYPE but is not enough.

You are using only an INPUT stream, and you need to use an outputstream too

I hope that this example will help you:

FTPClient client = new FTPClient();
client.connect("192.168.30.20");
client.login("pwd", "pwd");

client.setFileType(FTP.BINARY_FILE_TYPE);
String path_base = "/myPath/";
InputStream fis = new FileInputStream("A.pdf");
OutputStream os = client.storeFileStream(path_base+ "B.pdf");


byte buf[] = new byte[8192];
int bytesRead = fis.read(buf);
while (bytesRead != -1) {
   os.write(buf, 0, bytesRead);
   bytesRead = fis.read(buf);}

fis.close();
os.close();
client.completePendingCommand();
client.logout();
client.disconnect();

Upvotes: 1

Jason Goemaat
Jason Goemaat

Reputation: 29234

Are you transferring in ASCII mode instead of binary? ASCII mode converts CR/LF to LF and vice-versa depending on server and client settings.

Are you using Apache's FTP client? It says the default is ASCII, you could try setting BINARY_FILE_TYPE with setFileType:

client.setFileType(FTPClient.BINARY_FILE_TYPE);

Upvotes: 3

Related Questions