开发者

FTP a file to server, but it the result arrives with zero byte size

I am tring to upload a file to a database server using FTPClient. It shows the file is transfered succesfully, but the file is empty (size 0 bytes).

Below is the source code that I have used to build. Can anyone resolve this issue?

package Examples;

import org.apache.commons.net.ftp.*;
import java.io.FileInputStream;
import java.io.IOException;

public class Main {

    public static void main(String[] args) {

        FTPClient client = new FTPClient();
        FileInputStream fis = null;

        try {

            client.connect("server");
            client.login("userid", "password");
            System.out.print("Message : " + client.getReplyString());

            client.changeWorkingDirectory("/loaddata");
            System.out.println("Working Directory" + client.printWorkingDirectory());

            client.setDefaultPort(22);
            int f1 = client.getDefaultPort();
            boolean f2 = client.setFileType(FTPClient.BINARY_FILE_TYPE);
            System.out.println("File transfer port no  " + f1);
            System.out.println("FTP server reply ." + client.getReplyString());

            String localfile = "c:/Touch.txt";
            fis = new FileInputStream(localfile);
            int lastSlash = localfile.lastIndexOf('/');
            String filename = localfile.substring(lastSlash+1);
            System.out.println("file : "+fis);

            client.setFileTransferMode(2);
            System.out.println("Flag reply ." + client.getReplyString());

            boolean flag = client.storeFile(file开发者_Python百科name,fis);

            System.out.println("Flag reply ." + client.getReplyString());

            if (flag) {
                System.out.println("Successfully uploaded the file");
            } else {
                System.out.println("Not able to upload the file");
            }

            fis.close();
            client.logout();
            System.out.println("Logout ." + client.getReplyString());

        } catch (Exception e) {
            System.out.println("Exception " + e);
        } finally {
            if (client.isConnected()) {
                try {
                    client.disconnect();
                    System.out.println("Server Disconnected." + client.getReplyString());
                } catch (IOException ioe) {
                    // do nothing
                }
            }
        }
    }
}


Looking at some other FTPClient-questions, I think the reason is a bug in the Apache Commons-NET library (of which the FTPClient is a component) in version 3.0.

Install a newer version (3.0.1 fixes the bug).


I removed the client.changeWorkingDirectory("/loaddata"); otherwise left it all intact and got success. Could the line be a problem? Than again I used commons 3.1

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