Several Methods for writing files to FTP in JAVA

Source: Internet
Author: User

1. use URL: 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 (); The above is a piece of code. The URL constructor parameters can use different access protocols (such as http and ftp ), "//" is followed by the user name and password. The two are separated by ":", followed by the separator "@", "@", followed by the IP address and port, then there is the directory, and finally the file name to be written. The directory must exist. Otherwise, FileNotFoundException will be thrown. The file may not exist and a new file will be created if it does not exist, otherwise, the new content will overwrite the previous content; 2. use FtpClient: 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"); // The written file name pw. write ("this is a test"); pw. flush (); pw. close (); 3. ftpClientimport sun.net. ftp. ftpClient; import java.net. socket; import java. io. IOException; public class PasvFtpClient extends FtpClient {/*** FTP server address */private String serverAddr;/*** connect to the FTP server Socket */private Socket socket Socket; /*** copy the static variable defined by the parent class */protected final static int FTP_ERROR = 3; /*** copy the static variable 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 () main part of {super (); socket = null;}/*** rewrite. The parent class adopts the PORT mode, here, the FTP command is changed to PASV mode * @ param s * @ return to connect to the FTP server Socket * @ 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 failed = 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 );}}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.