您现在的位置是:主页 > news > 医疗网站建设中心/武汉网站运营专业乐云seo

医疗网站建设中心/武汉网站运营专业乐云seo

admin2025/5/13 5:25:53news

简介医疗网站建设中心,武汉网站运营专业乐云seo,网站建设商家,最新国际热点新闻首先我们需要创建一个服务端,让他一直监听一个ip端口:为了防止卡死,我们单独创建一个线程运行它。 而后创建一个客户端,让他去访问服务端监听的ip和端口号。 转载自:小囡楠 1.客户端 通过new Socket("ip"…

医疗网站建设中心,武汉网站运营专业乐云seo,网站建设商家,最新国际热点新闻首先我们需要创建一个服务端,让他一直监听一个ip端口:为了防止卡死,我们单独创建一个线程运行它。 而后创建一个客户端,让他去访问服务端监听的ip和端口号。 转载自:小囡楠 1.客户端 通过new Socket("ip"…

首先我们需要创建一个服务端,让他一直监听一个ip+端口:为了防止卡死,我们单独创建一个线程运行它。

而后创建一个客户端,让他去访问服务端监听的ip和端口号。

转载自:小囡楠

1.客户端

通过new Socket("ip",port)连接服务端

创建文件输入流读取文件

创建返回套接字的输出流

写入文章名称,长度等属性

读取、写入文章操作

关闭流

package com.company;import javax.xml.crypto.Data;
import java.io.*;
import java.net.Socket;//客户端
public class Client extends Socket{private  final String SERVER_IP="192.168.201.104";private final int SERVER_PORT=8999;private Socket client;private FileInputStream fis;private DataOutputStream dos;//创建客户端,并指定接收的服务端IP和端口号public Client() throws IOException{this.client=new Socket(SERVER_IP,SERVER_PORT);System.out.println("成功连接服务端..."+SERVER_IP);}//向服务端传输文件public void sendFile(String url) throws IOException {File file=new File(url);try {fis = new FileInputStream(file);//BufferedInputStream bi=new BufferedInputStream(new InputStreamReader(new FileInputStream(file),"GBK"));dos = new DataOutputStream(client.getOutputStream());//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;while ((length = fis.read(bytes, 0, bytes.length)) != -1) {dos.write(bytes, 0, length);dos.flush();}System.out.println("======== 文件传输成功 ========");}catch(IOException e){e.printStackTrace();System.out.println("客户端文件传输异常");}finally{fis.close();dos.close();}}public static void main(String[] args) {try {Client client = new Client(); // 启动客户端连接client.sendFile("E:/dxn/aaa.txt"); // 传输文件} catch (Exception e) {e.printStackTrace();}}
}

2 服务端

启动服务端new ServerSocket(port)

接收连接服务端的客户端对象

创建返回套接字的输入流

创建文件输出流写出文件

读取文章名称,长度等属性

读取、写入文章操作

关闭流

package com.company;import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;public class FileServer extends ServerSocket{private static final int SERVER_PORT = 8999; // 服务端端口private ServerSocket server;private Socket socket;private DataInputStream dis;private FileOutputStream fos;public FileServer() throws Exception {server=new ServerSocket(SERVER_PORT);}public void task() throws IOException{System.out.println("======== 等待连接 ========");Socket socket = server.accept();System.out.println(" Ip:"+socket.getInetAddress()+"已连接");try {dis = new DataInputStream(socket.getInputStream());// 文件名和长度String fileName = dis.readUTF();long fileLength = dis.readLong();File directory = new File("E:/a");if(!directory.exists()) {directory.mkdir();}File file = new File(directory.getAbsolutePath() + File.separatorChar + fileName);fos = new FileOutputStream(file);System.out.println("file。。。。。。。。。。。。。。"+file);System.out.println("fileName。。。。。。。。。。。。。。"+fileName);System.out.println("======== 开始接收文件 ========");byte[] bytes = new byte[1024];int length = 0;while((length = dis.read(bytes, 0, bytes.length)) != -1) {fos.write(bytes, 0, length);fos.flush();}System.out.println("======== 文件接收成功 ========");} catch (Exception e) {e.printStackTrace();} finally {try {if(fos != null)fos.close();if(dis != null)dis.close();socket.close();} catch (IOException e) {e.printStackTrace();}}}public static void main(String[] args) {try {FileServer server = new FileServer(); // 启动服务端server.task();} catch (Exception e) {e.printStackTrace();}}}