简单的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;
 }
}
相关文章
相关标签/搜索