ftp文件上传下载

 

package isale.ftp.test;

import java.io.IOException;
import java.io.OutputStream;

import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPFile;

/**
* 使用commons的net包进行ftp链接. 相关包:commons-net-1.4.1.jar ;
* commons-io-1.2.jar;jakarta-oro-2.0.8.jar测试通过.可以列出ftp上的文件
* 通过把ftp服务器上的文件流连接到outSteam及可以把文件下载到本机的目录..限制如果目录为中文则需要处理.最好使用英文文件名
*
* @author xzgf email:
*
*
* @create 2007-2-11
*
*/
public class ListFtpFile {

private FTPClient ftpClient = new FTPClient();

private OutputStream outStream = null;

/**
   * ftp服务器地址
   */
private String hostName = "192.168.0.3";

/**
   * 登录名
   */
private String userName = "isale";

/**
   * 登录密码
   */
private String password = "isale";

/**
   * 需要访问的远程目录
   */
private String remoteDir = "/bugzillaBackup";

/**
   * 登录方法
   *
   */
private void login() {
   try {
    // 链接到ftp服务器
    ftpClient.connect(hostName);
    System.out.println("连接到ftp服务器:" + hostName + " 成功..开始登录");
    // 登录.用户名 密码
    ftpClient.login(userName, password);
    System.out.println("登录成功.");

    FTPFile[] remoteFiles = ftpClient.listFiles(remoteDir);
    System.out.println("目录" + remoteDir + "下的文件:");
    if (remoteFiles != null) {
     for (int i = 0; i < remoteFiles.length; i++) {
      String name = remoteFiles[i].getName();
      long length = remoteFiles[i].getSize();
      String readableLength = FileUtils
        .byteCountToDisplaySize(length);
      System.out.println(name + ":\t\t" + readableLength);
     }
    }

   } catch (Exception e) {
    e.printStackTrace();
   } finally {
    // 使用IO包关闭流
    if (outStream == null) {
     System.out.println("outStream is null.");
    }
    IOUtils.closeQuietly(outStream);
    try {
     ftpClient.disconnect();
    } catch (IOException ioe) {
     ioe.printStackTrace();
    }
   }
}

public static void main(String[] args) {
   ListFtpFile listFtpfiles = new ListFtpFile();
   listFtpfiles.login();
}
}

package isale.ftp.test;

import java.io.InputStream;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.util.Properties;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPClientConfig;
import org.apache.commons.net.ftp.FTPReply;

