i'm currently working on a small checklist builder program in Java. I would like to upload and download the created file to my FTP Server (ftps). I'm using the following code for downloading:
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();
}
}
the problem is that the download itself works fine. But the downloaded file is empty. If i try it with an Image it isn't "readable". The upload instead works perfectly. I use the Apache Common IO Library for the FTP function.
If i download the file the console shows first status 5%, 20%, 50% and than, after adding the false statement, Download won't work ...
I have no idea why the file itself is downloading but not including any content.
Any ideas?
You're not doing resources in java correctly.
Anytime you create an object that represents a resource, you must close it. You open a new FileOutputStream
, and that is a resource. Anything that implements AutoCloseable
is definitely a resource that you must close. Try this:
try (OutputStream out = new FileOutputStream(data /* no need to wrap in File */)) {
// do your stuff with out here
}
Second note: Your exception handling is atrocious; please stop making this common mistake. Exceptions contain 4 useful bits of information: Type, Message, Trace, and Cause. You're literally tossing 3 out of the 4 into the bin. Just add throws Exception
to your main method, and your downloadFile
method. It saves you typing and makes your error messages much more useful.
I found the solution.
i have added the following two things and it works now
con.execPBSZ(0);
con.execPROT("P");
No idea what that stands for, but I'm going to find it out.
I know this answer is off-topic but I like to provide an example for user rzwitserloot which shows that printStackTrace()
does not hide any important information. This would not fit into a comment:
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();
}
}
}
Produces output:
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
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.