一个实现FTP上传的JAVA类

import java.io.*;
import sun.net.*;
import sun.net.ftp.*;

public class file_upload{
 //定义变量
 String filename;
 FtpClient ftpClient;

 //初始化函数
 public file_upload(){
  filename=null;
  ftpClient=new FtpClient();
 }
 
 //登录ftp服务器
 public void openConnection(String server,int port,String user,String password,String path) throws Exception{
  ftpClient.openServer(server,port);
  ftpClient.login(user,password);
  if(path.length()!=0) ftpClient.cd(path);
  ftpClient.binary();
 }
 
 //退出ftp服务器
 public void closeConnection() throws Exception{
  ftpClient.closeServer();
 }
 
 //上传文件
 public void upload_file(String filepath,String filename) throws Exception{
  File f=new File(filepath);
  FileInputStream is=new FileInputStream(f);
  TelnetOutputStream os=ftpClient.put(filename);
  int bytesRead;
  while((bytesRead=is.read())!=-1) os.write(bytesRead);
  is.close();
  os.close();
 }
}

posted on 2006-12-26 10:02  淘米旺旺  阅读(920)  评论(0编辑  收藏  举报

导航