Using ftpclient to upload and download and get the file directory

Source: Internet
Author: User
Tags ftp connection ftp file readline
There is a ftpclient in the Sun code, though it is not used as a public toolkit, but we can use it to take advantage of it. Java Code 1./** 2. * ftp file Upload and download 3. * Notice:4. * The reason why each time you want to connect to FTP is to let its directory return to the relative root directory, 5. * If the last ftpclient is reused it is possible that it is currently in the FTP directory not the 6 we want. * Directory, so in the FTP upload download files, it is best to log in again every time FTP 7. * @author Lgh 8.  
*/9.public class FtpClient {10.  
Private FtpClient ftpclient;  
Private String IP;  
private int port;  
Private String username;  
private String password;    16.17.    Public FtpClient () {18.  
} 19.        Public ftpclient (string ip, int port, string Username, string password) {21.  
This.ip = IP;  
This.port = port;  
This.username = Username;  
This.password = password;  
25.} 26.     27./** 28.     * files that need to be backed up 29.     * @param list 30.     * @return 31.    * * 32.        Private list Needbackfile (List list, String relativename) {33.  
List fileNames = new ArrayList ();   for (int i = 0; i < list.size (); i++) {35.         The string temp = (string) list.get (i);                if (Temp.indexof (Relativename) > 0) {37.  
Filenames.add (temp);        38.} 39.        } 40.  
return fileNames;  
41.} 42.        public static void Main (string[] args) {44.  
FtpClient client = new FtpClient ("...", 21, "...", "...");        45.46.  
try {47.//client.downloadfile ("crm/ccbcrm/", "d://", "CRMClientLog.log", "CRMClientLog.log");  
48.//Client.uploadfile ("", "d://", "CRMClientLog.log");  
List = client.getlist ("csrtestftp/Network/", false);            catch (Exception ex) {51.  
Logger.getlogger (FTPClient.class.getName ()). log (Level.severe, NULL, ex);    52.} 53.  
} 54.     55./** 56.     * Turn off FTP connection 57.     * @throws java.lang.Exception 58.    * * 59.        public void CloseServer () throws Exception {60.  
Ftpclient.closeserver ();  
61.} 62.     63./** 64. * Connection FTP Server 65.     * @param IP 66.     * @param Port 67.     * @param user 68.     * @param pwd 69.     * @return 70.     * @throws Exception 71.    * * 72.            public boolean connectserver (string ip, int port, string user, string pwd) 73.        Throws Exception {74.  
Boolean issuccess = false;            try {76.  
FtpClient = new FtpClient ();  
Ftpclient.openserver (IP, port);  
Ftpclient.login (user, PWD);  
Issuccess = true;            (Exception ex) {81.  
Ex.printstacktrace ();        82.} 83.  
return issuccess;  
84.} 85.     86./** 87.     * Get the directory under Remote 88.     * @param remotepath remote directory 89.     * @param whether FullPath requires full path 90.     * @return 91.    * * 92.        Public List getlist (String remotepath, Boolean FullPath) {93.  
List List = new ArrayList ();            A. try {95.               if (ConnectServer (IP, port, username, password)) {96. BufferedReader br = new BufferedReader (New InputStreamReader (Ftpclient.namelist (RemotePath)));  
A. String temp = ftpclient.getresponsestring ();  
SYSTEM.OUT.PRINTLN (temp);  
A. String readLine = null;  
lastindex int; if (lastindex = Remotepath.lastindexof ("/")) > 0| |                    (lastindex = Remotepath.lastindexof ("//")) > 0 102. || (lastindex = Remotepath.lastindexof ("\")) > 0| |                    (lastindex = Remotepath.lastindexof (file.separator)) > 0) {103.  
RemotePath = remotepath.substring (0, lastindex);                104.//Remove RemotePath suffix, may be '/', also may be other symbol 105.  
while ((ReadLine = Br.readline ())!= null) {106.                        (!fullpath) {108.  
List.add (Readline.substring (Remotepath.length () + 1, readline.length ())); 109. System.out.println (Readline.substring (remotepath.lengtH () + 1, readline.length ());                        The.} else {111.  
List.add (ReadLine);  
112. SYSTEM.OUT.PRINTLN (ReadLine);                113.} 114.                } 115.  
Ftpclient.closeserver ();        116.} 117.            The catch (Exception ex) {118.  
Ex.printstacktrace ();        119.} 120.  
return list;  
121.} 122.     123./** 124.     * Download file 125.     * @param remotepath 126.     * @param localpath 127.     * @param filename 128.     * @throws Exception 129.    * * 130. public void DownloadFile (string remotepath, String LocalPath, String remotefilename, String localfilename) throws Excepti        on {131.            try {132.                if (ConnectServer (IP, port, username, password)) {133.                    if (remotepath.length ()!= 0) {134.  
FTPCLIENT.CD (RemotePath);                135.} 136.  
Ftpclient.binary (); 137.               Telnetinputstream is = Ftpclient.get (remotefilename);  
138. File FP = new file (LocalPath);                    139. if (!fp.exists ()) {140.  
Fp.mkdirs ();                141.} 142.  
File File_out = new file (LocalPath + file.separator + localfilename);  
143. FileOutputStream OS = new FileOutputStream (file_out);  
144. byte[] bytes = new byte[1024];  
145. int Readbye;                    146. while ((Readbye = is.read (bytes))!=-1) {147.  
Os.write (bytes, 0, Readbye);                148.} 149.  
Is.close ();  
Os.close ();  
151. Ftpclient.closeserver ();        152.} 153.            The catch (Exception ex) {154.  
Ex.printstacktrace ();    155.} 156.  
} 157.     158./** 159.     * Upload file 160.     * @param remotepath 161.     * @param localpath 162. * @param filename 163.    * @throws Exception 164.    * * 165.        public void UploadFile (string remotepath, String localpath, String filename) throws Exception {166.            try {167.                if (ConnectServer (IP, port, username, password)) {168.                    if (remotepath.length ()!= 0) {169.  
FTPCLIENT.CD (RemotePath);                170.} 171.  
Ftpclient.binary ();  
172. Telnetoutputstream OS = ftpclient.put (filename);  
173. File file_in = new file (localpath + file.separator + filename);  
174. FileInputStream is = new FileInputStream (file_in);  
175. byte[] bytes = new byte[1024];  
176. int Readbye;                    177. while ((Readbye = is.read (bytes))!=-1) {178.  
Os.write (bytes, 0, Readbye);                179.} 180.  
Is.close ();  
181. Os.close ();  
Ftpclient.closeserver (); 183.}  
184.} catch (Exception ex) {185.  
Ex.printstacktrace ();    186.} 187.  
} 188.     189./** 190.     * @return the IP 191.    * * 192.        Public String GetIP () {193.  
return IP;  
194.} 195.     196./** 197.     * @param IP The IP to set 198.    * * 199.        public void setIp (String IP) {200.  
This.ip = IP;  
201.} 202.     203./** 204.     * @return the Port 205.    * * 206.        public int Getport () {207.  
return port;  
208.} 209.     210./** 211.     * @param port the port to set 212.    * * 213.        public void Setport (int port) {214.  
This.port = port;  
215.} 216.     217./** 218.     * @return the username 219.    * * 220.        Public String GetUserName () {221.  
return username;  
222.} 223.     224./** 225.     * @param username the username to set 226.    * * 227.        public void Setusername (String username) {228. This.username = Username  
229.} 230.     231./** 232.     * @return the password 233.    * * 234.        Public String GetPassword () {235.  
return password;  
236.} 237.     238./** 239.     * @param password The password to set 240.    * * 241.        public void SetPassword (String password) {242.  
This.password = password;  
 243.} 244.}


Original: http://lgh3292.iteye.com/blog/368819

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.