如何使用java从ftp服务器删除文件?
如何使用java程序从ftp服务器删除文件? 我可以使用以下代码成功上传ftp文件:
public static void main(String[] args) throws IOException { // TODO Auto-generated method stub String s = "ftp://username:password@ftpclient:21/text.txt;type=i"; URL u = new URL(s); URLConnection uc = u.openConnection(); BufferedOutputStream bos = new BufferedOutputStream(uc.getOutputStream()); bos.write(67); bos.close(); System.out.println("Done"); }
但是如何从这个ftp服务器中删除文件? 任何帮助将不胜感激………提前谢谢
您可以使用Apache FTPClient执行此操作以及FTP上的所有其他命令。 使用它像这样:
... FTPClient client = new FTPClient(); client.connect(host, port); client.login(loginname, password); client.deleteFile(fileNameOnServer); client.disconnect(); ...
查看Apache commons-net 。 它有一个FTP客户端(以及其他东西)。
删除文件的FTP命令是RMD
,我想你可以使用:
String s = "ftp://username:password@ftpclient:21/text.txt;type=i"; URL u = new URL(s); URLConnection uc = u.openConnection(); PrintStream ps = new PrintStream((uc.getOutputStream())); ps.println("RMD " + .getPath()); ps.close();
Java的URL和URLConnection不支持删除资源。 (我甚至对上传工作感到惊讶)。
所以你要么必须使用FTP客户端库(如Apache Commons Net的FTPClient),要么必须自己实现FTP协议客户端的必要部分。