java中利用Ftp向服务器传文件

此代码需要引入commons-net-1.4.1.jar

package com.test;

import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;

import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPFile;
import org.apache.commons.net.ftp.FTPReply;
import org.junit.Test;

public class FtpUtil {
 /**
   * Description: 向FTP服务器上传文件
   * @param url FTP服务器hostname
   * @param port FTP服务器端口
   * @param username FTP登录账号
   * @param password FTP登录密码
   * @param path FTP服务器保存目录
   * @param filename 上传到FTP服务器上的文件名
   * @param input    输入流
   * @return 成功返回true,否则返回false
   */
  public boolean uploadFile(String url, int port, String username,
    String password, String path, String filename, InputStream input) {
   // 初始表示上传失败
   boolean success = false;
   // 创建FTPClient对象
   FTPClient ftp = new FTPClient();
   try {
    int reply;
    // 连接FTP服务器
    // 如果采用默认端口,可以使用ftp.connect(url)的方式直接连接FTP服务器
   // ftp.connect(url);
    ftp.connect(url, port);
    // 登录ftp
    ftp.login(username, password);
    // 看返回的值是不是230,如果是,表示登陆成功
    reply = ftp.getReplyCode();
    // 以2开头的返回值就会为真
    if (!FTPReply.isPositiveCompletion(reply)) {
     ftp.disconnect();
     return success;
    }
    // 转到指定上传目录
    ftp.changeWorkingDirectory(path);
    // 将上传文件存储到指定目录
    ftp.storeFile(filename, input);
    // 关闭输入流
    input.close();
    // 退出ftp
    ftp.logout();
    // 表示上传成功
    success = true;
   } catch (IOException e) {
    e.printStackTrace();
   } finally {
    if (ftp.isConnected()) {
     try {
      ftp.disconnect();
     } catch (IOException ioe) {
     }
    }
   }
   return success;
  }  
  
   /**
   * Description: 从FTP服务器下载文件
   * @param url  FTP服务器hostname
   * @param port   FTP服务器端口
   * @param username  FTP登录账号
   * @param password   FTP登录密码
   * @param remotePath   FTP服务器上的相对路径
   * @param fileName  要下载的文件名
   * @param localPath  下载后保存到本地的路径
   * @return
   */
  public boolean downFile(String url, int port, String username,
    String password, String remotePath, String fileName,
    String localPath) {
   // 初始表示下载失败
   boolean success = false;
   // 创建FTPClient对象
   FTPClient ftp = new FTPClient();
   try {
    int reply;
    // 连接FTP服务器
    // 如果采用默认端口,可以使用ftp.connect(url)的方式直接连接FTP服务器
    ftp.connect(url, port);
    // 登录ftp
    ftp.login(username, password);
    reply = ftp.getReplyCode();
    if (!FTPReply.isPositiveCompletion(reply)) {
     ftp.disconnect();
     return success;
    }      // 转到指定下载目录
    ftp.changeWorkingDirectory(remotePath);
    // 列出该目录下所有文件
    FTPFile[] fs = ftp.listFiles();
    // 遍历所有文件,找到指定的文件
    for (FTPFile ff : fs) {
     if (ff.getName().equals(fileName)) {
      // 根据绝对路径初始化文件
      File localFile = new File(localPath + "/" + ff.getName());
      // 输出流
      OutputStream is = new FileOutputStream(localFile);
      // 下载文件
      ftp.retrieveFile(ff.getName(), is);
      is.close();
     }
    }
    // 退出ftp
    ftp.logout();
    // 下载成功
    success = true;
   } catch (IOException e) {
    e.printStackTrace();
   } finally {
    if (ftp.isConnected()) {
     try {
      ftp.disconnect();
     } catch (IOException ioe) {
     }
    }
   }
   return success;
  }

 /**
  * 将本地文件上传到FTP服务器上
  * 
  */
  public static void main(String[] args) {
   try {
    FileInputStream in = new FileInputStream(new File("D:/test.txt"));
    FtpUtil util=new FtpUtil();
    boolean flag = util.uploadFile("218.240.49.146", 21, "lw", "lw",
      "/", "a.txt", in);
    System.out.println(flag);
   } catch (Exception e) {
    e.printStackTrace();
   }
 }
// @Test
// 
// public void testUpLoadFromDisk() {
//  try {
//   FileInputStream in = new FileInputStream(new File("D:/test.txt"));
//   boolean flag = uploadFile("218.240.49.146", 21, "lw", "lw",
//     "D:/test", "a.txt", in);
//   System.out.println(flag);
//  } catch (Exception e) {
//   e.printStackTrace();
//  }
// }

 /**
  * 在FTP服务器上生成一个文件,并将一个字符串写入到该文件中
  */
// @Test
// public void testUpLoadFromString() {
//  try {
//   InputStream input = new ByteArrayInputStream("test ftp"
//     .getBytes("utf-8"));
//   boolean flag = uploadFile("127.0.0.1", 21, "test", "test",
//     "D:/ftp", "test.txt", input);
//   System.out.println(flag);
//  } catch (UnsupportedEncodingException e) {
//   e.printStackTrace();
//  }
// }

}

commons-net-1.4.1.jar

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值