admin 管理员组

文章数量: 1184232


2024年3月19日发(作者:怎么添加html空格符)

客户端:

package ;

import edInputStream;

import putStream;

import ;

import tream;

import AccessFile;

import dress;

import ;

public class FileClient {

public static void main(String[] args) throws Exception {

// 使用本地文件系统接受网络数据并存为新文件

File file = new File("d:");

NewFile();//地创建一个新的空文件

RandomAccessFile raf = new RandomAccessFile(file, "rw");

// 通过Socket连接文件服务器

Socket server = new Socket(alHost(),

60000);

// 创建网络接受流接受服务器文件数据

InputStream netIn = utStream();

InputStream in = new DataInputStream(new

BufferedInputStream(netIn));

// 创建缓冲区缓冲网络数据

byte[] buf = new byte[2048];

int num = (buf);

while (num != (-1)) {// 是否读完所有数据

(buf, 0, num);// 将数据写往文件

tes(num);// 顺序写文件字节

num = (buf);// 继续从网络中读取文件

}

();

();

}

}

服务端:

package ;

import edOutputStream;

import tputStream;

import ;

import putStream;

import Stream;

import Socket;

import ;

public class FileServer {

public static void main(String[] args) throws Exception {

String fileName = "d:";

File file = new File(fileName);// 创建文件流用来读取文件中的数据

FileInputStream fos = new FileInputStream(file);

ServerSocket ss = new ServerSocket(60000);// 创建网络服务器接受

Socket client = ();

OutputStream netOut = putStream();// 创建网络输出

doc = new DataOutputStream(new

客户请求

流并提供数据包装器

OutputStream

BufferedOutputStream(netOut));

// 创建文件读取缓冲区

byte[] buf = new byte[2048];

int num = (buf);

while (num != (-1)) {// 是否读完文件

(buf, 0, num);// 把文件数据写出网络缓冲区

();// 刷新缓冲区把数据写往客户端

num = (buf);// 继续从文件中读取数据

}

();

();

}

}


本文标签: 文件 数据 网络 创建 接受