java通过socket传输文件

JAVA网络编程 专栏收录该内容
12 篇文章 0 订阅

客户端代码


package 基于socket的文件传输;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.net.Socket;

/**
 * 从本地读取文件,然后向服务器上传文件
 *
 */
public class FileTransferClient {
    private String host = "localhost";
    private int port = 8888;
    private Socket socket;
    private static String fileName="D:\\[TPP&CRL&bilibili][The King's Avatar][09][GB][1080P].mp4";

    public FileTransferClient() throws IOException {
        socket = new Socket(host, port);
    }

    private void sendFile(String filePath) {

            while (true) {

                try {
                    File file = new File(filePath);
                    System.out.println("文件大小:" + file.length() + "kb");
                    DataInputStream dis = new DataInputStream(new FileInputStream(filePath));
                    DataOutputStream dos = new DataOutputStream(socket.getOutputStream());
                    byte[] buf = new byte[1024 * 9];
                    int len = 0;
                    while ((len = dis.read(buf)) != -1) {
                        dos.write(buf, 0, len);

                    }
                    dos.flush();
                    System.out.println("文件上传结束,,,,");

                    dis.close();
                    dos.close();
                } catch (FileNotFoundException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }

            }
    }

    public static void main(String[] args) throws IOException {
        new FileTransferClient().sendFile(fileName);
    }

}

服务器端代码


package 基于socket的文件传输;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;

/**
 * 接受客户端发来的文件,然后保存在本地
 * 
 */
public class FileTransferServer {

    private int port = 8888;
    private ServerSocket serverSocket;
    private static String fileName="D:\\[The King's Avatar][09].mp4";

    public FileTransferServer() throws IOException {
        serverSocket = new ServerSocket(port);
        System.out.println("服务器已经启动,,,,,,");
    }

    private void receieveFile(String filePath) {
        while (true) {
            try {
                Socket socket = null;

                socket = serverSocket.accept();
                System.out.println("接收到客户端的连接,,,,");

                DataInputStream dis = new DataInputStream(socket.getInputStream());
                DataOutputStream dos = new DataOutputStream(new FileOutputStream(filePath));

                byte[] buf = new byte[1027 * 9];
                int len = 0;

                while ((len = dis.read(buf)) != -1) {
                    dos.write(buf, 0, len);
                }
                dos.flush();

                System.out.println("文件接受结束,,,,");
                dis.close();
                dos.close();

            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

        }

    }

    public static void main(String[] args) throws IOException {
        new FileTransferServer().receieveFile(fileName);
    }

}
  • 6
    点赞
  • 1
    评论
  • 35
    收藏
  • 一键三连
    一键三连
  • 扫一扫,分享海报

©️2021 CSDN 皮肤主题: 深蓝海洋 设计师:CSDN官方博客 返回首页
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值