posts - 0, comments - 77, trackbacks - 0, articles - 356
            BlogJava :: 首頁 :: 新隨筆 :: 聯(lián)系 :: 聚合  :: 管理

          FtpClient的上傳,下載等操作

          Posted on 2008-03-22 14:38 semovy 閱讀(809) 評論(0)  編輯  收藏 所屬分類: JAVA應(yīng)用

          package com.semovy.test;

          import java.io.BufferedReader;
          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.io.OutputStream;

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

          public class FTPTest extends FtpClient {

           public void connectServer(String server, String user, String password,
             String path) throws Exception {
            openServer(server);
            login(user, password);
            System.out.println("login   success!");
            if (path.length() != 0)
             cd(path);
            binary();
           }

           // 向ftp服務(wù)器發(fā)送操作命令
           public void sendCommand(String command) throws IOException {
            issueCommand(command);
           }

           // 刪除文件
           public void deleteFile(String fileName) throws IOException {
            issueCommand("DELE " + fileName);
           }

           public void closeConnect() {
            try {
             closeServer();
            } catch (IOException ex) {
             ex.printStackTrace();
            }
           }

           public void upload(String localFilePath, String remoteFilePath) {
            try {
             TelnetOutputStream os = put(remoteFilePath);
             File file_in = new File(localFilePath);
             FileInputStream is = new FileInputStream(file_in);
             byte[] bytes = new byte[1024];
             int ch;
             while ((ch = is.read(bytes)) != -1) {
              os.write(bytes, 0, ch);
             }
             is.close();
             os.close();
             System.out.println("upload file successfully.");
            } catch (IOException e) {
             e.printStackTrace();
            }
           }

           public void download(String localFilePath, String remoteFilePath) {
            try {
             int ch;
             File fi = new File(localFilePath);
             OutputStream out = new FileOutputStream(fi);
             TelnetInputStream fget = get(remoteFilePath);
             byte[] b = new byte[1024];
             int len = 0;
             while ((len = fget.read(b)) != -1) {
              out.write(b, 0, len);
             }
             fget.close();
             out.close();
             System.out.println("download file successfully.");
            } catch (IOException e) {
             e.printStackTrace();
            }
           }
           public static void list(FtpClient ftp)throws Exception
           {
            InputStream in = ftp.list();
            BufferedReader reader = new BufferedReader(new InputStreamReader(in));
            String temp = null;
            while((temp = reader.readLine()) != null)
            {
             
             System.out.println(temp);
            }
            in.close();
           }
           public static void main(String[] args) throws Exception {
            FTPTest ftp = new FTPTest();
            ftp.connectServer("teckotooling.vicp.net", "admin", "admin", "");
            String localFilePath = "d:/iReport-2.0.5-windows-installer.exe";
            String remoteFilePath = "iReport-2.0.5-windows-installer.exe";
            ftp.upload(localFilePath,remoteFilePath);
            ftp.download(localFilePath, remoteFilePath);
            ftp.deleteFile("AI_CS3_chs.exe");
            ftp.rename("中草藥", "一起走過的日子");
            list(ftp);
            ftp.cd("一起走過的日子");
            list(ftp);
            ftp.cdUp();
            list(ftp);
            System.out.println("ftp.getLocalAddress(): " + ftp.getLocalAddress());
            System.out.println("ftp.welcomeMsg: " + ftp.welcomeMsg);
            System.out.println("ftp.getConnectTimeout(): " + ftp.getConnectTimeout());
            System.out.println("ftp.getReadTimeout(): " + ftp.getReadTimeout());
            System.out.println("ftp.system(): " + ftp.system());
            System.out.println("ftp.FTP_PORT: " + ftp.FTP_PORT);
            System.out.println("ftp.encoding: " + ftp.encoding);
            System.out.println("ftp.getFtpProxyHost(): " + ftp.getFtpProxyHost());
            System.out.println("ftp.serverIsOpen(): " + ftp.serverIsOpen());
            ftp.closeConnect();
           }
          }

          主站蜘蛛池模板: 鸡泽县| 开远市| 来宾市| 三门峡市| 龙里县| 曲沃县| 山阴县| 锡林郭勒盟| 仪征市| 丰顺县| 柳州市| 巴林左旗| 桦川县| 浮山县| 萨迦县| 长沙市| 阿拉善右旗| 扎兰屯市| 阳原县| 江门市| 宁夏| 光泽县| 福建省| 电白县| 四平市| 崇义县| 靖安县| 自贡市| 黔西县| 子长县| 麻江县| 景洪市| 从化市| 绵竹市| 鹿邑县| 句容市| 芮城县| 邻水| 临沂市| 乐清市| 云林县|