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);// 继续从文件中读取数据
}
();
();
}
}
版权声明:本文标题:java Socket 传输文件1 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.roclinux.cn/p/1710790517a573271.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论