Java FTP 客户端代码优化:交互式输入文件路径

以下代码展示了如何修改 Java FTP 客户端代码,使用 Scanner 类实现交互式输入文件路径功能,增强代码灵活性和易用性。

原始代码:

package ftp;

import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.net.Socket;

/**
 * 文件传输Client端<br>
 */
public class FileTransferClient extends Socket {

    private static final String SERVER_IP = "10.41.104.38"; // 服务端IP
    private static final int SERVER_PORT = 8899; // 服务端端口

    private Socket client;

    private FileInputStream fis;

    private DataOutputStream dos;

    /**
     * 构造函数<br/>
     * 与服务器建立连接
     * @throws Exception
     */
    public FileTransferClient() throws Exception {
        super(SERVER_IP, SERVER_PORT);
        this.client = this;
        System.out.println("Cliect[port:" + client.getLocalPort() + "] 成功连接服务端");
    }

    /**
     * 向服务端传输文件
     * @throws Exception
     */
    public void sendFile() throws Exception {
        try {
            File file = new File("E:\\test.txt");
            if(file.exists()) {
                fis = new FileInputStream(file);
                dos = new DataOutputStream(client.getOutputStream());

                // 文件名和长度
                dos.writeUTF(file.getName());
                dos.flush();
                dos.writeLong(file.length());
                dos.flush();

                // 开始传输文件
                System.out.println("======== 开始传输文件 =======");
                byte[] bytes = new byte[1024];
                int length = 0;
                long progress = 0;
                while((length = fis.read(bytes, 0, bytes.length)) != -1) {
                    dos.write(bytes, 0, length);
                    dos.flush();
                    progress += length;
                    System.out.print("| " + (100*progress/file.length()) + "% | ");
                }
                System.out.println();
                System.out.println("======== 文件传输成功 =======");
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if(fis != null)
                fis.close();
            if(dos != null)
                dos.close();
            client.close();
        }
    }

    /**
     * 入口
     * @param args
     */
    public static void main(String[] args) {
        try {
            FileTransferClient client = new FileTransferClient(); // 启动客户端连接
            client.sendFile(); // 传输文件
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}

修改后的代码:

package ftp;

import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.net.Socket;
import java.util.Scanner;

/**
 * 文件传输Client端<br>
 */
public class FileTransferClient extends Socket {

    private static final String SERVER_IP = "10.41.104.38"; // 服务端IP
    private static final int SERVER_PORT = 8899; // 服务端端口

    private Socket client;

    private FileInputStream fis;

    private DataOutputStream dos;

    /**
     * 构造函数<br/>
     * 与服务器建立连接
     * 
     * @throws Exception
     */
    public FileTransferClient() throws Exception {
        super(SERVER_IP, SERVER_PORT);
        this.client = this;
        System.out.println("Cliect[port:" + client.getLocalPort() + "] 成功连接服务端");
    }

    /**
     * 向服务端传输文件
     * 
     * @throws Exception
     */
    public void sendFile() throws Exception {
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入文件路径:");
        String filePath = scanner.nextLine();
        try {
            File file = new File(filePath);
            if (file.exists()) {
                fis = new FileInputStream(file);
                dos = new DataOutputStream(client.getOutputStream());

                // 文件名和长度
                dos.writeUTF(file.getName());
                dos.flush();
                dos.writeLong(file.length());
                dos.flush();

                // 开始传输文件
                System.out.println("======== 开始传输文件 =======");
                byte[] bytes = new byte[1024];
                int length = 0;
                long progress = 0;
                while ((length = fis.read(bytes, 0, bytes.length)) != -1) {
                    dos.write(bytes, 0, length);
                    dos.flush();
                    progress += length;
                    System.out.print("| " + (100 * progress / file.length()) + "% | ");
                }
                System.out.println();
                System.out.println("======== 文件传输成功 =======");
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            scanner.close();
            if (fis != null)
                fis.close();
            if (dos != null)
                dos.close();
            client.close();
        }
    }

    /**
     * 入口
     * 
     * @param args
     */
    public static void main(String[] args) {
        try {
            FileTransferClient client = new FileTransferClient(); // 启动客户端连接
            client.sendFile(); // 传输文件
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}

修改内容:

  1. 在 sendFile() 方法中增加交互式输入文件路径的功能,使用 Scanner 类实现。
  2. 在 finally 块中关闭 Scanner 对象。

这样修改后,用户可以在命令行中输入文件路径,而不是在代码中写死文件路径。增加了程序的灵活性和易用性。

使用说明:

  1. 运行代码。
  2. 程序会提示您输入文件路径。
  3. 输入文件路径并按回车键。
  4. 程序会将文件传输到 FTP 服务器。

注意:

  • 确保 FTP 服务器已启动,并且您已配置好连接信息(IP 地址和端口号)。
  • 请确保输入的文件路径是有效的。
  • 为了方便理解,以上代码已将双引号替换为单引号。
Java FTP 客户端代码优化:交互式输入文件路径

原文地址: https://www.cveoy.top/t/topic/ocrs 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录