天天看點

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;

      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");

        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;

        JOptionPane.showMessageDialog(null,"連結失敗,請重新輸入!"+e.getMessage(), "連結失敗", JOptionPane.ERROR_MESSAGE);

        i=2;

    return i;

  //判斷是否有該檔案

  public boolean pdFile(String filename){

    boolean flag=false;

        FTPFile[] file=ftp.listFiles(filename);

        if(file.length>0){

          flag=true;

    return flag;

  public int upLoadFile(String filename,InputStream input){

          if(!pdFile(filename)){

            ftp.storeFile(filename, input);

            input.close();

          }else{

            JOptionPane.showMessageDialog(null,"上傳"+filename+"重複", "上傳失敗", JOptionPane.ERROR_MESSAGE);

            i=1;

      catch (Exception e) {

        JOptionPane.showMessageDialog(null,"上傳"+filename+"失敗:"+e.getMessage(), "上傳失敗", JOptionPane.ERROR_MESSAGE);

    }else{

      JOptionPane.showMessageDialog(null,"未連接配接FTP伺服器", "未連接配接FTP伺服器", JOptionPane.ERROR_MESSAGE);

      i=3;

  public int downLoadFile(String filename,String locapth){

      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);

        JOptionPane.showMessageDialog(null,"下載下傳"+filename+"不存在", "下載下傳失敗", JOptionPane.ERROR_MESSAGE);

}

繼續閱讀