⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 testtransfer.java

📁 java编写的非常详尽的基于ftp协议的上传下载源码
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/**
 *
 *  Java FTP client library.
 *
 *  Copyright (C) 2000  Enterprise Distributed Technologies Ltd
 *
 *  www.enterprisedt.com
 *
 *  This library is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU Lesser General Public
 *  License as published by the Free Software Foundation; either
 *  version 2.1 of the License, or (at your option) any later version.
 *
 *  This library is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 *  Lesser General Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser General Public
 *  License along with this library; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 *  Bug fixes, suggestions and comments should be should posted on 
 *  http://www.enterprisedt.com/forums/index.php
 *
 *  Change Log:
 *
 *        $Log: TestTransfer.java,v $
 *        Revision 1.15  2008-05-02 07:42:12  bruceb
 *        force quit in finally
 *
 *        Revision 1.14  2007-12-18 07:55:50  bruceb
 *        add finally
 *
 *        Revision 1.13  2007/04/21 04:14:12  bruceb
 *        test unix text files transferred
 *
 *        Revision 1.12  2007/02/26 07:14:34  bruceb
 *        make progress monitor public
 *
 *        Revision 1.11  2007/02/06 07:23:40  bruceb
 *        testNoLocalFilename()
 *
 *        Revision 1.10  2007/01/10 02:39:11  bruceb
 *        added testTransferUnique
 *
 *        Revision 1.9  2006/10/11 08:59:54  hans
 *        Organised imports.
 *
 *        Revision 1.8  2005/10/10 20:43:39  bruceb
 *        append now in FTPClientInterface
 *
 *        Revision 1.7  2005/07/15 17:30:06  bruceb
 *        rework of unit testing structure
 *
 *        Revision 1.6  2005/06/03 11:27:05  bruceb
 *        comment update
 *
 *        Revision 1.5  2004/08/31 10:44:49  bruceb
 *        minor tweaks re compile warnings
 *
 *        Revision 1.4  2004/05/01 17:05:43  bruceb
 *        Logger stuff added
 *
 *        Revision 1.3  2003/11/03 21:18:51  bruceb
 *        added test of progress callback
 *
 *        Revision 1.2  2003/05/31 14:54:05  bruceb
 *        cleaned up unused imports
 *
 *        Revision 1.1  2002/11/19 22:00:15  bruceb
 *        New JUnit test cases
 *
 *
 */

package com.enterprisedt.net.ftp.test;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;

import junit.framework.Test;
import junit.framework.TestSuite;

import com.enterprisedt.net.ftp.FTPException;
import com.enterprisedt.net.ftp.FTPProgressMonitor;
import com.enterprisedt.net.ftp.FTPTransferType;

/**
 *  Test get'ing and put'ing of remote files in various ways
 *
 *  @author     Bruce Blackshaw
 *  @version    $Revision: 1.15 $
 */
public class TestTransfer extends FTPTestCase {

    /**
     *  Revision control id
     */
    public static String cvsId = "@(#)$Id: TestTransfer.java,v 1.15 2008-05-02 07:42:12 bruceb Exp $";

    /**
     *  Get name of log file
     *
     *  @return name of file to log to
     */
    protected String getLogName() {
        return "TestTransfer.log";
    }

