使用Apache Commons FTPSClient,下载的文件始终存储为0字节。我尝试了不同的方法,但在存储的文件上总是得到0个字节。在下面的代码中,我展示了所有三种方法。
以下是使用过的代码:
public static void main(String[] args) {
String server = "ftps-url";
int port = 6321;
String user = "";
String pass = "";
FTPSClient ftp = null;
try {
ftp = new FTPSClient("SSL");
ftp.setAuthValue("SSL");
ftp.addProtocolCommandListener(new PrintCommandListener(new PrintWriter(System.out)));
int reply;
ftp.connect(server, port);
System.out.println("Connecting");
System.out.print(ftp.getReplyString());
// After connection attempt, you should check the reply code to verify success.
reply = ftp.getReplyCode();
if (!FTPReply.isPositiveCompletion(reply)) {
ftp.disconnect();
System.err.println("FTP server refused connection.");
System.exit(1);
}
ftp.login(user, pass);
ftp.execPBSZ(0);
ftp.execPROT("P");
// ... // transfer files
ftp.setBufferSize(1000);
ftp.enterLocalPassiveMode();
// ftp.setControlEncoding("GB2312");
ftp.changeWorkingDirectory("/output"); //path where my files are
ftp.setFileType(FTP.BINARY_FILE_TYPE);
//System.out.println("Remote system is " + ftp.getSystemName());
String[] filelist = ftp.listNames(); //returns null
System.out.println(filelist.length);
System.out.println(Arrays.toString(filelist));
// method 1
File inFile = new File("myfile.xls");
if (!inFile.exists()) {
inFile.createNewFile();
}
InputStream input = new FileInputStream(inFile);
ftp.completePendingCommand();
ftp.storeFile(filelist[0], input);
input.close();
// method 2
FileOutputStream fos = new FileOutputStream("myfile.xls");
ftp.retrieveFile(filelist[0], fos);
fos.flush();
fos.close();
//ftp.completePendingCommand();
// method 3
File path = new File("C:/Users/user/Desktop/");
String remoteFilePath = "/output/" + filelist[0];
File resultFile = new File(path, filelist[0]);
CheckedOutputStream fout = new CheckedOutputStream(new FileOutputStream(resultFile), new CRC32());
ftp.retrieveFile(remoteFilePath, fout);
} catch (Exception ex) {
System.out.println("error");
ex.printStackTrace();
} finally {
// logs out and disconnects from server
try {
if (ftp.isConnected()) {
ftp.logout();
ftp.disconnect();
}
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
答案 0 :(得分:0)
方法1是上传,而不是下载,并且它使用零长度文件破坏服务器上的文件,因此方法2和3都正确地检索零长度文件。