• Windows服务器从Linux服务器上以FTP形式获取图片


    Windows服务器上运行一个获取图片的程序,获取图片采用的是FTP方式;

    准备条件:

    Linux服务器上创建一个FTP的用户:ftppic

    这个账号要有权限才可以,然后编写Windows端代码:

    public static boolean downFile(Stringurl,intport, Stringusername,
                    Stringpassword, StringremotePath, StringfileName,Stringpath
                    ) {
               booleansuccess= false;
               FTPClientftp= new FTPClient();
               intreply;
               try{
                    ftp.connect(url,port);
                    ftp.login(username,password);
                    //文件类型为二进制文件
                    ftp.setFileType(FTPClient.BINARY_FILE_TYPE);
                    reply= ftp.getReplyCode();
                    if(!FTPReply.isPositiveCompletion(reply)) {
                         ftp.disconnect();
                         returnsuccess;
                    }
                    //本地模式
                    ftp.enterLocalPassiveMode();
                    ftp.changeWorkingDirectory(remotePath);
                    FTPFile[]fs= ftp.listFiles();
                    for(FTPFileff: fs) {
                         if(ff.getName().equals(fileName)) {
                               FilelocalFile= new File(path+ff.getName());
                               OutputStream  is= new FileOutputStream(localFile);
                               ftp.retrieveFile(ff.getName(),is);
                               is.close();
                         }
                    }
                    ftp.logout();
                    success= true;
               }catch(SocketExceptione) {
                    e.printStackTrace();
               }catch(IOExceptione) {
                    e.printStackTrace();
               }finally{
                    if(ftp.isConnected()) {
                         try{
                               ftp.disconnect();
                         }catch(IOExceptione) {
                               e.printStackTrace();
                         }
                    }
               }
               return success;
         }                                                                                                                        
    

     亲测好使。

  • 相关阅读:
    Ajax中XML和JSON格式的优劣比较
    多语言的网站怎么做呀
    asp.net 2.0多语言网站解决方案
    FrameSet 与 IFrame 彻底剖析
    概要设计说明书
    用C#如何实现大文件的断点上传!
    PowerDesigner生成sql生成不了
    TransactionScope的正确用法
    iframe
    动网转型网游一年获得成功:每月盈利超500万
  • 原文地址:https://www.cnblogs.com/zjiacun/p/6908670.html
Copyright © 2020-2023  润新知