<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    問征夫以前路
    感謝所有關心過支持過我的人, 感謝所有恨過我嘲笑過我的人 !
    posts - 30,comments - 147,trackbacks - 0

            近日在做FTP的上傳下載功能,此功能幾經周折,終于實現單線程和多線程文件的上傳和下載,我現在把代碼上傳,算是一個OK的demo

    package com.cotel.service.gather;

    import java.io.BufferedReader;
    import java.io.DataOutputStream;
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.InputStreamReader;
    import java.text.ParseException;
    import java.util.ArrayList;
    import java.util.Properties;
    import org.apache.log4j.*;
    import com.cotel.parse.ParseXML;
    import com.cotel.util.FileFinder;

    import sun.net.TelnetInputStream;
    import sun.net.TelnetOutputStream;
    import sun.net.ftp.FtpClient;


    /**
     * 
     * 功能描述:FTP上傳和下載
     *      
     * 
    @author <a href="mailto:zhanghhui@126.com">KenZhang</a>
     * 
    @version 1.0 
     * Creation date: 2007-8-22 - 下午04:57:32
     
    */

    public class GatherServiceImpl implements IGatherService{
        
    //引入日志
        private static Logger log = (Logger)Logger.getLogger(GatherServiceImpl.class);
         
    /** 
         * 類的初始化,建立ftp的連接,用戶登錄,指定ftp的傳輸流
         * 
    @param host
         * 
    @param port
         * 
    @param user
         * 
    @param psw
         
    */

    //    建立一條與指定主機、指定端口上的FTP服務器的連接
        private FtpClient aftp = new FtpClient();
        
    private DataOutputStream outputs;
        
    private TelnetOutputStream outs;
        
    private TelnetInputStream inps;
        
        
    //此路徑只應用于在java Application里面進行調試
        private static String path = System.getProperty("user.dir"+ "\\WebRoot\\WEB-INF\\classes\\charging\\";

        
    //在tomcat里面運行時用下面路徑----
    //    private static String path = GatherServiceImpl.class.getClass().getResource("/").getPath()+ "charging";
        
        
        
    public void FTPClass(String host,String port,String user,String psw,String url){
            
            
    try{
                
    //   注冊到FTP服務器
                aftp.openServer(host);
                log.debug(
    "登陸.");
                aftp.login(user,psw);
                log.debug(
    "登錄FTP服務器成功!");
                aftp.binary();
            }
    catch(IOException e){
                log.debug(
    "連接FTP服務器失敗!");
                e.printStackTrace();
            }

        }

            
        
    /** 
         * 通過ftp上傳文件到服務器上
         * 
    @param localFile 本地所要上傳的文件
         * 
    @param remoteFile 傳到服務器上的文件名稱
         
    */

        
    public boolean upFile(String localFile,String remoteFile){
            
    boolean result = true;
            
    if(aftp != null){
                log.debug(
    "正在上傳文件"+localFile+",請稍等.");
                
    try{
                    File file 
    = new File(localFile);
                    outs 
    = aftp.put(remoteFile);
                    FileInputStream in 
    = new FileInputStream(file);
                    
    byte[] bytes = new byte[1024];
                    
    int c;
                    
    while((c = in.read(bytes))!=-1){
                        outs.write(bytes,
    0,c);
                        }

                    outs.close();
                    in.close();
                    log.debug(
    "上傳文件"+localFile+"成功!");
                    log.debug(
    "上傳文件所在目錄:"+remoteFile+"");
                    }
    catch(Exception e){
                        e.printStackTrace();
                        log.debug(
    "上傳文件"+localFile+"失敗!");
                        result 
    = false;
                    }

                }
    else{
                    result 
    = false;
                    }

                    
    return result;
            }

            
        
    /** 
         * 下載FTP服務器上的文件
         * 
    @param localFile 本地文件名
         * 
    @param remoteFile 遠程服務器文件名
         
    */

        
    public boolean downFile(String remoteFile,String localFile){
            
    boolean result = true;
            log.debug(
    "begin");
            
    if(aftp!=null){
                log.debug(
    "正在下載文件"+remoteFile+",請等待");
                
    try
                    log.debug("===" + remoteFile);
                    inps 
    = aftp.get(remoteFile);                    
                    FileInputStream in 
    = null;
                    FileOutputStream os;
                    
    if(inps != null){                    
                        os 
    = new FileOutputStream(localFile);
                        
    byte[] bytes = new byte [1024];
                        
    int c ;
                        
    while ((c = inps.read(bytes))!=-1){
                        os.write(bytes,
    0,c);
                        log.debug(c);
                        }

                    inps.close();
                    os.close();
                    }
    else
                        log.debug(
    "file not exist!");
                inps.close();
            
                log.debug(
    "下載文件"+localFile+"成功!");
                log.debug(
    "下載文件所在目錄:"+localFile+"");
                }
    catch(Exception e){
                        e.printStackTrace();
                        log.debug(
    "下載文件"+localFile+"失敗!");
                        result 
    = false;
                    }

                }

                
    return false;
            }
        
        
        
    /**
         * 斷開ftp連接
         * 
    @throws IOException 
         *
         
    */

        
    public void disconnect() throws IOException{
            aftp.closeServer();
            log.debug(
    "FTP服務器連接斷開!");
            }
        
        
        
    // 返回當前目錄的所有文件及文件夾
        public ArrayList getFileList() throws IOException{
            BufferedReader dr 
    = new BufferedReader(new InputStreamReader(aftp.nameList("*.txt")));
            ArrayList al 
    = new ArrayList();
            String s 
    = "";
            
    while ((s=dr.readLine())!=null){
                al.add(s);
                }

            
    return al;
            }


        
    /**    main方法測試
         * 
    @param args
         * 
    @throws IOException
         * 
    @author <a href="mailto:zhanghhui@126.com">KenZhang</a>
         * Creation date: 2007-8-23 - 上午10:11:03
         
    */

        
    public static void main(String[] args) throws IOException{
            GatherServiceImpl gatherService 
    = new GatherServiceImpl();
            log.debug(
    "begin");
            
             
    //從配置文件中獲得監聽端口和發送端口
              Properties p = new Properties();
              
    //讀取配置文件
              InputStream in = GatherServiceImpl.class.getResourceAsStream(
                  
    "/config.properties");
              p.load(in);
              in.close();
              String ftpHostIP 
    = p.getProperty("ftpHostIP");
              String ftpPort 
    = p.getProperty("ftpPort");
              String ftpUserName 
    = p.getProperty("ftpUserName");
              String ftpPassWord 
    = p.getProperty("ftpPassWord");
              String ftpPath 
    = p.getProperty("ftpPath");
              gatherService.FTPClass(ftpHostIP, ftpPort, ftpUserName, ftpPassWord, ftpPath);
              
              
    //文件批量下載,把擴展名為.txt的所有文件一次下載到服務器
              ArrayList list = gatherService.getFileList();
              
    for(int i = 0; i < list.size(); i++){
                  String fileName 
    = null;
                  fileName 
    = (String)list.get(i);
                  
    //System.out.println("-------"+fileName);
                  gatherService.downFile(fileName,path+fileName);
              }

              
            
    //文件進行批量上傳,把擴展名為.txt的所有文件一次上傳到服務器
            FileFinder fileFinder = new FileFinder();
            File[] files 
    = fileFinder.getFilesBySuffix(".txt", path);
            
    for (int i = 0; i < files.length; i++{            
                System.out.println(
    "-------"+files[i].getName());
                gatherService.upFile(path
    +files[i].getName(),files[i].getName());
            }

            
            
    //從ftp服務器下載文件
            
    //  gatherService.downFile("地標信息.txt",path+"地標信息.txt");
            
    //上傳文件到ftp服務器
            
    //ftpClient.upFile(path+"地標信息.txt", "ftproot/地標信息.txt");
            
            
    //與FTP服務器連接斷開
              gatherService.disconnect();

    }

             代碼改動很多,才實現了我想要的需求。用java Application運行的時候是不是有緩存呢?有的話,這個緩存怎么去呢?我都是再把程序重新編譯一次,再執行。好像起點作用!
    posted on 2007-08-31 16:01 kenzhang 閱讀(647) 評論(1)  編輯  收藏

    FeedBack:
    # re: FTP實現上傳下載
    2007-12-01 22:00 | 未來之我制作
    好啊 這些代碼恰到好處啊

    我要以此作為參考代碼學習啊   回復  更多評論
      

    只有注冊用戶登錄后才能發表評論。


    網站導航:
     
    主站蜘蛛池模板: 视频一区在线免费观看| 99精品一区二区免费视频| 亚洲无码在线播放| 亚洲免费在线视频观看| 无码一区二区三区亚洲人妻| 日本亚洲欧洲免费天堂午夜看片女人员 | 亚洲s码欧洲m码吹潮| 在线观看亚洲成人| 99久久久国产精品免费无卡顿| 蜜臀亚洲AV无码精品国产午夜.| 久久国产亚洲精品麻豆| 免费无码AV电影在线观看| 国产精品美女免费视频观看| 亚洲午夜在线一区| 亚洲一区二区三区无码中文字幕 | 亚洲第一黄片大全| 麻豆高清免费国产一区| 一级特黄特色的免费大片视频| 亚洲毛片免费观看| 国产亚洲一区区二区在线| 成人免费a级毛片| 久久国产乱子精品免费女| 亚洲日韩在线中文字幕综合 | 亚洲综合区图片小说区| 亚洲色偷偷狠狠综合网| 免费看国产成年无码AV片| 国产午夜精品免费一区二区三区 | 成人免费视频小说| 无码人妻一区二区三区免费n鬼沢 无码人妻一区二区三区免费看 | 香蕉视频在线观看免费国产婷婷 | 免费人成网站在线观看10分钟| 黄色视频在线免费观看| 日韩亚洲人成网站| 亚洲av乱码一区二区三区 | 人人公开免费超级碰碰碰视频| 亚洲免费福利在线视频| 亚洲精品一卡2卡3卡三卡四卡| 亚洲精品无码国产| 亚洲一级特黄无码片| 免费吃奶摸下激烈视频| 真实乱视频国产免费观看|