Well, I've been learning Java and I managed to create an FTP client that uploads and downloads files from a server. Oddly, the thing I can't figure out is how to do an FTP delete. Can anyone help me on that? I don't want to use an API or someone else's class. (How would I learn from that? ;)
I've tried looking through some existing API's for hints, but their too complicated for me to figure out. You have to go from function to function in tons of files and I still don't get it.
Any help or suggestions would be great! Thanks everyone.
might this help:
package org.kodejava.example.commons.net;
import org.apache.commons.net.ftp.FTPClient;
import java.io.IOException;
public class FtpDeleteDemo {
public static void main(String[] args) {
FTPClient client = new FTPClient();
try {
client.connect("ftp.domain.com");
client.login("admin", "secret");
// Delete file on the FTP server. When the FTP delete complete
// it returns true.
String filename = "/testing/data.txt";
boolean deleted = client.deleteFile(filename);
if (deleted) {
System.out.println("File deleted...");
}
client.logout();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
client.disconnect();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
DavidKroukamp
Practically a Master Poster
693 posts since Dec 2011
Reputation Points: 282
Solved Threads: 169
Have you read the RFC on how FTP works?
You can use a Socket connection to send the FTP server commands made of Strings.
You need to know the protocol for it to work.
NormR1
Posting Expert
6,677 posts since Jun 2010
Reputation Points: 1,138
Solved Threads: 656
NormR1
Posting Expert
6,677 posts since Jun 2010
Reputation Points: 1,138
Solved Threads: 656
That could work. Try it and see how it goes.
NormR1
Posting Expert
6,677 posts since Jun 2010
Reputation Points: 1,138
Solved Threads: 656
With FTP you send a request to the server. The server does what you ask.
You need to read the doc on the protocol. One place is the RFC doc for FTP.
NormR1
Posting Expert
6,677 posts since Jun 2010
Reputation Points: 1,138
Solved Threads: 656
Connect to the server and send the command as a String.
I don't know if this will be helpful or confusing. I wrote an FTP Server a few years ago.
Here is its console log from when I connect to it with a FTP tool: WS_FTP95.
FTPrun(): connected accepted Socket[addr=/127.0.0.1,port=4145,localport=21]
ServerSocket[addr=0.0.0.0/0.0.0.0,port=0,localport=21]
MyIP4PASV: 127,0,0,1,
writeString: writing >220 Norms FTP server ready.
< (end of writing) <<<
FTPreq: >USER Norm<
writeString: writing >331 Password required.
< (end of writing) <<<
FTP req: PASS asdf
writeString: writing >230 User logged in.
< (end of writing) <<<
FTP request: PWD
writeString: writing >257 "D:/www/" is current directory.
< (end of writing) <<<
FTP request: SYST
writeString: writing >215 NAME Norm's FTP Server
< (end of writing) <<<
FTP request: HELP
writeString: writing >214-The following commands are recognized:
USER
QUIT
ABOR
TYPE
LIST
PWD
MKD
CWD
TYPE
PORT
SYST
HELP
NLST
CDUP
PASV
RETR
STOR
214 Help command successful.
< (end of writing) <<<
FTP request: PORT 127,0,0,1,16,50
dpH RmtAddr: 127.0.0.1:4146
dpH OSocket: Socket[addr=/127.0.0.1,port=4146,localport=20]
writeString: writing >220 PORT Command successful.
< (end of writing) <<<
FTP request: LIST
writeString: writing >150 Opening ASCII mode data connection.
< (end of writing) <<<
dpHdoLIST: >< true
writeString: writing >226 Transfer complete.
< (end of writing) <<<
FTP request: PWD
writeString: writing >257 "D:/www/" is current directory.
< (end of writing) <<<
FTP request: PORT 127,0,0,1,16,51
dpH RmtAddr: 127.0.0.1:4147
dpH OSocket: Socket[addr=/127.0.0.1,port=4147,localport=20]
writeString: writing >220 PORT Command successful.
< (end of writing) <<<
FTP request: LIST
writeString: writing >150 Opening ASCII mode data connection.
< (end of writing) <<<
dpHdoLIST: >< true
writeString: writing >226 Transfer complete.
< (end of writing) <<<
FTP request: QUIT
User: Norm logged off
writeString: writing >221 Goodbye.
< (end of writing) <<<
FTPrun(): waiting on connect at Mon Jan 23 21:07:52 EST 2012 ServerSocket[addr=0.0.0.0/0.0.0.0,port=0,localport=21]
The Strings inside of the > < are what the server received or sent.
The one in red was received from the client.
The one in green was what was sent to the client
NormR1
Posting Expert
6,677 posts since Jun 2010
Reputation Points: 1,138
Solved Threads: 656
See my last post. It has part of a dialog between a FTP server and a client.
The client was an FTP tool I got years ago. I suspect it uses sockets.
NormR1
Posting Expert
6,677 posts since Jun 2010
Reputation Points: 1,138
Solved Threads: 656
how to send FTP commands.
Use the output stream you get from a Socket.
NormR1
Posting Expert
6,677 posts since Jun 2010
Reputation Points: 1,138
Solved Threads: 656
How does the short piece of code relate to the stack trace?
For example: Where is line 155
What is the value of the variable: url?
NormR1
Posting Expert
6,677 posts since Jun 2010
Reputation Points: 1,138
Solved Threads: 656
Congratulations on getting your code to work.
I wouldn't say it was finished, but at least you have the basics done.
NormR1
Posting Expert
6,677 posts since Jun 2010
Reputation Points: 1,138
Solved Threads: 656