用Java实现FTP上传

为了解决AMQ取消fileserver的问题,今天研究了一下源码,把AMQ里的ftp上传方法提取出来,单独写成一个类,供以后使用。
需注意的是,需要引用commons.net包,可以直接百度找到,我用的是commons-net-3.5。

package ftpTest;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.ConnectException;
import java.net.MalformedURLException;
import java.net.URL;
import javax.jms.JMSException;
import org.apache.commons.net.ftp.FTPClient;

public class FTPStrategy
{
  protected URL url;
  protected String ftpUser = "";
  protected String ftpPass = "";

  public FTPStrategy() throws MalformedURLException {
    this.url = new URL("ftp://sysop:abcde123@83.25.46.53/opt/activemq/patches/");
  }

  protected void setUserInformation(String userInfo) {
    if (userInfo != null) {
      String[] userPass = userInfo.split(":");
      if (userPass.length > 0) this.ftpUser = userPass[0];
      if (userPass.length > 1) this.ftpPass = userPass[1]; 
    }
    else { this.ftpUser = "anonymous";
      this.ftpPass = "anonymous"; }
  }

  protected FTPClient createFTP() throws IOException, JMSException
  {
    String connectUrl = this.url.getHost();
    setUserInformation(this.url.getUserInfo());
    int port = this.url.getPort() < 1 ? 21 : this.url.getPort();
    
    FTPClient ftp = new FTPClient();
    try {
      ftp.connect(connectUrl, port);
    } catch (ConnectException e) {
      throw new JMSException("Problem connecting the FTP-server");
    }
    if (!ftp.login(this.ftpUser, this.ftpPass)) {
      ftp.quit();
      ftp.disconnect();
      throw new JMSException("Cant Authentificate to FTP-Server");
    }
    return ftp;
  }
  public URL uploadFile(File file) throws JMSException, IOException  {
   FTPClient ftp = createFTP();
   try {
     String path = this.url.getPath();
     String workingDir = path.substring(0, path.lastIndexOf("/"));
     String filename = file.getName();
     ftp.setFileType(2);
     String url;
     if (!ftp.changeWorkingDirectory(workingDir))
       url = this.url.toString().replaceFirst(this.url.getPath(), "") + "/";
     else {
       url = this.url.toString();
     }
     InputStream in = new FileInputStream(file);
     if (!ftp.storeFile(filename, in)) {
       throw new JMSException("FTP store failed: " + ftp.getReplyString());
     }
     return new URL(url + filename);
   } finally {
     ftp.quit();
     ftp.disconnect();
   }
  }
}

直接实例化后,使用uploadFile方法就可以直接发文件了

public static void main (String[] args) throws JMSException, IOException {
 FTPStrategy ftps = new FTPStrategy();
 ftps.uploadFile(new File("e:\\test.vbs"));
}

源码读来真是获益良多,虽然常会被一个个类弄昏头,但是整体考虑时又能发现设计的巧妙。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 173,699评论 25 708
  • 1. Java基础部分 基础部分的顺序:基本语法,类相关的语法,内部类的语法,继承相关的语法,异常的语法,线程的语...
    子非鱼_t_阅读 31,780评论 18 399
  • spring官方文档:http://docs.spring.io/spring/docs/current/spri...
    牛马风情阅读 1,734评论 0 3
  • 提起“精致”,我们脑海里大概都会出现这样一位美丽的女子吧: 身材高挑,肤白貌美,妆容精致,衣着典雅,微微一笑已倾城...
    花音in阅读 949评论 2 18
  • 一、认识新概念 在学习一些新概念新思想的过程中,如我刚刚在读古典的文章时,提出一个观点:你读了很多书却记不...
    帅梅香茗阅读 762评论 4 6