• 欢迎浏览“String me = Creater\忠实的资深Linux玩家;”,请文明浏览,理性发言,有侵犯你的权益请邮件我(creater@vip.qq.com).
  • 把任何的失败都当作一次尝试,不要自卑;把所有的成功都想成是一种幸运,不要自傲。
  •    2年前 (2016-05-01)  java |   抢沙发  18 
    文章评分 0 次,平均分 0.0

    网络上关于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;
                    }
                }
            }
        }
    }
    
     

    除特别注明外,本站所有文章均为String me = "Creater\忠实的资深Linux玩家";原创,转载请注明出处来自http://unix8.net/home.php/4832.html

    关于

    发表评论

    暂无评论

    切换注册

    登录

    忘记密码 ?

    切换登录

    注册

    扫一扫二维码分享