mysunftp.java

来自「基于sun的开发工具包开发的FTP功能」· Java 代码 · 共 197 行

JAVA
197
字号
package zcptools.net.ftp;

import sun.net.ftp.*;
import sun.net.*;
import java.io.*;
import java.util.*;

/**
 * <p>Title: zhaocp tools</p>
 *
 * <p>Description: 赵春平的工具包</p>
 *
 * <p>Copyright: Copyright (c) 2003</p>
 *
 * <p>Company: bluestar</p>
 *
 * @author not attributable
 * @version 1.0
 */
public class MySunFtp{
    private String hostIp = MyFtpConfig.getString("hostIp");
    private int hostPort = MyFtpConfig.getInt("hostPort");
    private String userName = MyFtpConfig.getString("userName");
    private String userPwd = MyFtpConfig.getString("userPwd");
    private String userPath = MyFtpConfig.getString("userPath");
    private String fileType = MyFtpConfig.getString("fileType");
    private String filePath = MyFtpConfig.getString("filePath");
    private String fileReName = MyFtpConfig.getString("fileReName");

    private FtpClient ftpClient = null;

    public MySunFtp() throws Exception{
        this.ftpClient = new FtpClient(this.hostIp,this.hostPort);
        this.ftpClient.login(this.userName,this.userPwd);
    }

    /**
     * 取得所有的文件列表
     * @return Vector 文件列表集
     * @throws Exception
     */
    public Vector getAllFileList()throws Exception{
        Vector vReturn = new Vector();
        if(userPath != null && userPath.trim().length() > 0 && !userPath.equals("unknow")){
            this.ftpClient.cd(userPath);
        }
        TelnetInputStream input = this.ftpClient.list();
        java.io.BufferedReader bfRead = new BufferedReader(new InputStreamReader(input));
        String strLine = bfRead.readLine();
        while(strLine != null){
                int index = strLine.lastIndexOf(" ");
                if(index != -1){
                    strLine = strLine.substring(index + 1);
                    vReturn.add(strLine.trim());
                }
            strLine = bfRead.readLine();
        }
        bfRead.close();
        input.close();
        return vReturn;

    }


    /**
     * 取指定的文件列表
     * @param getFileType String 文件类型
     * @return Vector 文件列表集
     * @throws Exception
     */
    public Vector getFileList(String getFileType)throws Exception{
        Vector vReturn = new Vector();
        if(userPath != null && userPath.trim().length() > 0 && !userPath.equals("unknow")){
            this.ftpClient.cd(userPath);
        }
        TelnetInputStream input = this.ftpClient.list();
        java.io.BufferedReader bfRead = new BufferedReader(new InputStreamReader(input));
        String strLine = bfRead.readLine();
        while(strLine != null){
            if(strLine.endsWith(getFileType)){
                int index = strLine.lastIndexOf(" ");
                if(index != -1){
                    strLine = strLine.substring(index + 1);
                    vReturn.add(strLine.trim());
                }
            }
            strLine = bfRead.readLine();
        }
        bfRead.close();
        input.close();
        return vReturn;
    }

    /**
     * 取得文件列表 要取的文件类型配置文件中已指出
     * @return Vector 文件列表
     * @throws Exception
     */
    public Vector getFileList() throws Exception{
        Vector vReturn = new Vector();
        if(userPath != null && userPath.trim().length() > 0 && !userPath.equals("unknow")){
            this.ftpClient.cd(userPath);
        }
        TelnetInputStream input = this.ftpClient.list();
        java.io.BufferedReader bfRead = new BufferedReader(new InputStreamReader(input));
        String strLine = bfRead.readLine();
        while(strLine != null){
            if(strLine.endsWith(fileType)){
                int index = strLine.lastIndexOf(" ");
                if(index != -1){
                    strLine = strLine.substring(index + 1);
                    vReturn.add(strLine.trim());
                }
            }
            strLine = bfRead.readLine();
        }
        bfRead.close();
        input.close();
        return vReturn;
    }

    /**
     * 取得一个文件
     * @param fileName String 文件名称
     * @return File 返回该文件
     * @throws Exception
     */
    public File getFile(String fileName) throws Exception{
        File returnFile = new File(filePath + fileName);
        this.ftpClient.binary();
        TelnetInputStream fileInput = this.ftpClient.get(fileName);
        java.io.FileOutputStream fileOut = new FileOutputStream(returnFile);
        byte[] bFile = new byte[1024];
        int iRead = fileInput.read(bFile);
        while(iRead != -1){
            fileOut.write(bFile,0,iRead);
            fileOut.flush();
            iRead = fileInput.read(bFile);
        }
        fileOut.close();
        fileInput.close();
        if(fileReName.trim().length()>0 && !fileReName.equals("unknow")){
            this.ftpClient.rename(fileName,fileName+this.getSystemDate()+ "back");
        }
        return returnFile;
    }

    /**
     * 关闭链接
     * @throws Exception
     */
    public void closeServer()throws Exception{
        this.ftpClient.closeServer();
    }

    /**
     * 取得系统时间
     * @return String
     */
    private static String getSystemDate(){
        java.util.Date date = new java.util.Date();
        java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat(
        "yyyyMMddHHmmss");
        return sdf.format(date);
    }

    /**
     * 删除一个文件,该方法还没测试过
     * @param fileName String 要删除的文件名称
     */
    public void delFile(String fileName){
        this.ftpClient.sendServer("DELE "+fileName+"\r\n");
        String returnMessage = this.ftpClient.getResponseString();
        System.out.println("returnMessage=="+returnMessage) ;
    }

     /*FTP远程命令列表
         USER    PORT    RETR    ALLO    DELE    SITE    XMKD    CDUP    FEAT
         PASS    PASV    STOR    REST    CWD     STAT    RMD     XCUP    OPTS
         ACCT    TYPE    APPE    RNFR    XCWD    HELP    XRMD    STOU    AUTH
         REIN    STRU    SMNT    RNTO    LIST    NOOP    PWD     SIZE    PBSZ
         QUIT    MODE    SYST    ABOR    NLST    MKD     XPWD    MDTM    PROT
         */



    public static void main(String[] args) throws Exception{
        MySunFtp mysunftp = new MySunFtp();
        Vector vFiles = mysunftp.getFileList();
        for(int i=0;i<vFiles.size();i++){
            String fileName = (String)vFiles.get(i);
            File file1 = mysunftp.getFile(fileName);
        }
        mysunftp.closeServer();
    }
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?