`

ftp工具工具类

阅读更多
import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import sun.net.TelnetInputStream;
import sun.net.TelnetOutputStream;
import sun.net.ftp.FtpClient;

import org.apache.commons.net.ftp.FTPClient;

public class FtpClientUtil { 
FtpClient ftpClient; 
private String server; 
private int port; 
private String userName; 
private String userPassword; 

public FtpClientUtil(String server,int port,String userName,String userPassword) 
{ 
  this.server=server; 
  this.port=port; 
  this.userName=userName; 
  this.userPassword=userPassword; 
} 
/** 
  * 链接到服务器 
  * @return 
  */ 
public boolean open() 
{ 
  if(ftpClient!=null&&ftpClient.serverIsOpen()) 
   return true; 
  try 
  { 
      ftpClient= new FtpClient(); 
      ftpClient.openServer(server,port); 
      ftpClient.login(userName, userPassword); 
      ftpClient.binary(); 
      return true; 
  } 
  catch(Exception e) 
  { 
   e.printStackTrace(); 
   ftpClient=null; 
   return false; 
  } 
} 

public boolean cd(String dir){ 
  boolean f = false; 
  try { 
   ftpClient.cd(dir); 
  } catch (IOException e) { 
 //  Logs.error(e.toString()); 
   return f; 
  } 
  return true; 
} 

/** 
  * 上传文件到FTP服务器 
  * @param localPathAndFileName 本地文件目录和文件名 
  * @param ftpFileName  上传后的文件名 
  * @param ftpDirectory FTP目录如:/path1/pathb2/,如果目录不存在回自动创建目录 ,全局ftp路径
  * @throws Exception 
  */ 
public boolean upload(String localDirectoryAndFileName,String ftpFileName,String ftpDirectory)throws Exception { 
  if(!open()) 
   return false; 
  FileInputStream is=null; 
  TelnetOutputStream os=null; 
  ftpDirectory=new String(ftpDirectory.getBytes("ISO-8859-1"), "GBK");
  try 
  { 
   char ch = ' '; 
   if (ftpDirectory.length() > 0) 
    ch = ftpDirectory.charAt(ftpDirectory.length() - 1); 
   for (; ch == '/' || ch == '\\'; ch = ftpDirectory.charAt(ftpDirectory.length() - 1)) 
    ftpDirectory = ftpDirectory.substring(0, ftpDirectory.length() - 1); 

   int slashIndex = ftpDirectory.indexOf(47); 
   int backslashIndex = ftpDirectory.indexOf(92); 
   int index = slashIndex; 
   String dirall = ftpDirectory; 
   if (backslashIndex != -1 && (index == -1 || index > backslashIndex)) 
    index = backslashIndex; 
   String directory = ""; 
   while (index != -1) { 
    if (index > 0) { 
     String dir = dirall.substring(0, index); 
     directory = directory + "/" + dir; 
     ftpClient.sendServer("XMKD " + directory + "\r\n"); 
     ftpClient.readServerResponse(); 
    } 
    dirall = dirall.substring(index + 1); 
    slashIndex = dirall.indexOf(47); 
    backslashIndex = dirall.indexOf(92); 
    index = slashIndex; 
    if (backslashIndex != -1 && (index == -1 || index > backslashIndex)) 
     index = backslashIndex; 
   } 
   ftpClient.sendServer("XMKD " + ftpDirectory + "\r\n"); 
   ftpClient.readServerResponse(); 
//   ftpClient.binary(); 
   if(!"".equals(ftpFileName)){
	   os = ftpClient.put(ftpDirectory+"/"+new String(ftpFileName.getBytes("ISO-8859-1"), "GBK")); 
	   File file_in = new File(new String(localDirectoryAndFileName.getBytes("ISO-8859-1"), "GBK"));
	   is = new FileInputStream(file_in); 
	   byte bytes[] = new byte[1024]; 
	   int i; 
	   while ((i = is.read(bytes)) != -1) 
	    os.write(bytes, 0, i); 
   }
   
   //清理垃圾 
   
    
   return true; 
  } 
  catch(Exception e) 
  { 
   e.printStackTrace(); 
   return false; 
  } 
  finally 
  { 
   if (is != null) 
      is.close(); 
   if (os != null) 
      os.close(); 
  } 
} 
/** 
  * 从FTP服务器上下载文件并返回下载文件长度 
  * @param ftpDirectoryAndFileName 
  * @param localDirectoryAndFileName 
  * @return 
  * @throws Exception 
  */ 
public long download(String ftpDirectoryAndFileName,String localDirectoryAndFileName)throws Exception 
{ 
  long result = 0; 
  if(!open()) 
   return result; 
  TelnetInputStream is = null; 
  FileOutputStream os = null; 
  try  
  {     
        is = ftpClient.get(new String(ftpDirectoryAndFileName.getBytes("ISO-8859-1"), "GBK")); 
        java.io.File outfile = new java.io.File(new String(localDirectoryAndFileName.getBytes("ISO-8859-1"), "GBK"));
        os = new FileOutputStream(outfile); 
        byte[] bytes = new byte[1024]; 
        int c; 
        while ((c = is.read(bytes)) != -1) 
        { 
            os.write(bytes, 0, c); 
            result = result + c; 
        } 
     } 
  catch (Exception e)  
  { 
         throw e; 
  } 
  finally 
  { 
      if (is != null) 
        is.close(); 
      if (os != null) 
        os.close(); 
      
   } 
      return result; 
} 
/** 
  * 返回FTP目录下的文件列表 
  * @param ftpDirectory 
  * @return 
  */ 
  public List<String> getFileNameList(String ftpDirectory) 
  { 
     List<String> list = new ArrayList<String>(); 
     if(!open()) 
   return list; 
     try  
     { 
        DataInputStream dis = new  DataInputStream(ftpClient.nameList(ftpDirectory)); 
        String filename = ""; 
        while((filename=dis.readLine())!=null)   
        { 
          list.add(filename);         
        }   
     } catch (Exception e)  
     { 
        e.printStackTrace(); 
     } 
     return list; 
  } 
  /** 
   * 删除FTP上的文件 
   * @param ftpDirAndFileName 
   */ 
  public boolean deleteFile(String ftpDirAndFileName) 
  { 
   if(!open()) 
   return false; 
   ftpClient.sendServer("DELE "+ftpDirAndFileName+"\r\n"); 
   return true; 
  } 
  /** 
   * 删除FTP目录 
   * @param ftpDirectory 
   */ 
  public boolean deleteDirectory(String ftpDirectory) 
  { 
   if(!open()) 
   return false; 
   ftpClient.sendServer("XRMD "+ftpDirectory+"\r\n"); 
   return true; 
  } 
	/**
	 * 检查文件夹是否存在
	 * 
	 * @param dir
	 * @param ftpClient
	 * @return
	 */
  private boolean isDirExist(  FTPClient ftpClient ,String dir) {
		boolean isDirExist = false;
		try {
			isDirExist = ftpClient.changeWorkingDirectory(dir);
		} catch (IOException e) {
			e.printStackTrace();
		}
		return isDirExist;
	}
	
  /** 
   * 关闭链接 
   */ 
  public void close() 
  { 
   try 
   { 
       if(ftpClient!=null&&ftpClient.serverIsOpen()) 
        ftpClient.closeServer(); 
   }catch(Exception e) 
   { 
    
   } 
  } 
} 

分享到:
评论
发表评论

文章已被作者锁定,不允许评论。

相关推荐

    FtpUtil.java(ftp工具类)

    ftp工具类,构造方法初始化类,连接ftp,列出所有文件内容,下载文件

    FTP 工具类

    FTP 工具类,赚钱积分下载东西,谢谢各位!谢谢各位!

    java版FTP工具类

    ftp工具类,帮助你很容易的实现ftp功能

    apache FTP 工具类

    接下来,我们将深入探讨FTP工具类的主要功能、使用方法以及源码分析。 **1. FTPClient类** FTPClient是Apache Commons Net的核心类,它实现了FTP协议的大部分功能。通过这个类,我们可以连接到FTP服务器,执行登录...

    ftp工具类操作ftp服务器

    ftp操作工具类,用户ftp文件的添加,删除,等操作!

    文件FTP上传的工具类

    ftp 上传时,用到的工具类,项目上配置好ftp服务器后,controller可以方便的调用此工具类进行上传

    java操作FTP工具类

    java操作FTP工具类:实现基本断点上传下载、实现上传下载进度汇报、实现中文目录创建及中文文件创建,添加对于中文的支持

    JAVA 操作FTP的工具类,上传,下载,删除功能都有了。

    在这个场景中,我们有一个工具类,它包含了FTP的上传、下载和删除功能,这对于任何需要与FTP服务器交互的应用程序都是非常有用的。 首先,让我们详细了解一下FTP上传功能。在Java中,我们可以使用`FTPClient`类来...

    ftp上传工具类

    java的ftp工具类,需要的自行下载查看,有切换目录,创建目录方法。

    ftp上传下载工具类

    FTP上传下载工具类通常是指一个编程接口或代码库,为开发者提供便捷的FTP客户端功能,包括连接到FTP服务器、上传文件、下载文件以及管理远程目录等操作。 在开发中,我们可能遇到以下关键知识点: 1. FTP基本概念...

    Android ftp 下载工具类

    本知识点主要集中在如何使用Java实现一个FTP下载工具类,以便在Android应用程序中集成这一功能。以下是对`DownloadFTPUtil.java`文件中可能包含的代码和相关知识点的详细解释。 1. **FTP客户端库**:在Android中,...

    java ,android平台FTP工具类

    FTP工具类,包括:文件夹上传下载,文件删除(包括非空文件夹),重命名等操作 基本的操作应该都有

    IOS FTP客户端工具类

    在iOS开发中,FTP(File Transfer Protocol)客户端工具类是一个重要的组成部分,主要用于与FTP服务器进行交互,实现文件的上传、下载、删除以及创建新目录等操作。本篇将详细讲解如何构建一个iOS FTP客户端工具类,...

    Apache FTPClient操作FTP工具类

    Apache FTPClient操作FTP工具类

    c#实现ftp工具类

    这是使用c#实现FTP功能的工具类,包含socket技术。

    java上传ftp服务器工具类

    java上传ftp服务器工具类,提供完成的方法,直接调用即可

    FTP工具类实现ftp上传下载

    采用java实现FTP文件的上传下载,包含文件以及文件夹上传下载,新建文件夹等基本相关操作,不会出现文件名的中文乱码,内含demo相关测试以及jar包,可直接导入使用,采用MyEclipse8.5,jdk1.6亲测无问题

    使用Apache commons-net.jar开发FTP工具类

    在本文中,我们将深入探讨如何利用这个库开发一个FTP工具类,以便在Java应用程序中进行文件上传、下载和其他FTP操作。 首先,我们需要了解FTP的基本概念。FTP是一种用于在网络上进行文件传输的标准协议。它允许用户...

    java ftp工具类源码

    java ftp文件传输工具类源码 依赖包 &lt;!-- https://mvnrepository.com/artifact/commons-net/commons-net --&gt; &lt;groupId&gt;commons-net&lt;/groupId&gt; &lt;artifactId&gt;commons-net&lt;/artifactId&gt; &lt;version&gt;3.7 ...

    FTP操作工具类

    FTP(File Transfer Protocol)操作工具类是编程领域中用于实现FTP文件传输协议功能的类库或模块。在软件开发中,FTP工具类通常包含了连接、登录FTP服务器,上传、下载文件,创建、删除目录等常见操作,使得开发者...

Global site tag (gtag.js) - Google Analytics