org.apache FTPClient

import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;

import javax.swing.JOptionPane;

import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPClientConfig;
import org.apache.commons.net.ftp.FTPFile;
import org.apache.commons.net.ftp.FTPReply;

import com.sun.org.apache.regexp.internal.recompile;

/**
*
* @author Administrator
*记得链接后必须关闭连接
*/

public class ftpClient {
  FTPClient ftp= null;
   public ftpClient() {
     // TODO Auto-generated constructor stub
    ftp= new FTPClient();
  }
   public void ftpdisconnect(){
     if(ftp.isConnected()){
       try{
        ftp.disconnect();
        
      } catch (Exception e) {
         // TODO: handle exception
        JOptionPane.showMessageDialog( null, "关闭失败!"+e.getMessage(), "关闭失败", JOptionPane.ERROR_MESSAGE);
      }
    }
  }
   public int getConn(String server,String username,String password,String path){
     int i=0;
     int opi=0;
     if(ftp.isConnected()){
      String ftpserver=ftp.getLocalAddress().getHostAddress();
       if(!ftpserver.equals(server)){
        opi=JOptionPane.showConfirmDialog( null, "是否断开"+ftpserver+ "--链接"+server, "提示", JOptionPane.YES_NO_OPTION);
      } else{
         return i;
      }
    }
     if(opi==0){
      ftp.setControlEncoding( "GBK");
      FTPClientConfig conf= new FTPClientConfig( "GBK");
      conf.setServerLanguageCode( "zh");
       try{
        ftp.connect(server);
        ftp.setSoTimeout(20); //超时十几秒的时间
        ftp.login(username, password);
         int reply=ftp.getReplyCode();
         if(!FTPReply.isPositiveCompletion(reply)){
          JOptionPane.showMessageDialog( null, "登录失败,用户名或者密码错误!", "登录失败", JOptionPane.ERROR_MESSAGE);
          i=1;
        }
         if(!ftp.changeWorkingDirectory(path)){
          JOptionPane.showMessageDialog( null, "跳转失败,路径有误!", "跳转失败", JOptionPane.ERROR_MESSAGE);
          i=3;
        }
      } catch (Exception e) {
         // TODO: handle exception
        JOptionPane.showMessageDialog( null, "链接失败,请重新输入!"+e.getMessage(), "链接失败", JOptionPane.ERROR_MESSAGE);
        i=2;
      }
    }
     return i;
  }
   //判断是否有该文件
   public boolean pdFile(String filename){
     boolean flag= false;
     if(ftp.isConnected()){
       try{
        FTPFile[] file=ftp.listFiles(filename);
         if(file.length>0){
          flag= true;
        }
      } catch (Exception e) {
         // TODO: handle exception
      }
    }
     return flag;
  }
   public int upLoadFile(String filename,InputStream input){
     int i=0;
     if(ftp.isConnected()){
       try{
           if(!pdFile(filename)){
            ftp.storeFile(filename, input);
            input.close();
          } else{
            JOptionPane.showMessageDialog( null, "上传"+filename+ "重复", "上传失败", JOptionPane.ERROR_MESSAGE);
            input.close();
            i=1;
        }
      }
       catch (Exception e) {
         // TODO: handle exception
        JOptionPane.showMessageDialog( null, "上传"+filename+ "失败:"+e.getMessage(), "上传失败", JOptionPane.ERROR_MESSAGE);
        i=2;
      }
    } else{
      JOptionPane.showMessageDialog( null, "未连接FTP服务器", "未连接FTP服务器", JOptionPane.ERROR_MESSAGE);
      i=3;
    }
     return i;
  }
   public int downLoadFile(String filename,String locapth){
     int i=0;
     if(ftp.isConnected()){
       if(pdFile(filename)){
        locapth=locapth+"\\"+filename;
        File file= new File(locapth);
         try{
          FileOutputStream out= new FileOutputStream(file);
          ftp.retrieveFile(filename, out);
          out.close();
        } catch (Exception e) {
           // TODO: handle exception
          JOptionPane.showMessageDialog( null, "下载"+filename+ "失败:"+e.getMessage(), "下载失败", JOptionPane.ERROR_MESSAGE);
          i=1;
        }
      } else{
        JOptionPane.showMessageDialog( null, "下载"+filename+ "不存在", "下载失败", JOptionPane.ERROR_MESSAGE);
        i=2;
      }
    } else{
      JOptionPane.showMessageDialog( null, "未连接FTP服务器", "未连接FTP服务器", JOptionPane.ERROR_MESSAGE);
      i=3;
    }
     return i;
  }
}

你可能感兴趣的:(职场,超时,FTPClient,休闲)