public class FtpClientCommonNetImpl {
// ftp配置
private Properties config;

protected FTPClient connectFtpServer() throws Exception {
   String server = this.config.getProperty("server");
   String userName = this.config.getProperty("userName");
   String password = this.config.getProperty("password");
   // 创建ftp客户端对象
   FTPClient ftp = new FTPClient();
   try {
   ftp.configure(this.getFTPClientConfig());
    // 连接ftp服务器
   ftp.connect(server);
    // 登录
   ftp.login(userName, password);

    // 返回值
    int reply = ftp.getReplyCode();
    if ((!FTPReply.isPositiveCompletion(reply))) {
    ftp.disconnect();
     throw new Exception("登录ftp服务器失败,请检查server[" + server
       + "]、username[" + userName + "]、password[" + password
       + "]是否正确!");
    } else {
     System.out.println("登陆成功");
    }

    return ftp;
   } catch (Exception ex) {
    throw ex;
   }
}

/**
   * 关闭连接
   *
   * @param ftp
   * @throws Exception
   */
protected void disconnectFtpServer(FTPClient ftp) throws Exception {
   try {
   ftp.logout();
   ftp.disconnect();
   } catch (Exception ex) {
    throw ex;
   }
}

/**
   * 上传
   *
   * @throws Exception
   */
public void upload(InputStream localIn, String remoteFilePath)
    throws Exception {
   /*
    * String server = this.config.getProperty("server"); String userName =
    * this.config.getProperty("userName"); String password =
    * this.config.getProperty("password");
    */
   FTPClient ftp = this.connectFtpServer();

   try {
    boolean result = ftp.storeFile(this
      .enCodingRemoteFilePath(remoteFilePath), localIn);
    // boolean result=ftp.storeFile(remoteFilePath,localIn);
    if (!result) {
     throw new Exception("文件上传失败!");
    }
   } catch (Exception ex) {
    throw ex;
   } finally {
    this.disconnectFtpServer(ftp);
   }
}

/**
   * 下载
   *
   * @throws Exception
   */
public void download(OutputStream localOut, String remoteFilePath)
    throws Exception {
   FTPClient ftp = this.connectFtpServer();

   try {
    boolean result = ftp.retrieveFile(this
      .enCodingRemoteFilePath(remoteFilePath), localOut);
    // boolean result=ftp.storeFile(remoteFilePath,localIn);
    if (!result) {
     throw new Exception("文件上传失败!");
    }
   } catch (Exception ex) {
    throw ex;
   } finally {
    this.disconnectFtpServer(ftp);
   }
}

/**
   * 上传结束以后关闭输入流
   *
   * @param localIn
   * @param remoteFilePath
   * @param afterUploadCloseInputStream
   * @throws Exception
   * @throws FtpException
   */
public void upload(InputStream localIn, String remoteFilePath,
    boolean afterUploadCloseInputStream) throws Exception {
   try {
    // 上传
    this.upload(localIn, remoteFilePath);
   } finally {
    if (afterUploadCloseInputStream) {
     if (localIn != null) {
      try {
       localIn.close();
      } catch (Exception ex) {
       throw ex;
      }
     }
    }
   }
}

/**
   * 得到配置
   *
   * @return
   */
protected FTPClientConfig getFTPClientConfig() {
   // 创建配置对象
   FTPClientConfig conf = new FTPClientConfig(this.config.getProperty(
     "systemKey", FTPClientConfig.SYST_NT));
   conf.setServerLanguageCode(this.config.getProperty(
     "serverLanguageCode", "zh"));
   return conf;
}

/**
   * 远程文件路径编码(上传到ftp上的文件路径)
   *
   * @param remoteFilePath
   * @return
   * @throws UnsupportedEncodingException
   */
protected String enCodingRemoteFilePath(String remoteFilePath)
    throws UnsupportedEncodingException {
   // return StringUtils.gbkToIso8859EnCoding(remoteFilePath);
   return new String(remoteFilePath.getBytes("gbk"), "ISO8859-1");

}

public Properties getConfig() {
   return config;
}

public void setConfig(Properties config) {
   this.config = config;
}

}

package isale.ftp.test;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Properties;

import org.apache.commons.net.ftp.FTPClientConfig;

public class FtpMain {
public static void main(String[] args) {
/*   //完成上传功能
   FtpClientCommonNetImpl fccn = new FtpClientCommonNetImpl();
   Properties properties=new Properties();
   properties.setProperty("systemKey",FTPClientConfig.SYST_NT);
   properties.setProperty("serverLanguageCode","zh");
   InputStream is = null;
   try {
    is = new FileInputStream("D:/Javascript权威指南/中国IT认证实验室学习下载频道.txt");
   } catch (FileNotFoundException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
   } 
   properties.setProperty("server","192.168.0.3");
   properties.setProperty("userName","isale");
   properties.setProperty("password","isale");
   fccn.setConfig(properties);
    try {
    fccn.upload(is, "/bugzillaBackup/javacript.java",true);
   } catch (Exception e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
   }*/
 
//   完成下载功能
   FtpClientCommonNetImpl fccn = new FtpClientCommonNetImpl();
   Properties properties=new Properties();
   properties.setProperty("systemKey",FTPClientConfig.SYST_NT);
   properties.setProperty("serverLanguageCode","zh");
   OutputStream os = null;
   try {
    os = new FileOutputStream("E:/中国IT认证实验室学习下载频道.txt");
   } catch (FileNotFoundException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
   } 
   properties.setProperty("server","192.168.0.3");
   properties.setProperty("userName","isale");
   properties.setProperty("password","isale");
   fccn.setConfig(properties);
    try {
    fccn.download(os, "/bugzillaBackup/javacript.java");
   } catch (Exception e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
   }
 
}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值