posts - 0, comments - 77, trackbacks - 0, articles - 356
  BlogJava :: 首页 :: 新随笔 :: 联系 :: 聚合  :: 管理

FtpClient的上传,下载等操作

Posted on 2008-03-22 14:38 semovy 阅读(802) 评论(0)  编辑  收藏 所属分类: JAVA应用

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服务器发送操作命令
 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();
 }
}


只有注册用户登录后才能发表评论。


网站导航: