簡單的ftp操做類

 

package com.onewaveinc.soldiercrab.utils;

import sun.net.ftp.*;
import sun.net.*;
import java.io.*;

/**
 * 使用sun.net.ftp工具包進行ftp上傳下載
 * @description:
 */
public class FtpTool {
 static String ip;
 static int port;
 static String user;
 static String pwd;
 static String remotePath;
 static String localPath;
 static FtpClient ftpClient;

 /**
  * 鏈接ftp服務器
  * 
  * @param ip
  * @param port
  * @param user
  * @param pwd
  * @return
  * @throws Exception
  */
 public static boolean connectServer(String ip, int port, String user, String pwd) throws Exception {
  boolean isSuccess = false;
  try {
   ftpClient = new FtpClient();
   ftpClient.openServer(ip, port);
   ftpClient.login(user, pwd);
   isSuccess = true;
  } catch (Exception ex) {
   throw new Exception("Connect ftp server error:" + ex.getMessage());
  }
  return isSuccess;
 }

 /**
  * 下載文件
  * 
  * @param remotePath
  * @param localPath 主須要輸入路徑,不帶文件名
  * @param filename
  * @throws Exception
  */
 public static void downloadFile(String remotePath, String localPath) throws Exception {
  String[] sArray = parseFtpUrl(remotePath);
  if (sArray[0].indexOf(":") != -1) {
   setIp(sArray[0].substring(0, sArray[0].indexOf(":")));
   setPort(Integer.parseInt(sArray[0].substring(sArray[0].indexOf(":") + 1)));
  } else {
   setIp(sArray[0]);
   setPort(21);
  }
  setUser(sArray[1]);
  setPwd(sArray[2]);
  setRemotePath(sArray[3].substring(1, sArray[3].lastIndexOf("/")+1));
  String filename = sArray[3].substring(sArray[3].lastIndexOf("/") + 1);
  System.out.println("ip:"+getIp());
  System.out.println("port:"+getPort());
  System.out.println("user:"+getUser());
  System.out.println("pwd:"+getPwd());
  System.out.println("remotePath:"+getRemotePath());
  System.out.println("filename:"+filename);
  TelnetInputStream is=null;
  FileOutputStream os=null;
  try {
   if (connectServer(getIp(), getPort(), getUser(), getPwd())) {
    if (getRemotePath().length() != 0)
     ftpClient.cd(getRemotePath());
    ftpClient.binary();
    is = ftpClient.get(filename);
    File file_out = new File(localPath + File.separator + filename);
    os= new FileOutputStream(file_out);
    byte[] bytes = new byte[1024];
    int c;
    while ((c = is.read(bytes)) != -1) {
     os.write(bytes, 0, c);
    }
    
   }
  } catch (Exception ex) {
   throw new Exception("ftp download file error:" + ex.getMessage());
  }finally{
   if(is!=null){
    is.close();
   }
   if(os!=null){
    os.close();
   }
   if(ftpClient!=null){
    ftpClient.closeServer();
   }
  }
 }
 /**
  * 下載文件
  * 
  * @param remotePath
  * @param localPath
  * @param filename
  * @throws Exception
  */
 public static File getFile(String remotePath, String localPath) throws Exception {
  String[] sArray = parseFtpUrl(remotePath);
  if (sArray[0].indexOf(":") != -1) {
   setIp(sArray[0].substring(0, sArray[0].indexOf(":")));
   setPort(Integer.parseInt(sArray[0].substring(sArray[0].indexOf(":") + 1)));
  } else {
   setIp(sArray[0]);
   setPort(21);
  }
  setUser(sArray[1]);
  setPwd(sArray[2]);
  setRemotePath(sArray[3].substring(1, sArray[3].lastIndexOf("/")+1));
  String filename = sArray[3].substring(sArray[3].lastIndexOf("/") + 1);
  System.out.println("ip:"+getIp());
  System.out.println("port:"+getPort());
  System.out.println("user:"+getUser());
  System.out.println("pwd:"+getPwd());
  System.out.println("remotePath:"+getRemotePath());
  System.out.println("filename:"+filename);
  TelnetInputStream is=null;
  FileOutputStream os=null;
  File file_out = null;
  try {
   if (connectServer(getIp(), getPort(), getUser(), getPwd())) {
    if (getRemotePath().length() != 0)
     ftpClient.cd(getRemotePath());
    ftpClient.binary();
    is = ftpClient.get(filename);
    file_out = new File(localPath + File.separator + filename);
    os= new FileOutputStream(file_out);
    byte[] bytes = new byte[1024];
    int c;
    while ((c = is.read(bytes)) != -1) {
     os.write(bytes, 0, c);
    }
   }
  } catch (Exception ex) {
   throw new Exception("ftp download file error:" + ex.getMessage());
  }finally{
   if(is!=null){
    is.close();
   }
   if(os!=null){
    os.close();
   }
   if(ftpClient!=null){
    ftpClient.closeServer();
   }
  }
  
  return file_out;
 }

