JAVA实现ftp文件的上传及下载

1、引入依赖包

import org.apache.commons.net.ftp.FTP;
import org.apache.commons.net.ftp.FTPClient;
import java.io.*;

2、连接ftp服务器

   String server = "192.168.1.100";
        int port = 21;
        String user = "test";
        String password = "password";


        FTPClient ftpClient = new FTPClient();
        try {
            ftpClient.connect(server, port);
            ftpClient.login(user, password);
            ftpClient.enterLocalPassiveMode();
            ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
       } catch (IOException ex) {
            System.out.println("发生IO异常:" + ex.getMessage());
            ex.printStackTrace();
        } finally {
            try {
                if (ftpClient.isConnected()) {
                    ftpClient.logout();
                    ftpClient.disconnect();
                }
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        }

3、上传文件

// 上传文件
String localFileToUpload = "D:\\test\\file.txt";
String remoteFileToUpload = "/test/file.txt";
boolean uploadSuccess = uploadFile(ftpClient, localFileToUpload, remoteFileToUpload);
if (uploadSuccess) {
     System.out.println("文件上传成功!");
} else {
      System.out.println("文件上传失败!");
}


public static boolean uploadFile(FTPClient ftpClient, String localFilePath, String remoteFilePath) throws IOException {
        File localFile = new File(localFilePath);
        InputStream inputStream = new FileInputStream(localFile);
        try {
            return ftpClient.storeFile(remoteFilePath, inputStream);
        } finally {
            inputStream.close();
        }
    }

4、下载文件

 // 下载文件
 String remoteFilePath = "/test/file.txt";
 String localFilePath = "D:\\test\\other\\file.txt";
 boolean downloadSuccess = downloadFile(ftpClient, remoteFilePath, localFilePath);
 if (downloadSuccess) {
       System.out.println("文件下载成功!");
 } else {
       System.out.println("文件下载失败!");
 }

 public static boolean downloadFile(FTPClient ftpClient, String remoteFilePath, String localFilePath) throws IOException {
        File downloadFile = new File(localFilePath);
        OutputStream outputStream = new BufferedOutputStream(new FileOutputStream(downloadFile));
        try {
            return ftpClient.retrieveFile(remoteFilePath, outputStream);
        } finally {
            outputStream.close();
        }
    }

  • 3
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 可以使用Java的网络编程功能,实现FTP协议的文件上传。具体步骤如下: 1. 建立与FTP服务器的连接:使用Java的Socket类进行连接,并使用该类的方法登录到FTP服务器。 2. 设置上传模式:使用FTP协议的特定命令设置上传模式为二进制模式。 3. 构造文件:使用Java的FileInputStream类读取待上传文件,并将其写入到FTP服务器。 4. 确认文件上传:使用FTP协议的特定命令确认文件已经成功上传。 5. 断开连接:使用Java的Socket类断开与FTP服务器的连接。 可以使用现成的开源FTP客户端库,如Apache Commons Net,简化开发难度。 ### 回答2: 要使用Java实现FTP文件上传,你需要使用JavaFTP库或框架。以下是一种可能的实现方式: 首先,需要引入FTP库或框架的相关依赖。比较常用的库包括Apache Commons Net和Apache FTPClient。你可以在项目的构建文件(比如Maven的pom.xml文件)中添加相应的依赖。 接下来,你需要建立FTP连接。使用FTPClient类的connect()方法连接到FTP服务器。你需要提供FTP服务器的主机名、端口号、用户名和密码。 一旦连接成功,你可以使用FTPClient的setFileType()方法设置文件类型,如二进制文件或文本文件。 然后,你可以使用changeWorkingDirectory()方法切换到FTP服务器上的目标目录,比如上传文件的目录。 之后,你可以使用storeFile()方法上传文件。该方法的参数是本地文件的路径和文件名,以及在FTP服务器上的文件名。 在上传过程中,你可以使用FTPClient的进度监听器获取上传进度并进行相应的处理。比如,你可以实现一个ProgressListener接口的类,重写bytesTransferred()方法,在方法中显示或记录上传进度。 最后,上传完成后,你需要使用disconnect()方法关闭FTP连接。 需要注意的是,在实际的使用中,你还需要处理异常、处理上传失败的情况、上传文件时的断点续等场景。这些细节根据具体的需求和使用情况可能会有所不同。 总结起来,实现FTP文件上传的步骤包括连接FTP服务器、设置文件类型、切换目录、上传文件,并考虑上传进度、异常处理和关闭连接等。使用JavaFTP库或框架可以简化这些操作,提高开发效率。 ### 回答3: Java实现FTP文件上传可以通过使用Apache Commons Net库来实现。以下是一个简单的示例代码: ```java import org.apache.commons.net.ftp.FTP; import org.apache.commons.net.ftp.FTPClient; import java.io.File; import java.io.FileInputStream; import java.io.IOException; public class FTPFileUploader { private String server; private int port; private String username; private String password; public FTPFileUploader(String server, int port, String username, String password) { this.server = server; this.port = port; this.username = username; this.password = password; } public void uploadFile(String localFilePath, String remoteFilePath) { FTPClient ftpClient = new FTPClient(); try { ftpClient.connect(server, port); ftpClient.login(username, password); ftpClient.enterLocalPassiveMode(); File localFile = new File(localFilePath); FileInputStream fileInputStream = new FileInputStream(localFile); ftpClient.setFileType(FTP.BINARY_FILE_TYPE); ftpClient.storeFile(remoteFilePath, fileInputStream); ftpClient.logout(); } catch (IOException e) { e.printStackTrace(); } finally { try { ftpClient.disconnect(); } catch (IOException e) { e.printStackTrace(); } } } public static void main(String[] args) { String server = "ftp.example.com"; int port = 21; String username = "your-username"; String password = "your-password"; String localFilePath = "path/to/local/file"; String remoteFilePath = "path/to/remote/file"; FTPFileUploader fileUploader = new FTPFileUploader(server, port, username, password); fileUploader.uploadFile(localFilePath, remoteFilePath); } } ``` 在这个示例代码中,首先创建了一个FTPClient对象,然后连接到指定的FTP服务器。调用`login`方法进行登录验证,并通过`enterLocalPassiveMode`方法进入被动模式。接着打开本地文件输入流,设置FTP输类型为二进制文件类型,使用`storeFile`方法将本地文件上传到指定的远程路径中。最后关闭连接。 在使用这个示例代码时,需要将FTP服务器的地址、端口、用户名和密码替换为实际的值,并指定本地文件路径和远程文件路径。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值