• 用Java写个ftp传输类实现文件的上传和下载,用ikvmc转成dll


    1.Java类:

    package com.wjy.ftp.transmission;
    
    import java.io.File;
    import java.io.FileOutputStream;
    import java.io.OutputStream;
    import java.io.StringBufferInputStream;
    
    import org.apache.commons.net.ftp.FTPClient;
    import org.apache.commons.net.ftp.FTPFile;
    import org.apache.commons.net.ftp.FTPReply;
    
    public class FtpTransmission {
        private String serverUrl=null;
        private String userName=null;
        private String password=null;
        private String storePath=null;
        private int port=0;
        
        public FtpTransmission(String serverUrl, String userNameString,
                String password, String storePath, int port) {
            super();
            this.serverUrl = serverUrl;
            this.userName = userNameString;
            this.password = password;
            this.storePath = storePath;
            this.port = port;
        }
    
        public String getServerUrl() {
            return serverUrl;
        }
    
        public void setServerUrl(String serverUrl) {
            this.serverUrl = serverUrl;
        }
    
        public String getUserNameString() {
            return userName;
        }
    
        public void setUserNameString(String userNameString) {
            this.userName = userNameString;
        }
    
        public String getPassword() {
            return password;
        }
    
        public void setPassword(String password) {
            this.password = password;
        }
    
        public String getStorePath() {
            return storePath;
        }
    
        public void setStorePath(String storePath) {
            this.storePath = storePath;
        }
    
        public int getPort() {
            return port;
        }
    
        public void setPort(int port) {
            this.port = port;
        }
    
        /**
         * Description:实现ftp的文件上传
         * @author 王吉元
         * @Version 1.0 Dec 16,2013
         * @param fileName:上传的文件名称
         * @param fileContent:文件的内容
         * @return  成功返回ture,失败返回false
         */
        public boolean fileUpLoad(String fileName,String contents){
            boolean isSuccessed=false;
            FTPClient ftpClient=new FTPClient();
            StringBufferInputStream input=new StringBufferInputStream(contents);
            try {
                int reply=0;
                ftpClient.connect(serverUrl,port);
                ftpClient.login(userName, password);
                reply=ftpClient.getReplyCode();
                if(!FTPReply.isPositiveCompletion(reply)){
                    ftpClient.disconnect();
                    return isSuccessed;
                }
                ftpClient.changeWorkingDirectory(storePath);
                ftpClient.storeFile(fileName, input);
                
                input.close();
                ftpClient.logout();
                isSuccessed=true;
            } catch (Exception e) {
                // TODO: handle exception
                e.printStackTrace();
            }finally{
                if(ftpClient.isConnected()){
                    try {
                        ftpClient.disconnect();
                    } catch (Exception e2) {
                        // TODO: handle exception
                        e2.printStackTrace();
                    }
                }
            }
            return isSuccessed;
        }
        /**
         * Description:实现ftp的文件下载
         * @author 王吉元
         * @Version 1.0 Dec 16,2013
         * @param fileName:下载的文件名称
         * @param localPath:下载后保存在本地的路径
         * @return  成功返回ture,失败返回false
         */
        public boolean fileDownload(String fileName,String localPath){
            boolean isSuccessed=false;
            FTPClient ftpClient=new FTPClient();
            try {
                int reply=0;
                ftpClient.connect(serverUrl,port);
                ftpClient.login(userName, password);
                reply=ftpClient.getReplyCode();
                if(!FTPReply.isPositiveCompletion(reply)){
                    ftpClient.disconnect();
                    return isSuccessed;
                }
                ftpClient.changeWorkingDirectory(storePath);
                
                FTPFile[] files=ftpClient.listFiles();
                for(FTPFile file : files){
                    if(file.getName().equals(fileName)){
                        File localFile=new File(localPath+"/"+file.getName());
                        OutputStream outputStream=new FileOutputStream(localFile);
                        
                        ftpClient.retrieveFile(file.getName(), outputStream);
                        outputStream.close();
                    }
                }
                
                ftpClient.logout();
                isSuccessed=true;
            } catch (Exception e) {
                // TODO: handle exception
                e.printStackTrace();
            }finally{
                if(ftpClient.isConnected()){
                    try {
                        ftpClient.disconnect();
                    } catch (Exception e2) {
                        // TODO: handle exception
                        e2.printStackTrace();
                    }
                }
            }
            return isSuccessed;
        }
    }

    2.Java的jar测试:(被注释掉的部分是上传测试代码)

    package com.wjy.main;
    
    import java.io.File;
    import java.io.FileInputStream;
    
    import com.wjy.ftp.transmission.FtpTransmission;
    
    public class TestMain {
    //    public static void main(String args[]){
    //        FtpTransmission ftpTransmission=new FtpTransmission("10.13.30.22", "wjy", "wjywjy", "./tools/", 21);
    //        try {
    //            StringBuilder stringBuilder=new StringBuilder();
    //            FileInputStream fileInputStream=new FileInputStream(new File("E://testmodel.cld"));
    //            int cc;
    //            while((cc=fileInputStream.read())!=-1){
    //                stringBuilder.append((char)cc);
    //            }
    //            System.out.println(stringBuilder);
    //            boolean flag=ftpTransmission.fileUpLoad("testmodel.cld", stringBuilder.toString());
    //            System.out.println(flag);
    //        } catch (Exception e) {
    //            // TODO: handle exception
    //            e.printStackTrace();
    //        }
    //    }
        
        public static void main(String args[]){
        FtpTransmission ftpTransmission=new FtpTransmission("10.13.30.22", "wjy", "wjywjy", "./tools/", 21);
        try {
            boolean flag=ftpTransmission.fileDownload("ctest.txt","F://NB");
            System.out.println(flag);
        } catch (Exception e) {
            // TODO: handle exception
            e.printStackTrace();
        }
    }
    }

    3.C#的dll(由ikvmc转成)测试:(被注释掉的部分是上传测试代码)

    using System;
    using System.IO;
    using System.Collections.Generic;
    using System.Text;
    using com.wjy.ftp.transmission;
    namespace FTPTransJarTest
    {
        class Program
        {
            //static void Main(string[] args)
            //{
                
            //FtpTransmission ftpTransmission=new FtpTransmission("10.13.30.22", "wjy", "wjywjy", "./tools/", 21);
            //try {
            //    StringBuilder stringBuilder=new StringBuilder();
            //    StreamReader file = new StreamReader("E://testmodel.cld");
            //    int cc;
            //    while((cc=file.Read())!=-1){
            //        stringBuilder.Append((char)cc);
            //    }
            //    Boolean flag = ftpTransmission.fileUpLoad("ctest.cld",stringBuilder.ToString());
            //    Console.Write(flag);
            //} catch (Exception e) {
            //    // TODO: handle exception
            //    Console.Write(e.Message);
            //}
        
            //}
    
            static void Main(string[] args)
            {
    
                FtpTransmission ftpTransmission = new FtpTransmission("10.13.30.22", "wjy", "wjywjy", "./tools/", 21);
                try
                {
                    Boolean flag = ftpTransmission.fileDownload("ctest.txt", "F://NB");
                    Console.Write(flag);
                }
                catch (Exception e)
                {
                    // TODO: handle exception
                    Console.Write(e.Message);
                }
    
            }
        }
    }
  • 相关阅读:
    android IntentService生命周期问题
    日志
    python for android : BeautifulSoup 有 bug
    光电耦合器简单介绍以及作用
    cocos2dx 3.1从零学习(五)——动画
    openssl之EVP系列之9---EVP_Digest系列函数的一个样例
    html5 SVG
    CSS选择器
    ISCC2014-reverse
    哇塞!HTML5 实现的雨滴效果 CSS发抖
  • 原文地址:https://www.cnblogs.com/wangjiyuan/p/jarftp1.html
Copyright © 2020-2023  润新知