JAVA文件传输(具体到文件名)

一、服务器端

package 文件传输;

import java.net.*;
import java.io.*;

public class ServerTcpListener implements Runnable {

    public static void main(String[] args) {

        try {
            final ServerSocket server = new ServerSocket(33456);
            Thread th = new Thread(new Runnable() {
                public void run() {
                    while (true) {
                        try {
                            System.out.println("开始监听...");
                            Socket socket = server.accept();
                            System.out.println("有链接");
                            receiveFile(socket);
                        } catch (Exception e) {
                        }
                    }
                }

            });

            th.run(); //启动线程运行
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void run() {
    }

    public static void receiveFile(Socket socket) {

        byte[] inputByte = null;
        int length = 0;
        DataInputStream dis = null;
        FileOutputStream fos = null;
        try {
            try {

                dis = new DataInputStream(socket.getInputStream());
                fos = new FileOutputStream(new File("d:/cc.jpg"));//在D盘新建同类型文件接收所传文件
                inputByte = new byte[1024];
                System.out.println("开始接收数据...");
                while ((length = dis.read(inputByte, 0, inputByte.length)) > 0) {
                    System.out.println(length);
                    fos.write(inputByte, 0, length);
                    fos.flush();
                }
                System.out.println("完成接收");
            } finally {
                if (fos != null)
                    fos.close();
                if (dis != null)
                    dis.close();
                if (socket != null)
                    socket.close();
            }
        } catch (Exception e) {

        }

    }
}

二、客户端

package 文件传输;

import java.io.DataOutputStream;//数据输出流允许应用程序以可移植的方式将原始Java数据类型写入输出流。然后,应用程序可以使用数据输入流将数据读入。
import java.io.File;
import java.io.FileInputStream;//FileInputStream从文件系统中的文件中获取输入字节。可用的文件取决于主机环境。
import java.net.InetSocketAddress;//这个类实现了一个IP套接字地址(IP地址+端口号),它也可以是一对(主机名+端口号),在这种情况下,将会做出一个尝试来解析主机名。
								 //如果解决失败,那么地址说是没有解决,但仍然可以在一些情况下使用,如通过代理连接。
import java.net.Socket;//这个类实现客户端套接字(也称为“套接字”)。套接字是用于在两台机器之间通信的端点。

public class ClientTcpSend { //客户端

    public static void main(String[] args) {
        int length = 0;
        byte[] sendBytes = null;
        Socket socket = null;
        DataOutputStream dos = null;
        FileInputStream fis = null;
        try {
            try {
                socket = new Socket();
                //使用指定的超时值将此套接字连接到服务器。超时为零被解释为无限超时。连接将被阻塞,直到建立或发生错误。
                socket.connect(new InetSocketAddress("127.0.0.1", 33456),
                        10 * 1000);
                dos = new DataOutputStream(socket.getOutputStream());
                //选择此电脑中存在的某一具体文件
                File file = new File("C:\\Users\\Lenovo\\Pictures\\Camera Roll\\0bbdfebc44d0cdb4989d86960b06a168.jpg");
                fis = new FileInputStream(file);
                sendBytes = new byte[1024];
                while ((length = fis.read(sendBytes, 0, sendBytes.length)) > 0) {
                    dos.write(sendBytes, 0, length);
                    dos.flush();
                }
            } finally {
                if (dos != null)
                    dos.close();
                if (fis != null)
                    fis.close();
                if (socket != null)
                    socket.close();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值