Http://www.blogjava.net/JaVaa/
1. Use URL:
URL
=
New
URL (
"
FTP: // javaa: javaa@172.168.2.222: 21/test/javaa.txt
"
);
Printwriter pw
=
New
Printwriter (URL. openconnection (). getoutputstream ());
PW. Write (
"
This is a test
"
);
PW. Flush ();
PW. Close ();
Upper
The URL constructor parameters can use different access protocols (such as HTTP and FTP), and "//" is followed by the user name and password, the two are separated by ":", followed
It is the separator "@", "@", followed by the IP address and port, followed by the directory, and finally the file name to be written, where the directory must exist, otherwise it will throw
Filenotfoundexception: The file may not exist. If it does not exist, a new file is created. Otherwise, the new content will overwrite the previous content;
2. Use ftpclient:
Ftpclient
=
New
Ftpclient ();
Ftpclient. openserver (
"
172.162.222
"
,
21
);
//
IP address and port
Ftpclient. login (
"
Javaa
"
,
"
Javaa
"
);
//
Username and password. If you log on anonymously, the username is anonymous. The password is a non-empty string.
Ftpclient. Cd (
"
Test
"
);
//
Switch to the test directory
Printwriter pw
=
New
Printwriter (ftpclient. Put (
"
Javaa.txt
"
));
//
Written file name
PW. Write (
"
This is a test
"
);
PW. Flush ();
PW. Close ();
3. ftpclient for transmitting data in PASV Mode
Import sun.net. FTP. ftpclient;
Import java.net. Socket;
Import java. Io. ioexception;
Public class pasvftpclient
Extends ftpclient {
/**
* FTP server address
*/
Private string serveraddr;
/**
* Socket connected to the FTP server
*/
Private Socket socket;
/**
* Impersonate static variables defined by the parent class
*/
Protected final static int ftp_error = 3;
/**
* Impersonate static variables defined by the parent class
*/
Protected final static int ftp_success = 1;
Public pasvftpclient (string s) throws ioexception {
Super (s );
Serveraddr = s;
Socket = NULL;
}
Public pasvftpclient (string S, int I) throws ioexception {
Super (S, I );
Serveraddr = s;
Socket = NULL;
}
Public pasvftpclient (){
Super ();
Socket = NULL;
}
/**
* The primary part of the rewrite. The parent class adopts the port mode. Here it is changed to the PASV mode.
* @ Param s refers to the input FTP command
* @ Return the socket connecting to the FTP server
* @ Throws ioexception
*/
Protected socket opendataconnection (string s) throws ioexception {
If (socket = NULL ){
String S1 = "PASV ";
If (issuecommand (S1) = ftp_error ){
Myftpprotocolexception ftpprotocolexception = new myftpprotocolexception (
"PASV ");
Throw ftpprotocolexception;
}
String responsestr = This. getresponsestring ();
Int location = responsestr. lastindexof (",");
Int n = integer. parseint (responsestr. substring (location + 1,
Responsestr. indexof (")")));
Responsestr = responsestr. substring (0, location );
Location = responsestr. lastindexof (",");
Int M = integer. parseint (responsestr. substring (location + 1,
Responsestr. Length ()));
Socket = new socket (serveraddr, M * 256 + n );
}
If (issuecommand (S) = ftp_error ){
Myftpprotocolexception ftpprotocolexception1 = new myftpprotocolexception (s );
Throw ftpprotocolexception1;
}
Return socket;
}
/**
* Close the connection to the FTP server
* @ Throws ioexception
*/
Public void closeserver () throws ioexception {
Socket. Close ();
Socket = NULL;
Super. closeserver ();
}
/**
* Open the connection to the FTP server
* @ Param s FTP server address
* @ Param I FTP server port
* @ Throws ioexception
*/
Public void openserver (string S, int I) throws ioexception {
Super. openserver (S, I );
Serveraddr = s;
}
}
/**
* Custom FTP exception class
*/
Class myftpprotocolexception
Extends ioexception {
Myftpprotocolexception (string s ){
Super (s );
}
}