FTPClient's setFileTransferMode Not Taking Eff

2020-04-11 19:02发布

问题:

The following code is meant to take a file (any file would be nice, but right now I'm just using images anyway), and upload it to my server (which works, blah blah blah). The only problem is that the picture is quite skewed after transfer. The main suggestion is to use FTPClient's setFileTranferMode to FTPClient.BINARY_FILE_TYPE, which... has no effect at this point...

Here's the code for the method:

public void sendFile(File sendMe) throws IOException{
    f.connect(ip);
    f.login(username, password);

    String recipient=null;
    while(!f.changeWorkingDirectory(path+recipient)){
        recipient=JOptionPane.showInputDialog("What is the name of the computer you are sending this to?");
    }

    f.changeWorkingDirectory(path+recipient);
    f.setFileTransferMode(FTPClient.BINARY_FILE_TYPE);
    f.storeFile(sendMe.getName(), new BufferedInputStream(new FileInputStream(sendMe)));
    System.out.println("Stored!");

    f.disconnect();
    System.out.println("Uploaded");
}

As always, any help would be much appreciated! Thanks!

回答1:

You are not using the correct method to set the file type. You should use setFileType instead.

f.setFileType(FTPClient.BINARY_FILE_TYPE);


回答2:

Instead of relying on 3rd party FTP clients, why don't you build your own in VB.NET or C#. This way you will have more control if something goes wrong. Here is the code to to just that:

http://dot-net-talk.blogspot.com/2008/12/how-to-create-ftp-client-in-vbnet.html