 /**
  * 上傳文件
  * 
  * @param remotePath
  * @param localPath
  * @param filename
  * @throws Exception
  */
 public  static void uploadFile(String ftpPath, String localPath, String filename) throws Exception {
  String[] sArray = parseFtpUrl(ftpPath);
  if (sArray[0].indexOf(":") != -1) {
   setIp(sArray[0].substring(0, sArray[0].indexOf(":")));
   setPort(Integer.parseInt(sArray[0].substring(sArray[0].indexOf(":") + 1)));
  } else {
   setIp(sArray[0]);
   setPort(21);
  }
  setUser(sArray[1]);
  setPwd(sArray[2]);
  setRemotePath(sArray[3].substring(1, sArray[3].lastIndexOf("/")+1));
  System.out.println("ip:"+getIp());
  System.out.println("port:"+getPort());
  System.out.println("user:"+getUser());
  System.out.println("pwd:"+getPwd());
  System.out.println("remotePath:"+getRemotePath());
  System.out.println("filename:"+filename);
try {
   if (connectServer(getIp(), getPort(), getUser(), getPwd())) {
    if (remotePath.length() != 0)
//以前有個奇怪的問題,以前ftpPath 這個出入的變量名是 remotePath 和 靜態的變量remotePath爲同一個
//ftpClient.cd(remotePath)取到的不是傳入的ftpPath 而是 原本的自定義靜態變量,爲何?
ftpClient.cd(remotePath);
    ftpClient.binary();
    TelnetOutputStream os = ftpClient.put(filename);
    File file_in = new File(localPath + File.separator + filename);
    FileInputStream is = new FileInputStream(file_in);
    byte[] bytes = new byte[1024];
    int c;
    while ((c = is.read(bytes)) != -1) {
     os.write(bytes, 0, c);
    }
    is.close();
    os.close();
    ftpClient.closeServer();
   }
  } catch (Exception ex) {
   throw new Exception("ftp upload file error:" + ex.getMessage());
  }
 }

 /**
  * @return
  */
 public static String getIp() {
  return ip;
 }

 /**
  * @return
  */
 public static int getPort() {
  return port;
 }

 /**
  * @return
  */
 public static String getPwd() {
  return pwd;
 }

 /**
  * @return
  */
 public static String getUser() {
  return user;
 }

 /**
  * @param string
  */
 public static void setIp(String string) {
  ip = string;
 }

 /**
  * @param i
  */
 public static void setPort(int i) {
  port = i;
 }

 /**
  * @param string
  */
 public static void setPwd(String string) {
  pwd = string;
 }

 /**
  * @param string
  */
 public static void setUser(String string) {
  user = string;
 }

 /**
  * @return
  */
 public static FtpClient getFtpClient() {
  return ftpClient;
 }

 /**
  * @param client
  */
 public static void setFtpClient(FtpClient client) {
  ftpClient = client;
 }

 /**
  * @return
  */
 public static String getRemotePath() {
  return remotePath;
 }

 /**
  * @param string
  */
 public static void setRemotePath(String string) {
  remotePath = string;
 }

 /**
  * @return
  */
 public static String getLocalPath() {
  return localPath;
 }

 /**
  * @param string
  */
 public static void setLocalPath(String string) {
  localPath = string;
 }

 /**
  * 從ftp地址解析出主機,用戶名,密碼,及路徑,如對於ftp://portal:portal@192.168.2.210/opt/data
  * 將解析出主機:192.168.2.210,用戶名:portal,密碼:portal,路徑:/opt/data
  * 
  * @param ftpUrl
  *            要解析的ftp地址字符串
  * @return 結果字符串數組
  */
 private static String[] parseFtpUrl(String ftpUrl) {
  String[] result = new String[4];
  ftpUrl = ftpUrl.substring(6);
  while (ftpUrl.indexOf("/") == 0) {
   ftpUrl = ftpUrl.substring(1);
  }
  String username = ftpUrl.substring(0, ftpUrl.indexOf(":"));
  String password = ftpUrl.substring(ftpUrl.indexOf(":") + 1, ftpUrl.indexOf("@"));
  ftpUrl = ftpUrl.substring(ftpUrl.indexOf("@"));
  String server = ftpUrl.substring(ftpUrl.indexOf("@") + 1, ftpUrl.indexOf("/"));
  String directory = ftpUrl.substring(ftpUrl.indexOf("/"));
  result[0] = server;
  result[1] = username;
  result[2] = password;
  result[3] = directory;
  return result;
 }
}
相關文章
相關標籤/搜索