`
koreyoshi
  • 浏览: 243832 次
  • 性别: Icon_minigender_1
  • 来自: 深圳
社区版块
存档分类
最新评论

Java 实现 FTP上传下载(二)

    博客分类:
  • Java
 
阅读更多

/**
  * 登录FTP
  *
  * @param ftpUrl
  * @param ftpUserName
  * @param ftpPwd
  * @return
  * @throws SocketException
  * @throws IOException
  */
 public static FTPClient loginFtp(String ftpUrl, String ftpUserName,
   String ftpPwd) throws SocketException, IOException {
  FTPClient client = new FTPClient();
  // 让FTP支持中文 必须放在connect之前 否则无效
  client.setControlEncoding("UTF-8");
  client.connect(ftpUrl);
  boolean login = client.login(ftpUserName, ftpPwd);
  if (!login) {
   return null;
  }
  client.enterLocalPassiveMode(); // 更改获取模式
  return client;
 }

 

public upload(){

      FTPClient client = loginFtp(so.getFtpUrl(), so.getFtpUserName(), so.getFtpPwd());

     InputStream is = new ByteArrayInputStream("string test".getBytes("utf-8"));
    client.changeWorkingDirectory(so.getFromPath());
    client.storeFile(e.getKey().toString(), is);

}

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics