java代码 删除ftp已上传文件
时间: 2023-04-06 10:00:51 浏览: 110
你可以使用 Apache Commons Net 库中的 FTPClient 类来删除已上传的文件。以下是一个示例代码:
```java
import org.apache.commons.net.ftp.FTPClient;
public class FtpDeleteExample {
public static void main(String[] args) {
String server = "ftp.example.com";
int port = 21;
String username = "username";
String password = "password";
String remoteFilePath = "/path/to/file.txt";
FTPClient ftpClient = new FTPClient();
try {
ftpClient.connect(server, port);
ftpClient.login(username, password);
boolean deleted = ftpClient.deleteFile(remoteFilePath);
if (deleted) {
System.out.println("File deleted successfully.");
} else {
System.out.println("Failed to delete file.");
}
ftpClient.logout();
} catch (Exception e) {
e.printStackTrace();
} finally {
if (ftpClient.isConnected()) {
try {
ftpClient.disconnect();
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
}
```
请注意,您需要替换示例代码中的服务器、端口、用户名、密码和远程文件路径。
阅读全文