java FTP上传 commons-net应用

 

最近想了解下java方面ftp相关的知识,在网上找了相关资料学习,下面是学习后自己编写的ftp类

 

这里仅供大家参考。

 

 

package com.net.nova.ftp;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Properties;

import org.apache.commons.net.ftp.FTP;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPReply;
import org.apache.log4j.Logger;

import com.net.nova.dao.Utilities;

public class FtpService {

 private static Logger logger = Logger.getLogger(FtpService.class);

 private static String hostname;//ftp服务器的ip地址
 private static String port;//ftp端口号
 private static String username;//用户名
 private static String password;//密码
 private static String path;//ftp路径
 private static String localpath;//本地上传文件路径
 private static String local_path_uploaded;//上传后文件存放路径

 /**
  * 构造方法
  *
  *
  */
 public FtpService() {

 }
 /**
  *
  * 上传多个文件
  *
  * @param localDoc
  * @return
  * @throws Exception
  */
 public static boolean uploadFile() throws Exception {
  // 1. 连接服务器
  logger.info("开始连接ftp服务器...... start ");
  // 1. 连接ftp服务器
  FTPClient ftp = new FTPClient();
  ftp.setControlEncoding("GBK");
  Properties ppt = null;
  if (ppt == null) {
   ppt = Utilities.getProperties("config");
  }
  hostname = ppt.getProperty("ftp_server_ip");// ftp的ip地址
  port = ppt.getProperty("ftp_server_port");// 端口号
  username = ppt.getProperty("ftp_username");// 用户名
  password = ppt.getProperty("ftp_password");// 密码
  path = ppt.getProperty("ftp_path");// ftp服务器指定目录
  localpath = ppt.getProperty("local_path");
  local_path_uploaded = ppt.getProperty("local_path_uploaded");

  try {
   ftp.connect(hostname, Integer.parseInt(port));
  } catch (Exception ex) {
   logger.error("连接ftp服务器出现问题:" + ex.getMessage());
   ex.printStackTrace();
   throw ex;
  }
  // 2. 登录ftp服务器
  try {
   ftp.login(username, password);
  } catch (Exception ex) {
   logger.error("通过配置的用户名和密码登录fpt服务器失败!");
   ex.printStackTrace();
   throw ex;
  }
  // 3. 判断是否登录成功
  int reply = ftp.getReplyCode();
  if (!FTPReply.isPositiveCompletion(reply)) {
   try {
    ftp.disconnect();
   } catch (Exception ex) {
    logger.error("关闭ftp连接出现错误:" + ex.getMessage());
    ex.printStackTrace();
    throw ex;
   }
   return false;
  }
  // 4. 设置本地模式
  ftp.enterLocalPassiveMode();
  // 5. 设置二进制
  try {
   ftp.setFileType(FTP.BINARY_FILE_TYPE);
  } catch (IOException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
   throw e;
  }
  logger.info("连接ftp服务器成功!");
  // 2. 指定操作目录 远端
  try {
   ftp.changeWorkingDirectory(path);
  } catch (IOException e) {
   logger.error("设置ftp服务器的工作目录失败:" + e.getMessage());
   e.printStackTrace();
  }
  File locfile = new File(localpath);
  File file = null;
  File[] files = locfile.listFiles();// 获取目录下的所有文件目录
  for (int i = 0; i < files.length; i++) {
   file = (File) files[i];
   FileInputStream input = null;
   try {
    input = new FileInputStream(file);
   } catch (FileNotFoundException e) {
    logger.error("本地文件不存在:" + e.getMessage());
    e.printStackTrace();
   }
   // 上传文件
   boolean flag = false;
   try {
    flag = ftp.storeUniqueFile(path + file.getName(), input);
   } catch (IOException e) {
    // TODO Auto-generated catch block
    logger.error("上传文件失败,文件名为:" + file.getName());
    e.printStackTrace();
   }
   // 关闭流和文件操作
   if (input != null) {
    try {
     input.close();
    } catch (Exception ex) {
     logger.error("关闭读取流失败:" + ex.getMessage());
     ex.printStackTrace();
    }
   }
   // 将上传成功的文件转移到备份目录
   File oldfile = new File(localpath + file.getName());
   File newfile = new File(local_path_uploaded + file.getName());
   oldfile.renameTo(newfile);
  }
  // 关闭ftp连接
  try {
   if (ftp != null) {
    ftp.logout();
    ftp.disconnect();
   }
  } catch (Exception e) {
   e.printStackTrace();
  }
  return true;
 }

 public static void main(String args[]) {
  FtpService service = new FtpService();
  try {
   service.uploadFile();
  } catch (Exception e) {
   e.printStackTrace();
  }
 }
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值