[Java] java ftp服务器 →→→→→进入此内容的聊天室

来自 , 2019-06-13, 写在 Java, 查看 125 次.
URL http://www.code666.cn/view/df7f28ac
  1. /*
  2.   * To change this template, choose Tools | Templates
  3.   * and open the template in the editor.
  4.   */
  5. package test;
  6.  
  7. import com.enterprisedt.net.ftp.FTPClient;
  8. import com.enterprisedt.net.ftp.FTPConnectMode;
  9. import com.enterprisedt.net.ftp.FTPMessageCollector;
  10. import com.enterprisedt.net.ftp.FTPTransferType;
  11. import com.enterprisedt.util.debug.Logger;
  12. import java.io.File;
  13. import java.io.FileInputStream;
  14. import java.io.FileOutputStream;
  15. import java.net.URL;
  16.  
  17. /**
  18.  *
  19.  * @author Icer
  20.  */
  21. public class FtpClient {
  22.  
  23.     private FTPClient ftp = null;
  24.  
  25.     public FTPClient connFtpServer(String host, int port, String username, String password) {
  26.         ftp = new FTPClient();
  27.         try {
  28.  
  29.             ftp.setRemoteHost(host);
  30.             ftp.setRemotePort(port);
  31.             FTPMessageCollector listener = new FTPMessageCollector();
  32.             ftp.setMessageListener(listener);
  33.  
  34.             System.out.println("Connecting");
  35.             ftp.connect();
  36.  
  37.             System.out.println("Logging in");
  38.             ftp.login(username, password);
  39.  
  40.             System.out.println("Setting up passive,ASCII transfers");
  41.             ftp.setConnectMode(FTPConnectMode.PASV);
  42.             ftp.setType(FTPTransferType.ASCII);
  43.  
  44.         } catch (Exception e) {
  45.             e.printStackTrace();
  46.         }
  47.         return ftp;
  48.     }
  49.  
  50.     public void upload(String localfile, URL url) {
  51.         String host = url.getHost();
  52.         int port = url.getPort();
  53.         File filePath = new File(url.getPath());
  54.         String directory = filePath.getParent().substring(1);
  55.         String filename = filePath.getName();
  56.         try {
  57.             ftp.chdir(directory);
  58.             ftp.put(new FileInputStream(localfile), filename);
  59.         } catch (Exception e) {
  60.             e.printStackTrace();
  61.             System.out.println("upload failed");
  62.         }
  63.     }
  64.  
  65.  
  66.     public void upload(String localpath, String filename) {
  67.         try {
  68.             if(ftp.connected()) {
  69.  
  70.                 ftp.put(localpath, filename);
  71.                 ftp.dir();
  72.  
  73.                 System.out.println("hello");
  74.             } else {
  75.                 System.out.println("wrong");
  76.             }
  77.  
  78.         } catch (Exception e) {
  79.             System.out.println("upload failed");
  80.             e.printStackTrace();
  81.         }
  82.     }
  83.  
  84.     public void DownloadFile(String localpath, String filename) {
  85.         try {
  86.             ftp.chdir("");
  87.             String[] files = ftp.dir("", true);
  88.  
  89.             for (int i = 0; i < files.length; i++) {
  90.  
  91.                 System.out.println(files[i]);
  92.  
  93.                 ftp.get(new FileOutputStream(localpath + filename), filename);
  94.  
  95.             }
  96.         } catch (Exception e) {
  97.             e.printStackTrace();
  98.             System.out.println("upload failed");
  99.         }
  100.     }
  101.  
  102.     public void disConn() {
  103.         try {
  104.             ftp.quit();
  105.         } catch (Exception e) {
  106.             System.out.println("disconnection failed");
  107.             e.printStackTrace();
  108.         }
  109.     }
  110.  
  111.  
  112.     public static void main(String[] args) throws Exception {
  113.  
  114.     }
  115. }

回复 "java ftp服务器"

这儿你可以回复上面这条便签

captcha