import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.SocketException;
import java.text.SimpleDateFormat;
import java.util.Properties;
import org.apache.commons.logging.Log;
import org.apache.commons.net.ftp.FTP;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPClientConfig;
import org.apache.commons.net.ftp.FTPReply;
import sun.net.TelnetInputStream;
import sun.net.ftp.FtpClient;
 
 
 
public class Ftp {
 FtpClient ftpClient = null;
    private static Log logger;  
     
    private static String UserName = null;  
     
    private static String Password = null;  
     
    private static String Ip = null;  
     
    private static Integer Port = null;  
     
    private static Properties Property = null;  
  
     
    private  FTPClient FtpClientTEMP = null;  
     
    private static SimpleDateFormat dateFormat = new SimpleDateFormat(  
            "yyyy-MM-dd hh:mm");  
     
    public static final int BINARY_FILE_TYPE = FTP.BINARY_FILE_TYPE;  
     
    public static final int ASCII_FILE_TYPE = FTP.ASCII_FILE_TYPE;  
    public  static int  i=1;  
    public static Boolean connect_flag = false;
   
       
        public boolean loadFile(Ftp ftp , String[] remoteFileName, String[] localFileName,InputStream is) { 
            boolean flag = true;  
//            InputStream is = null;
            FileOutputStream os = null;
            long result = 0;   
            try {  
             FtpClient client = connectServerClient(ftp,is);
             for(int i = 0; i < remoteFileName.length; i++){
              os = new FileOutputStream(localFileName[i]);
              is = client.get(remoteFileName[i]);
              byte[] bytes = new byte[1024];     
              int c;     
              while ((c = is.read(bytes)) != -1) {     
               os.write(bytes, 0, c);     
               result = result + c;     
              }
             }
            } catch (Exception e) {  
                e.printStackTrace();  
                logger.debug("本地文件下载失败!", e);  
            } finally {
             try {
     is.close();
     os.close();
     closeConnect(ftp);
    } catch (IOException e) {
     e.printStackTrace();
    }
            }  
            return flag;  
        }
   
     
    public boolean uploadFile(Ftp ftp,InputStream is,File[] docNew, String[] newFileName) {  
        boolean flag = true;  
        try {  
      connect_flag = connectServer(ftp,is);
       //设置的三步骤 ftp.getFtpClientTEMP().setFileType(BINARY_FILE_TYPE); ftp.getFtpClientTEMP().enterLocalPassiveMode(); ftp.getFtpClientTEMP().setFileTransferMode(FTP.STREAM_TRANSFER_MODE); InputStream input = null; for(int i = 0; i < docNew.length; i++){ File file = docNew[i]; File newFile = new File(newFileName[i]); String dir = newFile.getParentFile().getPath(); if (!ftp.getFtpClientTEMP().changeWorkingDirectory(dir)) {// 如果不能进入dir下,说明此目录不存在! if (!makeDirectory(ftp,is,newFile.getParentFile().getPath())) { System.out.println("创建文件目录【"+dir+"】失败!"); } } changeWorkingDirectory(ftp,"/");// 回到FTP根目录 input = new FileInputStream(file); if (input == null) { System.out.println("本地文件不存在"); logger.debug("本地文件不存在,请重新选择!"); } if (newFileName[i].trim().equals("")) { newFileName[i] = file.getName(); } flag = ftp.getFtpClientTEMP().storeFile(newFileName[i], input); if (flag) { System.out.println("upload File succeed"); } else { System.out.println("upload File false"); } } input.close(); } catch (IOException e) { e.printStackTrace(); logger.debug("本地文件上传失败!", e); // TODO: handle exception } catch (Exception e) { e.printStackTrace(); // logger.debug("本地文件上传失败!", e); // TODO: handle exception }finally{ closeConnect(ftp); } return flag; } public void closeConnect(Ftp ftp) { FtpClientTEMP = ftp.getFtpClientTEMP(); ftpClient = ftp.getFtpClient(); try { if(FtpClientTEMP != null){ FtpClientTEMP.logout(); FtpClientTEMP.disconnect(); FtpClientTEMP = null; } if(ftpClient != null){ ftpClient.closeServer(); ftpClient = null; } } catch (Exception e) { e.printStackTrace(); } } public void setFileType(int fileType) { try { // connectServer(); FtpClientTEMP.setFileType(fileType); } catch (Exception e) { e.printStackTrace(); } } protected FTPClient getFtpClient(Ftp ftp,InputStream is) { connectServer(ftp,is); return FtpClientTEMP; } private static void setArg(InputStream is) { Property = new Properties(); BufferedInputStream inBuff = null; try { // File file = new File(configFile); inBuff = new BufferedInputStream(is); Property.load(inBuff); UserName = Property.getProperty("username"); Password = Property.getProperty("password"); Ip = Property.getProperty("ip"); Port = Integer.parseInt(Property.getProperty("port")); } catch (FileNotFoundException e1) { System.out.println("FTP配置文件不存在!"); e1.printStackTrace(); } catch (IOException e) { System.out.println("FTP配置文件无法读取!"); e.printStackTrace(); } } public boolean connectServer(Ftp ftp,InputStream is) { FtpClientTEMP = ftp.getFtpClientTEMP(); boolean flag = true; if (FtpClientTEMP == null) { int reply; try { setArg(is); FtpClientTEMP = new FTPClient(); FtpClientTEMP.setControlEncoding("GBK"); FtpClientTEMP.setDefaultPort(Port); FtpClientTEMP.configure(getFtpConfig()); FtpClientTEMP.connect(Ip); FtpClientTEMP.login(UserName, Password); FtpClientTEMP.setDefaultPort(Port); //System.out.print(FtpClient.getReplyString()); reply = FtpClientTEMP.getReplyCode(); FtpClientTEMP.setDataTimeout(120000); if (!FTPReply.isPositiveCompletion(reply)) { FtpClientTEMP.disconnect(); System.err.println("FTP server refused connection."); // logger.debug("FTP 服务拒绝连接!"); flag = false; } // System.out.println(i); // i ; } catch (SocketException e) { flag = false; e.printStackTrace(); System.err.println("登录ftp服务器【" +Ip+ "】失败,连接超时!"); // logger.debug("登录ftp服务器【" Ip "】失败"); } catch (IOException e) { flag = false; e.printStackTrace(); System.err.println("登录ftp服务器【"+ Ip +"】失败,FTP服务器无法打开!"); // logger.debug("登录ftp服务器【" Ip "】失败"); } } return flag; } public static void changeWorkingDirectory(Ftp ftp , String directory) { try { ftp.getFtpClientTEMP().changeWorkingDirectory(directory); } catch (IOException ioe) { ioe.printStackTrace(); } } public void renameFile(Ftp ftp,InputStream is,String oldFileName, String newFileName) { try { connectServer(ftp,is); FtpClientTEMP.rename(oldFileName, newFileName); } catch (IOException ioe) { ioe.printStackTrace(); } } private static FTPClientConfig getFtpConfig() { FTPClientConfig ftpConfig = new FTPClientConfig( FTPClientConfig.SYST_UNIX); ftpConfig.setServerLanguageCode(FTP.DEFAULT_CONTROL_ENCODING); return ftpConfig; } private static String iso8859togbk(Object obj) { try { if (obj == null) return ""; else return new String(obj.toString().getBytes("iso-8859-1"), "GBK"); } catch (Exception e) { return ""; } } public boolean makeDirectory(Ftp ftp,InputStream is,String dir) throws IOException { boolean flag = true; FtpClientTEMP = ftp.getFtpClientTEMP(); if(FtpClientTEMP == null){ connectServer(ftp,is); } flag = FtpClientTEMP.makeDirectory(dir); if (!flag) { System.out.println("make Directory " +dir+ " false"); } return flag; } public static Log getLogger() { return logger; } public static void setLogger(Log logger) { Ftp.logger = logger; } public FtpClient connectServerClient(Ftp ftp,InputStream is){ ftpClient = new FtpClient(); setArg(is); try { if(Port != -1){ ftpClient.openServer(Ip,Port); }else{ ftpClient.openServer(Ip); } ftpClient.login(UserName, Password); ftpClient.binary();// 用2进制上传、下载 System.out.println("已登录到\"" + ftpClient.pwd() + "\"目录"); return ftpClient; }catch (IOException e){ e.printStackTrace(); return null; } } public FTPClient getFtpClientTEMP() { return FtpClientTEMP; } public void setFtpClientTEMP(FTPClient ftpClientTEMP) { FtpClientTEMP = ftpClientTEMP; } public void setFtpClient(FtpClient ftpClient) { this.ftpClient = ftpClient; } public FtpClient getFtpClient() { return ftpClient; } }