zoukankan      html  css  js  c++  java
  • 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;
         }                                                                                                                        
    

     亲测好使。

  • 相关阅读:
    基于XMPP协议的Android即时通信系
    codeforces 830 B Cards Sorting
    [SCOI2007]修车
    spoj GSS4
    hdu 3652 B-number
    【模版】多项式乘法 FFT
    hdu 3642 Get The Treasury
    hdu 1255 覆盖的面积
    hdu 4553 约会安排
    UVA-11992 Fast Matrix Operations
  • 原文地址:https://www.cnblogs.com/zjiacun/p/6908670.html
Copyright © 2011-2022 走看看