zoukankan      html  css  js  c++  java
  • ftp上传文件

    1.连接ftp服务器

    a)在linux命令行下输入:ftp 10.18.34.115 

    b)服务器询问你用户名和口令,分别输入yint和相应密码,待认证通过即可。

     

        或者用下面的格式  

       ftp - -i  -n  IP_ADDRESS

      user USERNAME PASSWORD

        比如:

        ftp -i -n 172.17.17.17

        user  PUB    123456

    2.上传文件代码:

      1 package com.sitech.throwbill.easyask.classify;
      2 
      3 import java.io.File;
      4 import java.io.FileInputStream;
      5 import java.util.HashMap;
      6 import java.util.Map;
      7 
      8 import org.apache.commons.net.ftp.FTPClient;
      9 import org.apache.commons.net.ftp.FTPReply;
     10 import org.springframework.stereotype.Service;
     11 
     12 
     13 
     14 @Service()
     15 public class Test {
     16     private static FTPClient ftp;  
     17     public static void main(String[] args) {
     18 
     19         Test t = new Test();
     20         try {
      22             t.inputftp("134.96.177.92", "test", "test", "D:/test", "C:/Users/Administrator/Desktop/app-redis.xml");
     23             System.out.println("jiehsu ");
     24         } catch (Exception e) {
     25             e.printStackTrace();
     26         }
     27 
     28     }
     29    57 
     58     public void inputftp(String ip,String userName,String pwd,String remotePath,String localPath) throws Exception{
     59         Ftp f=new Ftp();
     60         f.setIpAddr(ip);
     61         f.setUserName(userName);
     62         f.setPwd(pwd);
     63         f.setPath(remotePath);
     64         Test.connectFtp(f);
     65         File file = new File(localPath);
     66         Test.upload(file);//把文件上传在ftp上
     67     }
     68 
     69     /**
     70      * 获取ftp连接
     71      * @param f
     72      * @return
     73      * @throws Exception
     74      */
     75     public static boolean connectFtp(Ftp f) throws Exception{
     76         ftp=new FTPClient();
     77         boolean flag=false;
     78         int reply;
     79         if (f.getPort()==null) {
     80             ftp.connect(f.getIpAddr(),21);
     81         }else{
     82             ftp.connect(f.getIpAddr(),f.getPort());
     83         }
     84         ftp.login(f.getUserName(), f.getPwd());
     85         ftp.setFileType(FTPClient.BINARY_FILE_TYPE);
     86         reply = ftp.getReplyCode();      
     87         if (!FTPReply.isPositiveCompletion(reply)) {      
     88             ftp.disconnect();      
     89             return flag;      
     90         }      
     91         ftp.changeWorkingDirectory(f.getPath());      
     92         flag = true;      
     93         return flag;
     94     }
     95 
     96     /**
     97      * ftp上传文件
     98      * @param f
     99      * @throws Exception
    100      */
    101     public static void upload(File f) throws Exception{
    102         if (f.isDirectory()) {
    103             ftp.makeDirectory(f.getName());
    104             ftp.changeWorkingDirectory(f.getName());
    105             String[] files=f.list();
    106             for(String fstr : files){
    107                 File file1=new File(f.getPath()+"/"+fstr);
    108                 if (file1.isDirectory()) {
    109                     upload(file1);
    110                     ftp.changeToParentDirectory();
    111                 }else{
    112                     File file2=new File(f.getPath()+"/"+fstr);
    113                     FileInputStream input=new FileInputStream(file2);
    114                     ftp.enterLocalPassiveMode();
    115                     ftp.storeFile(file2.getName(),input);
    116                     input.close();
    117                 }
    118             }
    119         }else{
    120             File file2=new File(f.getPath());
    121             FileInputStream input=new FileInputStream(file2);
    122             ftp.enterLocalPassiveMode();
    123             ftp.storeFile(file2.getName(),input);
    124             input.close();
    125         }
    126     }
    127 }

     跟多关于linux下ftp相关命令:http://m.blog.csdn.net/article/details?id=5754646

  • 相关阅读:
    10 个深恶痛绝的 Java 异常。。
    为什么公司宁愿 25K 重新招人,也不给你加到 20K?原因太现实……
    推荐一款代码神器,代码量至少省一半!
    Spring Cloud Greenwich 正式发布,Hystrix 即将寿终正寝。。
    hdu 3853 LOOPS(概率 dp 期望)
    hdu 5245 Joyful(期望的计算,好题)
    hdu 4336 Card Collector(期望 dp 状态压缩)
    hdu 4405 Aeroplane chess(概率+dp)
    hdu 5036 Explosion(概率期望+bitset)
    hdu 5033 Building (单调栈 或 暴力枚举 )
  • 原文地址:https://www.cnblogs.com/chafe/p/6514469.html
Copyright © 2011-2022 走看看