开发者

How to upload to FTP server in Java?

I have the following method to upload_files to an FTP server, I am not receiving any errors yet the file is not appearing on the server after its run. What could be the problem?

public static void upload_files(String un, String pw, String ip, String dir, String fn){
    FTPClient client = new FTPClient();
    FileInputStream fis = null;

    try {
        client.connect(ip);
        client.login(un, pw);

        String filename = dir+"/"+fn;
        fis = new FileInputStream(filename);

        client.storeFile(filename, 开发者_StackOverflowfis);
        client.logout();
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        try {
            if (fis != null) {
                fis.close();
            }
            client.disconnect();
            System.out.println("uploaded");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}


There are a number of possible issues. Assumption is that you are using FTPClient 3.x from Apache commons-net. If using something else, you should probably indicate that in your question. Ideas:

  • Check the reply status of the connection to make sure you are connecting as expected. There's an example on how to do this in the JavaDoc.

  • Your filename variable is the path to the local file you want to send. Is that really the same path you want to use for storing the file on the server (relative to the FTP login root)? It might be, but usually isn't. If not, your first parameter to client.storeFile(...) needs to be changed.

  • Most FTP servers provide ability to log all actions. Are you able to access yours? If so, that usually quickly makes clear what is going wrong.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