java中FTP的 查看,上传,下载功能

来源:互联网 发布:上古卷轴原版捏脸数据 编辑:程序博客网 时间:2024/05/07 02:12
 

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import sun.net.TelnetInputStream;
import sun.net.TelnetOutputStream;
import sun.net.ftp.FtpClient;

public class ftp {

 public static void main(String[] args) {
  getButton_actionPerformed();
  putButton_actionPerformed();
  ftpList_actionPerformed();
 }

 /**
  * 显示FTP服务器上的文件
  */
 public static void ftpList_actionPerformed() {
  // FTP服务器的IP地址
  String server = "*****";
  // 用户名
  String user = "******";
  // 密码
  String password = "*****";
  // 指定读取ftp的目录,不填则默认为根目录,读取则为根目录下的文件及文件夹
  String path = "";
  FtpClient ftpClient = new FtpClient();
  try {
   ftpClient.openServer(server);
   ftpClient.login(user, password);
   if (path.length() != 0) {
    ftpClient.cd(path);
   }
   TelnetInputStream is = ftpClient.list();
   int c;
   while ((c = is.read()) != -1) {
    System.out.print((char) c);
   }
   is.close();
   ftpClient.closeServer();
  } catch (IOException e) {
   e.printStackTrace();
  }
 }

 /**
  * 从FTP服务器上下载文件到指定的地方
  */
 public static void getButton_actionPerformed() {
  // FTP服务器的IP地址
  String server = "*****";
  // 用户名
  String user = "*****";
  // 密码
  String password = "******";
  // 指定读取ftp的目录,不填则默认为根目录,读取则为根目录下的文件及文件夹
  String path = "";
  // 该目录下要读取的文件名
  String filename = "80.txt";
  FtpClient ftpClient = new FtpClient();
  try {
   ftpClient.openServer(server);
   ftpClient.login(user, password);
   if (path.length() != 0) {
    ftpClient.cd(path);
   }
   ftpClient.binary();
   TelnetInputStream is = ftpClient.get(filename);
   // 在本地建立文件
   File file_out = new File("d:/56txt");
   FileOutputStream os = new FileOutputStream(file_out);
   byte[] bytes = new byte[1024];
   int c;
   while ((c = is.read(bytes)) != -1) {
    // 写入文件中
    os.write(bytes, 0, c);
   }
   is.close();
   os.close();
   ftpClient.closeServer();
  } catch (IOException e) {
   e.printStackTrace();
  }
 }

 /**
  * 向FTP服务器上上传一个文件
  */
 public static void putButton_actionPerformed() {
  // FTP服务器的IP地址
  String server = "*******";
  // 用户名
  String user = "*******";
  // 密码
  String password = "**********";
  // 指定读取ftp的目录,不填则默认为根目录,读取则为根目录下的文件及文件夹
  String path = "";
  // 该目录下要生成的文件名
  String filename = "55.txt";
  try {
   FtpClient ftpClient = new FtpClient();
   ftpClient.openServer(server);
   ftpClient.login(user, password);
   if (path.length() != 0) {
    ftpClient.cd(path);
   }
   ftpClient.binary();
   TelnetOutputStream os = ftpClient.put(filename);
   System.out.println(os);
   File file_in = new File(filename);
   System.out.println(file_in);
   // 读取本地文件
   FileInputStream is = new FileInputStream("d:/55.txt");
   byte[] bytes = new byte[1024];
   int c;
   System.out.println(is.read(bytes));
   while ((c = is.read(bytes)) != -1) {
    os.write(bytes, 0, c);
   }
   is.close();
   os.close();
   ftpClient.closeServer();
  } catch (IOException ex) {
   ex.printStackTrace();
  }
 }
}

原创粉丝点击