jdk7 自带ftp 下载功能

jdk7 ftp下载功能:

 

 

public class Test
{
 /**
  *
  * 本地文件名
  */
 private String localfilename;
 /**
  *
  * 远程文件名
  */
 private String remotefilename;
 /**
  *
  * FTP客户端
  */
 private FtpClient ftpClient;

 /**
  * 服务器连接
  *
  * @param ip
  *            服务器IP
  * @param port
  *            服务器端口
  * @param user
  *            用户名
  * @param password
  *            密码
  * @param path
  *            服务器路径
  * @throws FtpProtocolException
  *
  */
 public void connectServer(String ip, int port, String user, String password, String path) throws FtpProtocolException
 {
  try
  {
   /* ******连接服务器的两种方法****** */
   // 第一种方法
   
     ftpClient =FtpClient.create();
     ftpClient.connect(new InetSocketAddress(ip, port));
   
   // 第二种方法
   //ftpClient = FtpClient.create(ip);
   //ftpClient.login(user, null, password);
   ftpClient.login(user,password.toCharArray());
   // 设置成2进制传输
   ftpClient.setBinaryType();
   System.out.println("login success!");

   if (path.length() != 0)
   {
    // 把远程系统上的目录切换到参数path所指定的目录
    ftpClient.changeDirectory(path);
   }
   ftpClient.setBinaryType();
  } catch (IOException ex)
  {
   ex.printStackTrace();
   throw new RuntimeException(ex);
  }

 }

 /**
  * 关闭连接
  *
  */

 public void closeConnect()
 {
  try
  {
   ftpClient.close();
   System.out.println("disconnect success");
  } catch (IOException ex)
  {
   System.out.println("not disconnect");
   ex.printStackTrace();
   throw new RuntimeException(ex);
  }
 }

 

/**
  *
  * 下载文件
  *
  * @param remoteFile
  *            远程文件路径(服务器端)
  * @param localFile
  *            本地文件路径(客户端)
  * @throws FtpProtocolException
  *
  */
 public void download(String remoteFile, String localFile) throws FtpProtocolException
 {
  TelnetInputStream is = null;
  FileOutputStream os = null;
  try
  {

   // 获取远程机器上的文件filename,借助TelnetInputStream把该文件传送到本地。
   is = (TelnetInputStream) ftpClient.getFileStream(remoteFile);
   File file_in = new File(localFile);
   os = new FileOutputStream(file_in);

   byte[] bytes = new byte[1024];
   int c;
   while ((c = is.read(bytes)) != -1)
   {
    os.write(bytes, 0, c);
   }
   System.out.println("download success");
  } catch (IOException ex)
  {
   System.out.println("not download");
   ex.printStackTrace();
   throw new RuntimeException(ex);
  } finally
  {
   try
   {
    if (is != null)
    {
     is.close();
    }
   } catch (IOException e)
   {
    e.printStackTrace();
   } finally
   {
    try
    {
     if (os != null)
     {
      os.close();
     }
    } catch (IOException e)
    {
     e.printStackTrace();
    }
   }
  }
 }

 

}

Java通过JDK自带的API实现FTP可以使用Java的网络编程功能Java提供了java.net包中的一些类来处理网络连接和传输。以下是一个简单的示例,演示了如何使用JavaFTP实现: ```java import java.io.*; import java.net.*; public class FTPClient { public static void main(String[] args) { String server = "ftp.example.com"; // FTP服务器地址 int port = 21; // FTP服务器端口号 String user = "username"; // FTP登录用户名 String pass = "password"; // FTP登录密码 try { // 创建FTP连接 FTPClient ftp = new FTPClient(); ftp.connect(server, port); ftp.login(user, pass); // 打印服务器的响应信息 System.out.println("FTP服务器连接成功:" + ftp.getReplyString()); // 设置传输模式为二进制 ftp.setFileType(FTP.BINARY_FILE_TYPE); // 下载文件 String remoteFile = "/path/to/remote/file.txt"; // 远程文件路径 File localFile = new File("local-file.txt"); // 本地文件路径 OutputStream outputStream = new BufferedOutputStream(new FileOutputStream(localFile)); ftp.retrieveFile(remoteFile, outputStream); outputStream.close(); // 上传文件 String remoteDir = "/path/to/remote/directory"; // 远程目录路径 String localFilePath = "local-file.txt"; // 本地文件路径 InputStream inputStream = new BufferedInputStream(new FileInputStream(localFilePath)); ftp.storeFile(remoteDir + "/file.txt", inputStream); inputStream.close(); // 关闭FTP连接 ftp.logout(); ftp.disconnect(); System.out.println("文件上传和下载成功!"); } catch (IOException e) { e.printStackTrace(); } } } ``` 上述示例中,我们使用`FTPClient`类来连接FTP服务器、登录、设置传输模式、下载文件和上传文件等操作。使用`connect`方法连接FTP服务器,`login`方法登录,`setFileType`方法设置传输模式。使用`retrieveFile`方法下载文件,`storeFile`方法上传文件。最后使用`logout`方法和`disconnect`方法关闭FTP连接。 这只是一个简单的示例,实际使用中可能需要处理更多的异常情况和添加更多的功能
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值