簡體   English   中英

如何使用java從ftp服務器刪除文件?

[英]how to delete file from ftp server using java?

如何使用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 " + <myFile>.getPath());
ps.close();

Java的URL和URLConnection不支持刪除資源。 (我甚至對上傳工作感到驚訝)。

因此,您必須使用FTP客戶端庫(如Apache Commons Net的FTPClient),或者必須自己實現FTP協議客戶端的必要部分。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM