`
nwi887nj
  • 浏览: 15816 次
最近访客 更多访客>>
社区版块
存档分类
最新评论

edtftpj 2.0.1开源包扩展

 
阅读更多

edtftpj 2.0.1开源包扩展
2010年01月07日
  package com.enterprisedt.net.ftp.ext;
  import com.enterprisedt.net.ftp.FTPClient;
  import com.enterprisedt.net.ftp.FTPException;
  import com.enterprisedt.net.ftp.FTPFile;
  import com.enterprisedt.net.ftp.FTPMessageCollector;
  import com.enterprisedt.net.ftp.FTPTransferType;
  import com.enterprisedt.net.ftp.FTPConnectMode;
  import com.enterprisedt.util.debug.Level;
  import com.enterprisedt.util.debug.Logger;
  import com.enterprisedt.net.ftp.*;
  import java.util.List;
  import java.io.*;
  import java.text.*;
  /*
  说明:
  一、使用edtftpj 2.0.1开源包
  二、实现了以下功能
  1、上传指定文件夹(包括子文件和文件夹) uploadFolder(String folderName, String ftpPath)
  2、下载FTP上指定的文件夹 downloadFolder(String ftppath, String outdir, String ftpdir)
  3、上传指定文件夹下的所有文件到FTP指定目录下 uploadAllFilesInFolder(String folderName, String ftpPath)
  4、删除指定文件夹下的所有文件(包括子文件夹里面的文件,但由于不知怎么删除目录,所以暂没有做删除目录的功能) deleteAllFilesInFolder(String ftppath, String ftpdir)
  5、删除指定文件 String deleteFile(String ftpPath)
  6、判断FTP上目录是否存在 isDirExist(String dirname, String[] files)
  7、上传单个文件 uploadFile(String clientFileName, String ftpPath)
  8、下载单个文件 downloadFile(String remoteFileName,String localFileName)
  9、删除指定文件夹下的所有文件(不包括子文件夹,只是删除指定文件夹下的文件)
  */
  public class FtpEptUtil {
  private String ProxyServer;
  private String ProxyPort;
  private String ProxyUsername;
  private String ProxyPassword;
  private String ftpServer;
  private String ftpPort;
  private String ftpUserName;
  private String ftpPassword;
  private FTPClient ftpClient;
  private boolean isLogin = false;
  public FtpEptUtil(){    
  }
  /**
  * 初始化连接
  * @param pFtpServer FTP服务器地址
  * @param pFtpPort FTP服务器端口
  * @param pFtpUserName FTP登录用户名
  * @param pFtpPassword FTP登录密码
  * @throws IOException
  */   
  public FtpEptUtil(String pFtpServer, String pFtpPort, String pFtpUserName,
  String pFtpPassword) throws Exception {
  this.ftpServer = pFtpServer;
  if (pFtpPort.trim().equals(""))
  this.ftpPort = "21";
  else
  this.ftpPort = pFtpPort;
  if (pFtpUserName.trim().equals(""))
  this.ftpUserName = "Anonymous";
  else
  this.ftpUserName = pFtpUserName;
  this.ftpPassword = pFtpPassword;
  try {  
  ftpClient = new FTPClient(); //ftpServer, Integer.parseInt(ftpPort)
  ftpClient.setRemoteHost(ftpServer);
  ftpClient.setRemotePort(Integer.parseInt(ftpPort));
  ftpClient.setControlEncoding("gb2312"); //加上这一句后在 edtftpj 2.0.1 下就可以传中文文件名了
  System.out.println("开始登录");
  ftpClient.connect();
  ftpClient.login(ftpUserName, ftpPassword);
  System.out.println("登录成功");
  ftpClient.chdir("\\"); //在有的ftp服务器运行会出错,用ftpClient.chdir("/")又可以了
  System.out.println("已转入根目录");
  isLogin = true;
  } catch (Exception e) {
  throw new Exception(e.getMessage());
  }
  }
  /**
  * 初始化连接
  * @param ProxyServer 代理服务器地址
  * @param ProxyPort 代理服务器端口
  * @param ProxyUsername 代理服务器登录用户名
  * @param ProxyPassword 代理服务器登录密码
  * @param pFtpServer FTP服务器地址
  * @param pFtpPort FTP服务器端口
  * @param pFtpUserName FTP登录用户名
  * @param pFtpPassword FTP登录密码
  * @throws IOException
  */   
  public FtpEptUtil(String ProxyServer,String ProxyPort,String ProxyUsername,String ProxyPassword,String pFtpServer, String pFtpPort, String pFtpUserName,
  String pFtpPassword) throws Exception {
  if ((ProxyServer.trim().equals(""))||(ProxyServer==null))
  this.ProxyPort = "1080";
  else
  this.ProxyPort = ProxyPort;
  if ((ProxyUsername.trim().equals(""))||(ProxyUsername==null))
  this.ProxyUsername = "Anonymous";
  else
  this.ProxyUsername = ProxyUsername;
  if ((ProxyPassword.trim().equals(""))||(ProxyPassword==null))
  this.ProxyPassword = "";
  else
  this.ProxyPassword = ProxyPassword;
  if ((!ProxyServer.trim().equals(""))&&(ProxyServer!=null)){
  FTPClient.clearSOCKS();
  FTPClient.initSOCKS(ProxyPort,ProxyServer); //设置代理服务器
  FTPClient.initSOCKSAuthentication(ProxyUsername, ProxyPassword);//如果代理服务器,需要密码则使用这一句
  }
  this.ftpServer = pFtpServer;
  if (pFtpPort.trim().equals(""))
  this.ftpPort = "21";
  else
  this.ftpPort = pFtpPort;
  if (pFtpUserName.trim().equals(""))
  this.ftpUserName = "Anonymous";
  else
  this.ftpUserName = pFtpUserName;
  this.ftpPassword = pFtpPassword;
  try {  
  ftpClient = new FTPClient(); //ftpServer, Integer.parseInt(ftpPort)
  ftpClient.setRemoteHost(ftpServer);
  ftpClient.setRemotePort(Integer.parseInt(ftpPort));
  ftpClient.setControlEncoding("gbk"); //加上这一句后在 edtftpj 2.0.1 下就可以传中文文件名了
  System.out.println("开始登录");
  ftpClient.connect();
  ftpClient.login(ftpUserName, ftpPassword);
  System.out.println("登录成功");
  ftpClient.chdir("\\"); //在有的ftp服务器运行会出错,用ftpClient.chdir("/")又可以了
  System.out.println("已转入根目录");
  isLogin = true;
  } catch (Exception e) {
  throw new Exception(e.getMessage());
  }
  }
  /**
  * 连接服务器
  * @param pFtpServer FTP服务器地址
  * @param pFtpPort FTP服务器端口
  * @param pFtpUserName FTP登录用户名
  * @param pFtpPassword FTP登录密码
  * @throws IOException
  */   
  public void connServer(String pFtpServer, String pFtpPort,
  String pFtpUserName, String pFtpPassword) throws
  Exception {
  this.ftpServer = pFtpServer;
  if (pFtpPort.trim().equals("")) {
  this.ftpPort = "21";
  } else {
  this.ftpPort = pFtpPort;
  }
  if (pFtpUserName.trim().equals("")) {
  this.ftpUserName = "Anonymous";
  } else {
  this.ftpUserName = pFtpUserName;
  }
  this.ftpPassword = pFtpPassword;
  try {
  ftpClient = new FTPClient(); //ftpServer, Integer.parseInt(ftpPort)
  ftpClient.setRemoteHost(ftpServer);
  ftpClient.setRemotePort(Integer.parseInt(ftpPort));
  ftpClient.setControlEncoding("gb2312"); //加上这一句后在 edtftpj 2.0.1 下就可以传中文文件名了
  System.out.println("开始登录");
  ftpClient.connect();
  ftpClient.login(ftpUserName, ftpPassword);
  System.out.println("登录成功");
  ftpClient.chdir("\\"); //在有的ftp服务器运行会出错,用ftpClient.chdir("/")又可以了
  System.out.println("已转入根目录");
  isLogin = true;
  } catch (Exception e) {
  throw new Exception(e.getMessage());
  }
  }
  /**
  * 连接ftp服务器
  * @param server 服务器地址
  * @param path 文件夹,空代表根目录
  * @param password 密码
  * @param user   登陆用户
  * @param ftppath 登录到的FTP目录,空代表根目录
  */
  public void connServer(String server,int port,String user, String password,String ftppath) {
  if(ftppath.trim().equals("")||(ftppath==null)){
  ftppath = "\\";
  }    
  try {
  ftpClient = new FTPClient();
  ftpClient.setRemoteHost(server);
  ftpClient.setRemotePort(port);
  ftpClient.setControlEncoding("gb2312"); //加上这一句后在 edtftpj 2.0.1 下就要可以传中文文件名了
  System.out.println("开始登录");
  ftpClient.connect();
  ftpClient.login(user, password);
  System.out.println("登录成功");
  ftpClient.chdir(ftppath); //在有的ftp服务器运行会出错,用ftpClient.chdir("/")又可以了
  System.out.println("已转入根目录");
  isLogin = true;
  } catch (FTPException ex) {
  ex.printStackTrace();
  } catch (IOException ex) {
  ex.printStackTrace();
  }
  }
  /**
  * 1、上传指定文件夹(以指定的文件夹开始,包括所有子文件和子文件夹)
  * @param folderName 文件夹在本地址全路径(包括文件夹本身,如:C:\\temp\\)
  * @param ftpPath 文件夹上传后在FTP服务器上的路径(应是形如:/wsq/yhp/的全路径)
  * @throws IOException
  */
  public String uploadFolder(String folderName, String ftpPath) throws
  Exception {
  if (isLogin) {
  String strMsg = "";
  try {
  File file = new File(folderName);
  if (file.isDirectory()) {
  ftpClient.chdir("\\");
  ftpClient.setType(FTPTransferType.BINARY);
  if (checkFolderIsExist(ftpPath)) {
  ftpClient.chdir(ftpPath);
  } else {
  createFolder(ftpPath);
  }
  if (!checkFolderIsExist(file.getName())) {
  ftpClient.mkdir(file.getName());
  }
  ftpClient.chdir(file.getName());
  ftpPath = ftpPath + "\\" + file.getName();
  File[] files = file.listFiles();
  for (int i = 0; i > error!Message:" + ee.getMessage() +
  "\r\n";
  }
  }
  }
  }
  if (!strMsg.equals("")) {
  throw new Exception(strMsg);
  }
  } else {
  System.out.println("file:" + file.getName());
  throw new Exception(folderName + " is not a folder'name!");
  }
  } catch (Exception e) {
  strMsg += e.getMessage() + "\r\n";
  }
  return strMsg;
  } else {
  throw new Exception("you didnot login remote ftp server!");
  }
  }
  /**
  * 5、删除指定文件 String deleteFile(String ftpPath)
  * @param ftpPath FTP上对于根目录的路径
  * @throws IOException
  */   
  public String deleteFile(String ftpPath) throws
  Exception {
  if (isLogin) {
  String strMsg = "";
  try {
  ftpClient.delete(ftpPath);
  } catch (FTPException ex) {
  strMsg += "删除文件错误:" + ex.getMessage();
  } catch (IOException ex) {
  strMsg += "操作文件错误:" + ex.getMessage();
  }
  return strMsg;
  } else {
  throw new Exception("you didnot login remote ftp server!");
  }
  }
  /**
  * 4、删除指定目录(包括文件夹本身)deleteFolder(String ftpPath)
  * @param ftpPath FTP上对于根目录的路径
  * @throws IOException
  */   
  public String deleteFolder(String ftpPath) throws
  Exception {
  if (isLogin) {
  String strMsg = "";
  ftpClient.chdir("\\"); //进入目录
  //列出目录下所有文件和文件夹
  FTPFile[] ftpfiles = ftpClient.dirDetails(ftpPath);
  for (int i = 0; i 上传指定文件夹下的所有文件到FTP指定目录下
  * @param folderName 本地要上传的文件夹全路径
  * @param ftpPath FTP上对于根目录的路径
  * @throws IOException
  */   
  public void uploadAllFilesInFolder(String folderName, String ftpPath) throws
  Exception {
  if (isLogin) {
  String strMsg = "";
  try {
  File file = new File(folderName);
  if (file.isDirectory()) {
  ftpClient.chdir("\\");
  ftpClient.setType(FTPTransferType.BINARY);
  if (checkFolderIsExist(ftpPath)) {
  ftpClient.chdir(ftpPath);
  } else {
  createFolder(ftpPath);
  }
  File[] files = file.listFiles();
  for (int i = 0; i > error!Message:" + ee.getMessage() +
  "\r\n";
  }
  }
  }
  } else {
  throw new Exception(folderName + " is not a folder'name!");
  }
  } catch (Exception e) {
  throw new Exception(e.getMessage());
  }
  } else {
  throw new Exception("you didnot login remote ftp server!");
  }
  }
  /**
  * 7、上传单个文件 uploadFile(String clientFileName, String ftpPath)
  * @param clientFileName 本地要上传的文件的全路径
  * @param ftpPath FTP上对于根目录的路径
  * @throws IOException
  */
  public void uploadFile(String clientFileName, String ftpPath) throws
  Exception {
  if (isLogin) {
  try {
  //获取文件名
  String filename = "";
  int index = clientFileName.lastIndexOf("\\");
  filename = clientFileName.substring(index + 1);
  ftpClient.chdir("\\");
  ftpClient.setType(FTPTransferType.BINARY);
  if (checkFolderIsExist(ftpPath)) {
  ftpClient.chdir(ftpPath);
  } else {
  createFolder(ftpPath);
  }
  ftpClient.put(clientFileName, filename);
  } catch (Exception ex) {
  throw new Exception(ex.getMessage());
  }
  } else {
  throw new Exception("you didnot login remote ftp server!");
  }
  }
  /**
  * 8、下载单个文件 downloadFile(String remoteFileName,String localFileName)
  * @param remoteFileName --服务器上的文件名(含全路径)
  * @param localFileName--本地文件名(全路径名)
  */
  public void downloadFile(String remoteFileName,String localFileName){
  try{
  ftpClient.get(localFileName, remoteFileName);
  }catch(Exception e){
  e.printStackTrace();
  }finally{
  }
  }
  /**
  * 检查FTP服务器上文件夹是否存在
  * @param pFolder FTP上对于根目录的路径
  * @throws Exception
  */
  public boolean checkFolderIsExist(String pFolder) throws Exception {
  if (isLogin) {
  String folder = pFolder.trim();
  if (folder.startsWith("\\")) {
  folder = folder.substring(1);
  }
  if (folder.endsWith("\\")) {
  folder = folder.substring(0, folder.length() - 1);
  }
  String strLayer = "..";
  if (folder.indexOf("\\") > 0) {
  String[] folders = folder.split("\\\\");
  for (int i = 1; i ") > -1 &&
  files.indexOf(dirname) > -1) {
  return true;
  }
  }
  return false;
  }
  /**
  * 在FTP服务上建立目录
  * @param directory FTP上对于根目录的路径
  * @param subDirectory 要在FTP上建立的目录
  */
  private static void createDirectory(String directory, String subDirectory) {
  String dir[];
  File fl = new File(directory);
  try {
  if (subDirectory == "" && fl.exists() != true) {
  fl.mkdir();
  } else if (subDirectory != "") {
  dir = subDirectory.replace('\\', '/').split("/");
  for (int i = 0; i 上传指定文件夹(以指定的文件夹开始,包括所有子文件和子文件夹)
  //  try {
  //   ftpeptutil.uploadFolder(
  //       "C:\\Documents and Settings\\Administrator\\桌面\\6\\200809.1",
  //       "\\");
  //   System.out.println("上传完毕!");
  //  } catch (Exception e) {
  //   e.printStackTrace();
  //  }
  //2、下载FTP上指定的文件夹
  //  try {
  //   ftpeptutil.downloadFolder(
  //       "\\", //FTP服务器上的文件夹的父路径
  //       "D:\\temp\\", //下载到的本地目录
  //       "200809.1"); //要下载的FTP上的目录名(本地不创建此目录)
  //   System.out.println("下载完毕!");
  //  } catch (Exception e) {
  //   e.printStackTrace();
  //  }
  //3、上传指定文件夹下的所有文件到FTP指定目录下
  //  try {
  //   ftpeptutil.uploadAllFilesInFolder(
  //       "C:\\Documents and Settings\\Administrator\\桌面\\6\\200809.1", //要上传的文件所在的目录
  //       "yhp"); //FTP上存放的目录(不存在则创建)
  //   //此示例是将200809.1下的所有文件上传到FTP服务器上的yhp目录下(不含子目录)
  //   System.out.println("上传完毕!");
  //  } catch (Exception e) {
  //   e.printStackTrace();
  //  }
  //4、删除指定目录(包括文件夹本身)
  //  try {
  //   ftpeptutil.deleteFolder("\\", "200809.1"); //FTP上存放的目录(不存在则创建)
  //   //此示例是将200809.1下的所有文件上传到FTP服务器上的yhp目录下(不含子目录)
  //   System.out.println("上传完毕!");
  //  } catch (Exception e) {
  //   e.printStackTrace();
  //  }
  //5、删除指定文件 String deleteFile(String ftpPath)
  //  try {
  //   ftpeptutil.deleteFile("\\20071009\\1.tif"); //FTP上存放的目录(不存在则创建)
  //   //此示例是将200809.1下的所有文件上传到FTP服务器上的yhp目录下(不含子目录)
  //   System.out.println("删除完毕!");
  //  } catch (Exception e) {
  //   e.printStackTrace();
  //  } 
  //6、判断FTP上目录是否存在 isDirExist(String dirname, String[] files)
  //  try {
  //   String[] files = null;
  //   ftpeptutil.isDirExist("\\20071009",files); //FTP上存放的目录(不存在则创建)
  //   //此示例是将200809.1下的所有文件上传到FTP服务器上的yhp目录下(不含子目录)
  //   System.out.println("完毕!");
  //  } catch (Exception e) {
  //   e.printStackTrace();
  //  } 
  //7、上传单个文件 uploadFile(String clientFileName, String ftpPath)
  //  try {
  //   ftpeptutil.uploadFile("C:\\Documents and Settings\\Administrator\\桌面\\6\\200809.1\\log4j使用手册和配置文件示例.rar","\\yhp\\log4j使用手册和配置文件示例.rar"); //FTP上存放的目录(不存在则创建)
  //   //此示例是将200809.1下的所有文件上传到FTP服务器上的yhp目录下(不含子目录)
  //   System.out.println("上传完毕!");
  //  } catch (Exception e) {
  //   e.printStackTrace();
  //  } 
  //8、下载单个文件 downloadFile(String remoteFileName,String localFileName)
  //  try {
  //   ftpeptutil.downloadFile("\\yhp\\image001.jpg","D:\\temp\\image001.jpg");
  //   System.out.println("下载完毕!");
  //  } catch (Exception e) {
  //   e.printStackTrace();
  //  }
  //9、删除指定文件夹下的所有文件(不包括子文件夹,只是删除指定文件夹下的文件)
  //  try {
  //   ftpeptutil.deleteFilesInFolder("200809.1"); //FTP上存放的目录(不存在则创建)
  //   //此示例是将200809.1下的所有文件上传到FTP服务器上的yhp目录下(不含子目录)
  //   System.out.println("上传完毕!");
  //  } catch (Exception e) {
  //   e.printStackTrace();
  //  }
  }
  }
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics