「java监控ftp」java监控ftp目录

博主:adminadmin 2023-03-19 17:38:07 250

今天给各位分享java监控ftp的知识,其中也会对java监控ftp目录进行解释,如果能碰巧解决你现在面临的问题,别忘了关注本站,现在开始吧!

本文目录一览:

java ftp

import java.io.File;

import java.io.FileInputStream;

import java.io.IOException;

import java.io.OutputStream;

import java.io.UnsupportedEncodingException;

import sun.net.;

/**

*

* @author chenc

* @version 1.0

*

* 2008-02-19

*

*/

public class Ftp {

private String ip = "";

private String username = "";

private String password = "";

private String ftpDir = "";

private String localFileFullName = "";// 待上传的文件全名

private String ftpFileName = ""; // 文件上传到FTP后的名称

FtpClient ftpClient = null;

OutputStream os = null;

FileInputStream is = null;

public Ftp(String serverIP, String username, String password, String ftpDir) {

this.ip = serverIP;

this.username = username;

this.password = password;

if (ftpDir == null) {

this.ftpDir = "/ftpfileload";

} else {

try {

this.ftpDir = "/"

+ new String(ftpDir.getBytes("ISO-8859-1"), "GBK")

.toString();

} catch (UnsupportedEncodingException e) {

// TODO 自动生成 catch 块

e.printStackTrace();

}

}

}

private void createDir(String dir, FtpClient ftpClient) {

System.out.println(this.ftpDir);

ftpClient.sendServer("MKD " + dir + "\r\n");

try {

ftpClient.readServerResponse();

} catch (IOException e) {

// TODO 自动生成 catch 块

e.printStackTrace();

}

}

private Boolean isDirExist(String dir, FtpClient ftpClient) {

try {

ftpClient.cd(dir);

} catch (Exception e) {

// TODO 自动生成 catch 块

return false;

}

return true;

}

public String upload(String localFileFullName) {

// this.ftpFileName = "aaa.test";

// 获取文件后缀名

String ext = localFileFullName.substring(localFileFullName

.lastIndexOf("."));

// System.out.println(ext);

// 产生新文件名,用系统当前时间+文件原有后缀名

long newFileName = System.currentTimeMillis();

String newFileFullName = newFileName + ext;

// System.out.println("new file name:"+newFileFullName);

this.ftpFileName = newFileFullName;

try {

String savefilename = new String(localFileFullName

.getBytes("ISO-8859-1"), "GBK");

// 新建一个FTP客户端连接

ftpClient = new FtpClient();

ftpClient.openServer(this.ip);

ftpClient.login(this.username, this.password);

// 判断并创建目录

if (!isDirExist(this.ftpDir, ftpClient)) {

createDir(this.ftpDir, ftpClient);

}

ftpClient.cd(this.ftpDir);// 切换到FTP目录

ftpClient.binary();

os = ftpClient.put(this.ftpFileName);

// 打开本地待长传的文件

File file_in = new File(savefilename);

is = new FileInputStream(file_in);

byte[] bytes = new byte[1024];

// 开始复制

int c;

// 暂未考虑中途终止的情况

while ((c = is.read(bytes)) != -1) {

os.write(bytes, 0, c);

}

} catch (Exception e) {

e.printStackTrace();

System.err.println("Exception e in Ftp upload(): " + e.toString());

} finally {

try {

if (is != null) {

is.close();

}

if (os != null) {

os.close();

}

if (ftpClient != null) {

ftpClient.closeServer();

}

} catch (Exception e) {

System.err.println("Exception e in Ftp upload() finally"

+ e.toString());

}

}

return this.ftpFileName;

}

public void delFile(String dir, String filename) {

ftpClient = new FtpClient();

try {

ftpClient.openServer(this.ip);

ftpClient.login(this.username, this.password);

if (dir.length() 0) {

ftpClient.cd(dir);

}

ftpClient.sendServer("DELE " + filename + "\r\n");

ftpClient.readServerResponse();

} catch (IOException e) {

// TODO 自动生成 catch 块

e.printStackTrace();

}

}

}

我写的一个FTP类,你看看你能不能用。。。。。

Java的ftp操作方法有哪几种

FTP(File Transfer Protocol)是 Internet 上用来传送文件的协议(文件传输协议)。它是为了我们能够在 Internet 上互相传送文件而制定的的文件传送标准,规定了 Internet 上文件如何传送。也就是说,通过 FTP 协议,我们就可以跟 Internet 上的 FTP 服务器进行文件的上传(Upload)或下载(Download)等动作。

和其他 Internet 应用一样,FTP 也是依赖于客户程序/服务器关系的概念。在 Internet 上有一些网站,它们依照 FTP 协议提供服务,让网友们进行文件的存取,这些网站就是 FTP 服务器。网上的用户要连上 FTP 服务器,就要用到 FPT 的客户端软件,通常 Windows 都有“ftp”命令,这实际就是一个命令行的 FTP 客户程序,另外常用的 FTP 客户程序还有 CuteFTP、Ws_FTP、FTP Explorer等。

要连上 FTP 服务器(即“登陆”),必须要有该 FTP 服务器的帐号。如果是该服务器主机的注册客户,你将会有一个 FTP 登陆帐号和密码,就凭这个帐号密码连上该服务器。但 Internet 上有很大一部分 FTP 服务器被称为“匿名”(Anonymous)FTP 服务器。这类服务器的目的是向公众提供文件拷贝服务,因此,不要求用户事先在该服务器进行登记注册。

Anonymous(匿名文件传输)能够使用户与远程主机建立连接并以匿名身份从远程主机上拷贝文件,而不必是该远程主机的注册用户。用户使用特殊的用户名“anonymous”和“guest”就可有限制地访问远程主机上公开的文件。现在许多系统要求用户将Emai1地址作为口令,以便更好地对访问进行跟综。出于安全的目的,大部分匿名FTP主机一般只允许远程用户下载(download)文件,而不允许上载(upload)文件。也就是说,用户只能从匿名FTP主机拷贝需要的文件而不能把文件拷贝到匿名FTP主机。另外,匿名FTP主机还采用了其他一些保护措施以保护自己的文件不至于被用户修改和删除,并防止计算机病毒的侵入。在具有图形用户界面的 WorldWild Web环境于1995年开始普及以前,匿名FTP一直是Internet上获取信息资源的最主要方式,在Internet成千上万的匿名PTP主机中存储着无以计数的文件,这些文件包含了各种各样的信息,数据和软件。 人们只要知道特定信息资源的主机地址, 就可以用匿名FTP登录获取所需的信息资料。虽然目前使用WWW环境已取代匿名FTP成为最主要的信息查询方式,但是匿名FTP仍是 Internet上传输分发软件的一种基本方法

java如何测试连接ftp是否通

java测试连接ftp是否连通可以使用判断是否有异常来决定,实例如下:

/** 

     * connectServer 

     * 连接ftp服务器 

     * @throws java.io.IOException 

     * @param path 文件夹,空代表根目录 

     * @param password 密码 

     * @param user    登陆用户 

     * @param server 服务器地址 

     */ 

  public void connectServer(String server, String user, String password,  String path) 

  throws IOException 

   { 

     // server:FTP服务器的IP地址;user:登录FTP服务器的用户名 

     // password:登录FTP服务器的用户名的口令;path:FTP服务器上的路径 

      ftpClient = new FtpClient(); 

      ftpClient.openServer(server); 

      ftpClient.login(user, password); 

     //path是ftp服务下主目录的子目录 

     if (path.length() != 0)   ftpClient.cd(path); 

     //用2进制上传、下载 

      ftpClient.binary();      

/** 

     * upload 

     * 上传文件 

     * @throws java.lang.Exception 

     * @return -1 文件不存在 

     *           -2 文件内容为空 

     *           0 成功上传,返回文件的大小 

     * @param newname 上传后的新文件名 

     * @param filename 上传的文件 

     */ 

public long upload(String filename,String newname) throws Exception 

     long result = 0; 

      TelnetOutputStream os = null; 

      FileInputStream is = null; 

     try {          

          java.io.File file_in = new java.io.File(filename); 

         if (!file_in.exists()) return -1; 

         if (file_in.length()==0) return -2; 

          os = ftpClient.put(newname); 

          result = file_in.length(); 

          is = new FileInputStream(file_in); 

         byte[] bytes = new byte[1024]; 

         int c; 

         while ((c = is.read(bytes)) != -1) { 

               os.write(bytes, 0, c); 

          } 

      } finally { 

         if (is != null) { 

              is.close(); 

          } 

         if (os != null) { 

             os.close(); 

          } 

      } 

    return result; 

/** 

     * upload 

     * @throws java.lang.Exception 

     * @return 

     * @param filename 

     */ 

public long upload(String filename) 

throws Exception 

    String newname = ""; 

    if (filename.indexOf("/")-1) 

     { 

        newname = filename.substring(filename.lastIndexOf("/")+1); 

     }else 

     { 

        newname = filename; 

     } 

    return upload(filename,newname); 

/** 

     *   download 

     *   从ftp下载文件到本地 

     * @throws java.lang.Exception 

     * @return 

     * @param newfilename 本地生成的文件名 

     * @param filename 服务器上的文件名 

     */ 

public long download(String filename,String newfilename) 

throws Exception 

{   

    long result = 0; 

     TelnetInputStream is = null; 

     FileOutputStream os = null; 

    try 

     { 

        is = ftpClient.get(filename);        

        java.io.File outfile = new java.io.File(newfilename); 

        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 (IOException e) 

     { 

        e.printStackTrace(); 

     } 

    finally { 

         if (is != null) { 

              is.close(); 

          } 

         if (os != null) { 

             os.close(); 

          } 

      } 

     return result; 

/** 

   * 取得某个目录下的所有文件列表 

   * 

   */ 

public List getFileList(String path) 

     List list = new ArrayList(); 

    try 

     { 

        DataInputStream dis = new   DataInputStream(ftpClient.nameList(path)); 

       String filename = ""; 

       while((filename=dis.readLine())!=null)   

        {   

          list.add(filename);         

        }   

    

     } catch (Exception e) 

     { 

        e.printStackTrace(); 

     } 

    return list; 

/** 

     * closeServer 

     * 断开与ftp服务器的链接 

     * @throws java.io.IOException 

     */ 

public void closeServer() 

throws IOException 

{    

   try 

    { 

      if (ftpClient != null) 

       { 

         ftpClient.closeServer();      

       } 

    } catch (IOException e) { 

       e.printStackTrace(); 

    } 

   

  public static void main(String [] args) throws Exception 

   { 

     FtpUtil ftp = new FtpUtil(); 

    try { 

         //连接ftp服务器 

          ("10.163.7.15", "cxl", "1", "info2"); 

         /**   上传文件到 info2 文件夹下 */ 

          System.out.println("filesize:"+("f:/download/Install.exe")+"字节"); 

         /** 取得info2文件夹下的所有文件列表,并下载到 E盘下 */ 

          List list = ("."); 

         for (int i=0;ilist.size();i++) 

          { 

            String filename = (String)list.get(i); 

             System.out.println(filename); 

             (filename,"E:/"+filename); 

          } 

     } catch (Exception e) { 

       /// 

     }finally 

     { 

        ; 

     } 

   }   

}

用java实现ftp侧压缩文件的解压

你这个问题有点怪,首先你要在服务器解压缩的话,你有服务器的Java程序执行权限吗?如果有的话可以考虑,在服务器做Java对Ftp文件夹的文件监控,发现压缩包自动调用解压缩代码解压,如果没有服务器执行权限,那你就不用考虑这个问题了

关于java监控ftp和java监控ftp目录的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站。