java ftp操作

2016年5月1日 由 Creater 留言 »

网络上关于java ftp的操作代码很杂很乱,没有一个比较正式的实例,刚好项目上需要,在github上搜索了这个easyFTP(链接:https://github.com/unix8net/easyFTP),将其中的easyFTP.java提取出来再修改修改,感觉非常好用。

测试代码:

			easyFTP ftp = new easyFTP();
			ftp.connect("118.123.18.73", "lanlan1989", "88888888");
			boolean status = false;

			// 远程ftp工作目录
			status = ftp.setWorkingDirectory("/lanlan1989/others/");
			
			// 打印出ftp目录下的文件
			String[] names = ftp.listName();
			System.out.println(Arrays.toString(names));

			// 本地文件上传:本地文件+上传后保存的文件名
			ftp.uploadFile("E:\\20160412110527","20160412110527");

			// 下载文件:远程文件名+本地路径与文件名
			ftp.downloadFile("easyFTP.java.cpp", "E:\\easyFTP.java.cpp.txt");

			ftp.disconnect();

附easyFTP.java:

package net.unix8.ftp;

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

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.SocketException;
import java.net.UnknownHostException;

/**
 * A simple FTPCeint WRAPPER CLASS to perform basic Upload/Download Operations
 * Author : ADEEL AHMAD
 */

public class easyFTP {

    private FTPClient mFtpClient =null;

    public easyFTP(){
        mFtpClient=new FTPClient();
        this.mFtpClient.setConnectTimeout(10*1000);
    }

    public void setFtpClient(FTPClient mFtpClient) {
        this.mFtpClient = mFtpClient;
    }

    public void useCompressedTransfer (){
        try {
            mFtpClient.setFileTransferMode(org.apache.commons.net.ftp.FTP.COMPRESSED_TRANSFER_MODE);
        }catch (Exception e){
            e.printStackTrace();
        };
    }

    public String [] listName () throws Exception {
        try{
            return mFtpClient.listNames();
        }catch (Exception e){
            throw e;
        }
    }

    public boolean setWorkingDirectory (String dir)throws  Exception{
        try{
            return mFtpClient.changeWorkingDirectory(dir);
        }catch (Exception e){
            throw e;
        }
    }

    public FTPClient getFtpClient() {return mFtpClient;}

    public void setTimeout (int seconds) throws  Exception{
        try {
            mFtpClient.setConnectTimeout(seconds * 1000);
        }catch (Exception e){
            throw e;
        }
    }

    public boolean makeDir(String dir) throws Exception {
        try {
            return mFtpClient.makeDirectory(dir);
        }catch (Exception e){
            throw e;
        }
    }

    public void disconnect(){
        try {
            mFtpClient.disconnect();
        }catch (Exception e ){
            e.printStackTrace();
        }
    }

    public void connect(String ip, String userName, String pass) throws Exception{
        boolean status = false;
        try {
            try {
                mFtpClient.connect(ip);
            }catch (Exception e){
                e.printStackTrace();
            }
            status = mFtpClient.login(userName, pass);
            //Log.e("isEasyFTPConnected", String.valueOf(status));
        } catch (SocketException e) {
            throw e;
        }
        catch (UnknownHostException e) {
            throw e;
        }
        catch (IOException e) {
            throw e;
        }
    } 

 //Passing Local File path/Uri
   public void uploadFile(String uri,String name) throws  Exception{
        try {
            File file = new File(uri);
            mFtpClient.setFileType(org.apache.commons.net.ftp.FTP.BINARY_FILE_TYPE);
            FileInputStream srcFileStream = new FileInputStream(file);
            boolean status = mFtpClient.storeFile(name, srcFileStream);
            //Log.e("Status", String.valueOf(status));
            srcFileStream.close();
        } catch (Exception e) {
            throw e;
        }
    }

//Passing InputStream and fileName
    public void uploadFile(InputStream srcFileStream, String name) throws  Exception{
        try {
            mFtpClient.setFileType(org.apache.commons.net.ftp.FTP.BINARY_FILE_TYPE);
            boolean status = mFtpClient.storeFile(name, srcFileStream);
            //Log.e("Status", String.valueOf(status));
            srcFileStream.close();
        } catch (Exception e) {
            throw e;
        }
    }

    public void downloadFile(String remoteFilePath, String dest)  throws Exception{
        File downloadFile=new File(dest);
        File parentDir = downloadFile.getParentFile();
        if (!parentDir.exists())
            parentDir.mkdir();
        OutputStream outputStream = null;
        try {
            outputStream = new BufferedOutputStream(new FileOutputStream(downloadFile));
            mFtpClient.setFileType(FTPClient.BINARY_FILE_TYPE);
            boolean status= mFtpClient.retrieveFile(remoteFilePath, outputStream);
            //Log.e("Status", String.valueOf(status));
        } catch (Exception e) {
            throw e;
        }
        finally {
            if (outputStream != null) {
                try {
                    outputStream.close();
                } catch (IOException e) {
                    throw e;
                }
            }
        }
    }
}
广告位

发表评论

你必须 登陆 方可发表评论.