java操作FTP

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

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

public class ftp
{

 private FtpClient ftpClient ;
 private String FtpServer = "192.168.0.8";
 private String FtpPort = "21";
 private String FtpUser = "root";
 private String FtpPwd = "root";
 public ftp()
 {
 }
 public ftp(String pm_sFtpServer,String pm_sFtpPort,String pm_sFtpUser,String pm_sFtpPwd)
 {
  this.FtpServer = pm_sFtpServer;
  this.FtpPort = pm_sFtpPort;
  this.FtpUser = pm_sFtpUser;
  this.FtpPwd = pm_sFtpPwd;

 }

 /*
  * 连接FTP服务器
  */
 public void login()
 {
  try
  {
   this.ftpClient = new FtpClient(this.FtpServer,Integer.parseInt(this.FtpPort));
   //登陆服务端FTP,设定ascii传输方式
   this.ftpClient.openServer(this.FtpServer);
   this.ftpClient.login(this.FtpUser,this.FtpPwd);
   this.ftpClient.ascii();
  }
  catch (NumberFormatException e)
  {
   System.out.println("FTPTools:FTP连接错误!");
   e.printStackTrace();
  }
  catch (IOException e)
  {
   System.out.println("FTPTools:FTP连接错误!");
   e.printStackTrace();
  }
 }
 public void stop()
 {
  String message = "";
    try
  {
      if(ftpClient!=null)
   {
    ftpClient.closeServer();
    message = "与主机"+FtpServer+"连接已断开!";
    System.out.println(message);
      }
    }
    catch(IOException e)
    {
      message = "与主机"+FtpServer+"断开连接失败!"+e;
      System.out.println(message);
    }
 }
 /*
  * @完成文件下载操作
  * @param pm_sServerFilePath 服务器文件路径
  * @param pm_sServerFileName 服务器文件名
  * @param pm_sDesPath 客户端下载文件存放路径
  */
 public void download(String pm_sServerFilePath,String pm_sServerFileName,String pm_sDesPath)
 {
  try
  {
   login();
   //进入下载目录
   this.ftpClient.cd(pm_sServerFilePath);
   //流转换,完成下载操作
   TelnetInputStream ins = this.ftpClient.get(pm_sServerFileName);
   FileOutputStream ous = new FileOutputStream(pm_sDesPath+"//"+pm_sServerFileName);
   byte[] tmp = new byte[1024];
   int lenth = 0;
   while((lenth = ins.read(tmp))!=-1)
   {
    ous.write(tmp,0,lenth);
   }
   //ous.flush();
   ins.close();
   ous.close();
  }
  catch (IOException e)
  {
   System.out.println("FTP下载错误!");
   e.printStackTrace();
  }
 }
 /*
  * @完成文件传送操作
  * @param pm_oSrcFile
  */
 public void upload(File pm_oSrcFile,String pm_sServerFilePath)
 {
  try
  {
   login();
   //进入上传目录
   this.ftpClient.cd(pm_sServerFilePath);
   //流转换,完成上传操作
   TelnetOutputStream out = this.ftpClient.put(pm_oSrcFile.getName());
   FileInputStream ins = new FileInputStream(pm_oSrcFile);
   byte[] tmp = new byte[1024];
   int lenth = 0;
   
   while((lenth = ins.read(tmp))!=-1)
   {
    out.write(tmp,0,lenth);
   }
   //out.flush();
   out.close();
   ins.close();
  }
  catch (IOException e)
  {
   System.out.println("FTP上传错误!");
   e.printStackTrace();
  }

 }

 public static void main(String[] args) throws Exception
 {
  //ftp f = new ftp("192.168.0.29","21","temp","temp");
  ftp f = new ftp();
  f.download("/opt/sfcl/oaj/data","2006-08-31-08","temp");
  //File uploadfile = new File("list.java");
  //f.upload(uploadfile,"data");
  f.stop();
  Thread.sleep(10000);
 }
}

 

Java中,你可以使用第三方库如Apache Commons Net或者FTPClient4J来进行FTP(File Transfer Protocol)服务器操作。以下是一个简单的示例,展示如何连接、上传、下载以及断开FTP连接: 首先,你需要在项目中添加相应的依赖。例如,对于Apache Commons Net,你可以在pom.xml中添加: ```xml <dependency> <groupId>commons-net</groupId> <artifactId>commons-net</artifactId> <version>3.6</version> </dependency> ``` 然后,你可以使用以下代码片段进行基本操作: ```java import org.apache.commons.net.ftp.*; public class FTPOperations { private FtpClient client; public void connect(String host, int port, String user, String password) throws IOException { client = new FtpClient(); client.connect(host, port); client.login(user, password); } public void uploadFile(String localFilePath, String remoteFilePath) throws IOException { FileInputStream fis = new FileInputStream(localFilePath); client.storeFile(remoteFilePath, fis); fis.close(); } public void downloadFile(String remoteFilePath, String localFilePath) throws IOException { InputStream fis = client.retrieveFileStream(remoteFilePath); FileOutputStream fos = new FileOutputStream(localFilePath); byte[] buffer = new byte[1024]; int length; while ((length = fis.read(buffer)) > 0) { fos.write(buffer, 0, length); } fos.close(); fis.close(); } public void disconnect() throws IOException { client.logout(); client.disconnect(); } public static void main(String[] args) { try { FTPOperations ftp = new FTPOperations(); // 连接到FTP服务器 ftp.connect("ftp.example.com", 21, "username", "password"); // 执行上传、下载等操作 ftp.uploadFile("/path/to/local/file.txt", "/remote/file.txt"); ftp.downloadFile("/remote/file.txt", "/path/to/download/file.txt"); // 断开连接 ftp.disconnect(); } catch (Exception e) { e.printStackTrace(); } } } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值