【发布时间】:2018-09-21 09:46:27
【问题描述】:
我用 Java 编写了以下方法,它为我下载了一个文件, 从服务器到我的本地计算机。
public void downloadcsv() {
String server = "servername.host";
int port = 21;
String user = "username";
String pass = "password";
FTPClient ftpClient = new FTPClient();
try {
ftpClient.connect(server, port);
ftpClient.login(user, pass);
ftpClient.enterLocalPassiveMode();
ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
String remoteFile = "/serverpath/daten.csv";
File downloadFile = new File("localpath/daten.csv");
OutputStream outputStream = new BufferedOutputStream(new FileOutputStream(downloadFile));
boolean success = ftpClient.retrieveFile(remoteFile, outputStream);
outputStream.close();
if (success) {
System.out.println("File has been downloaded successfully.");
}
} 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();
}
}
}
到目前为止一切顺利。 现在,我想删除服务器上文件的内容。
只是内容,因为我需要用另一个程序将文件写入其中。
有没有一种快速的方法来做到这一点,在下载期间或之后?
现在,我只是用另一种方法删除文件并创建一个新文件。
但这似乎效率低下。 感谢您的帮助。
【问题讨论】:
-
@algoob,解决方法见this,放入
if(success)。 -
好的,但我如何在服务器上做到这一点?我想删除服务器上文件的内容。