    /**
     *  Test transfering a binary file
     */
    public void testTransferBinary() throws Exception {

        log.info("testTransferBinary()");
        
        try {

            connect();
            
            // monitor transfer progress
            ftp.setProgressMonitor(new TestProgressMonitor(), 250000);
    
            // move to test directory
            ftp.chdir(testdir);
            ftp.setType(FTPTransferType.BINARY);
            
            // put to a random filename
            String filename = generateRandomFilename();
            ftp.put(localDataDir + localBinaryFile, filename);
    
            // get it back        
            ftp.get(localDataDir + filename, filename);
    
            // delete remote file
            ftp.delete(filename);
            try {
                ftp.modtime(filename);
                fail(filename + " should not be found");
            }
            catch (IOException ex) {
                log.info("Expected exception: " + ex.getMessage());
            }
            catch (FTPException ex) {
                log.info("Expected exception: " + ex.getMessage());
            }
    
            // check equality of local files
            assertIdentical(localDataDir + localBinaryFile, localDataDir + filename);
    
            // and delete local file
            File local = new File(localDataDir + filename);
            local.delete();
    
            ftp.quit();
        }
        finally {
            ftp.quitImmediately();
        }
    }
    
    
    /**
     *  Test transfering using the server to generate a unique
     *  file name
     */
    public void testTransferUnique() throws Exception {

        log.info("testTransferUnique()");
        
        try {

            connect();
            
            // monitor transfer progress
            ftp.setProgressMonitor(new TestProgressMonitor(), 250000);
    
            // move to test directory
            ftp.chdir(testdir);
            ftp.setType(FTPTransferType.BINARY);
            
            // put to a random filename
            String filename = ftp.put(localDataDir + localBinaryFile, null);
            log.info("Put file to '" + filename + "'");
    
            // get it back        
            ftp.get(localDataDir + filename, filename);
    
            // delete remote file
            ftp.delete(filename);
            try {
                ftp.modtime(filename);
                fail(filename + " should not be found");
            }
            catch (IOException ex) {
                log.info("Expected exception: " + ex.getMessage());
            }
            catch (FTPException ex) {
                log.info("Expected exception: " + ex.getMessage());
            }
    
            // check equality of local files
            assertIdentical(localDataDir + localBinaryFile, localDataDir + filename);
    
            // and delete local file
            File local = new File(localDataDir + filename);
            local.delete();
    
            ftp.quit();
        }
        finally {
            ftp.quitImmediately();
        }
    }
    
    /**
     *  Test transfering by only supplying a local dir, not a
     *  full pathname
     */
    public void testNoLocalFilename() throws Exception {

        log.info("testNoLocalFilename()");
        
        try {

            connect();
            
            // monitor transfer progress
            ftp.setProgressMonitor(new TestProgressMonitor(), 250000);
    
            // move to test directory
            ftp.chdir(testdir);
            ftp.setType(FTPTransferType.BINARY);
            
            // put to a random filename
            String filename = generateRandomFilename();
            ftp.put(localDataDir + localBinaryFile, filename);
            log.info("Put file to '" + filename + "'");
    
            // get it back        
            ftp.get(localDataDir, filename);
    
            // delete remote file
            ftp.delete(filename);
            try {
                ftp.modtime(filename);
                fail(filename + " should not be found");
            }
            catch (IOException ex) {
                log.info("Expected exception: " + ex.getMessage());
            }
            catch (FTPException ex) {
                log.info("Expected exception: " + ex.getMessage());
            }
    
            // check equality of local files
            assertIdentical(localDataDir + localBinaryFile, localDataDir + filename);
    
            // and delete local file
            File local = new File(localDataDir + filename);
            local.delete();
    
            ftp.quit();
        }
        finally {
            ftp.quitImmediately();
        }
    }

    /**
     *  Test transfering a text file
     */
    public void testTransferText() throws Exception {

        log.info("testTransferText()");
        
        try {

            connect();
            
            // monitor transfer progress
            ftp.setProgressMonitor(new TestProgressMonitor(), 250000);        
    
            // move to test directory
            ftp.chdir(testdir);
            ftp.setType(FTPTransferType.ASCII);
    
            // put to a random filename
            String filename = generateRandomFilename();
            ftp.put(localDataDir + localTextFile, filename);
    
            // get it back        
            ftp.get(localDataDir + filename, filename);
    
            // delete remote file
            ftp.delete(filename);
            try {
                ftp.modtime(filename);
                fail(filename + " should not be found");
            }
            catch (IOException ex) {
                log.info("Expected exception: " + ex.getMessage());
            }
            catch (FTPException ex) {
                log.info("Expected exception: " + ex.getMessage());
            }
    
            // check equality of local files
            assertIdentical(localDataDir + localTextFile, localDataDir + filename);
    
            // and delete local file
            File local = new File(localDataDir + filename);
            local.delete();
    
            ftp.quit();
        }
        finally {
            ftp.quitImmediately();
        }
    }
    
    
    /**
     *  Test transfering a text file
     */

⌨️ 快捷键说明

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