为什么FTP下载不能与Java一起正常工作

为什么FTP下载不能与Java一起正常工作,java,file,download,ftp,is-empty,Java,File,Download,Ftp,Is Empty,我目前正在用Java开发一个小的清单生成器程序。我想上传并下载创建的文件到我的FTP服务器(ftps)。我正在使用以下代码下载: public static void downloadfile(){ FTPSClient con = null; System.out.println("Download Status: 5%"); try { System.out.println("Download Status: 20%"); c

我目前正在用Java开发一个小的清单生成器程序。我想上传并下载创建的文件到我的FTP服务器(ftps)。我正在使用以下代码下载:

public static void downloadfile(){
    FTPSClient con = null;

    System.out.println("Download Status: 5%");
    try
    {
        System.out.println("Download Status: 20%");
        con = new FTPSClient();
        con.connect(url);

        if (con.login(user, psw))
        {
            System.out.println("Download Status: 50%");
            con.enterLocalPassiveMode(); // important!
            con.setFileType(FTP.BINARY_FILE_TYPE);
            String data = "E:\\Downloads\\Testdokument.txt";

            OutputStream out = new FileOutputStream(new File(data));
            boolean result = con.retrieveFile("Testdokument.txt", out);
            out.close();
            System.out.println(result);
            if (result) {
                System.out.println("Download Status: 100%");
            } else if(result == false) {
                System.out.println("Download won't work");
            }
            con.logout();
            con.disconnect();
        }
    }
    catch (Exception e)
    {
        System.out.println("download failed");
        e.printStackTrace();
    }

}
问题是下载本身工作正常。但是下载的文件是空的。如果我试着用一个图像,它是不“可读”的。相反,上传效果很好。我使用Apache公共IO库实现FTP功能

如果我下载的文件控制台显示第一状态5%,20%,50%,然后,在添加错误声明后,下载将不起作用

我不知道为什么文件本身正在下载,但不包括任何内容


有什么想法吗?

您没有正确使用java编写资源

无论何时创建表示资源的对象,都必须将其关闭。打开一个新的
文件输出流
,这是一个资源。任何实现
自动关闭的
都是您必须关闭的资源。试试这个:

try (OutputStream out = new FileOutputStream(data /* no need to wrap in File */)) {
    // do your stuff with out here
}
第二个注意事项:您的异常处理非常糟糕;请停止犯这种常见的错误。异常包含4个有用的信息位:类型、消息、跟踪和原因。你简直是在把4个中的3个扔进垃圾箱。只需将
抛出异常
添加到主方法中,并将
下载文件
方法添加到主方法中。它可以节省您的打字时间,并使您的错误消息更加有用。

我找到了解决方案

我已经添加了以下两个东西,现在可以使用了

con.execPBSZ(0);
con.execPROT("P");

不知道这代表什么,但我会找出答案。

我知道这个答案离题了,但我想为用户rzwitserroot提供一个示例,它表明
printStackTrace()
不会隐藏任何重要信息。这与评论不符:

public class Main
{

    public static void doSomething()
    {
        int array[] = {1, 2, 3};
        int b = array[3];  // throws Exception
    }

    public static void test() throws Exception
    {
        try
        {
            doSomething();
        }
        catch (Exception e)
        {
            throw new Exception("something bad happened", e);
        }
    }

    public static void main(String[] args) throws Exception
    {
        try
        {
            test();
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
    }
}
产生输出:

java.lang.Exception: something bad happened
    at Main.test(Main.java:18)
    at Main.main(Main.java:26)
Caused by: java.lang.ArrayIndexOutOfBoundsException: Index 3 out of bounds for length 3
    at Main.doSomething(Main.java:7)
    at Main.test(Main.java:14)
    ... 1 more

另请参见Oracle的文档:

本地数据文件是否存在?父目录是否存在?
e.printStackTrace()
输出所有4个信息。@rzwitserroot感谢您的回答。我就是这样学的。但我会记住你的提示并尝试优化我的代码。@Stefan不,它不会。因此名为“print stack trace”:它只打印堆栈跟踪。printStartTrace输出所有信息,请参见:
java.lang.Exception:com.company.Main.doSomething(Main.java:14)和com.company.Main.Main(Main.java:22)发生了错误原因:java.lang.ArrayIndexOutOfBoundsException:索引3超出了com.company.Main.doSomething(Main.java:10)中长度3的界限。。。1更多
对于难看的格式,我很抱歉,我不知道如何在注释中设置这些文本的格式。@Stefan这是错误的。您显示的不是命令
e.printStackTrace()
的输出。代码“e.printStackTrace()”不在代码段中。我们达成了强烈的一致:我说过要做你做的事。哦,对不起。我发错了例子。我会解决这个。。。完成。