Reputation: 1127
I try to download a zip file from a public ftp server (which doesn't require psw or username), and I find that the zip file can be downloaded successfully but cannot be extracted. When I try to extracted, there is an error pop up: "the file cannot be extracted with this program". Here is my code, and I cannot find any problem. Thanks in advance!
String server = "ftp.sec.gov";
int port = 21;
FTPClient ftpClient = new FTPClient();
try {
ftpClient.connect(server, port);
// ftpClient.login(user, pass);
ftpClient.enterLocalPassiveMode();
ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
String remoteFile1 = "/edgar/full-index/1993/QTR1/form.zip";
File downloadFile1 = new File("/Users/nancy/Documents/work/INDEX/form.zip");
OutputStream outputStream1 = new BufferedOutputStream(new FileOutputStream(downloadFile1));
boolean success = ftpClient.retrieveFile(remoteFile1, outputStream1);
outputStream1.close();
if (success) {
System.out.println("File #1 has been downloaded successfully.");
}
outputStream1.close();
} catch (IOException ex) {
System.out.println("Error: " + ex.getMessage());
ex.printStackTrace();
} finally {
try {
if (ftpClient.isConnected()) {
ftpClient.logout();
ftpClient.disconnect();
}
} catch (IOException ex) {
ex.printStackTrace();
}
}
Upvotes: 0
Views: 289
Reputation: 201507
Your "public" ftp site is an anonymous server and does require a username and password. You can use something like
ftpClient.login("anonymous", "user@");
And you could also simplify your code with a try-with-resources
close like
try (OutputStream outputStream1 = new BufferedOutputStream(
new FileOutputStream(downloadFile1))) {
boolean success = ftpClient.retrieveFile(remoteFile1, outputStream1);
if (success) {
System.out.println("File #1 has been downloaded successfully.");
}
}
Upvotes: 1