java点对点传输文件代码

Python019

java点对点传输文件代码,第1张

//在我电脑运行没问题,把E:/EKI.txt传送到D:/EKI.txt你可以换成其它文件

//先运行Server,然后client,共三个class有问题QQ23400262

package ch.socket.file

import java.io.*

import java.net.ServerSocket

import java.net.Socket

public class Server extends Thread {

public static int port = 6789

public static String host = "127.0.0.1"

private static ServerSocket server = null

public void run() {

if (server == null) {

try {

// 1、新建ServerSocket实例

server = new ServerSocket(port)

} catch (IOException e) {

e.printStackTrace()

}

}

System.out.println("服务器启动...")

while (true) {

try {

// 2、访问ServerSocket实例的accept方法取得一个客户端Socket对象

Socket client = server.accept()

if (client == null)

continue

new SocketConnection(client, "D:\\").start()

} catch (IOException ex) {

ex.printStackTrace()

}

}

}

public static void main(String[] args) {

new Server().start()

}

}

package ch.socket.file

import java.io.*

import java.io.IOException

import java.net.Socket

import java.net.UnknownHostException

public class Client {

private Socket client

private boolean connected

public boolean isConnected() {

return connected

}

public void setConnected(boolean connected) {

this.connected = connected

}

public Client(String host, int port) {

try {

// 1、新建Socket对象

client = new Socket(host, port)

System.out.println("服务器连接成功!")

this.connected = true

} catch (UnknownHostException e) {

this.connected = false

close()

} catch (IOException e) {

System.out.println("服务器连接失败!")

this.connected = false

close()

}

}

/**

* 将文件内容发送出去

*

* @param filepath

* 文件的全路径名

*/

public void sendFile(String filepath) {

DataOutputStream out = null

DataInputStream reader = null

try {

if (client == null)

return

File file = new File(filepath)

reader = new DataInputStream(new BufferedInputStream(

new FileInputStream(file)))

// 2、将文件内容写到Socket的输出流中

out = new DataOutputStream(client.getOutputStream())

out.writeUTF(file.getName())// 附带文件名

int bufferSize = 2048// 2K

byte[] buf = new byte[bufferSize]

int read = 0

while ((read = reader.read(buf)) != -1) {

out.write(buf, 0, read)

}

out.flush()

} catch (IOException ex) {

ex.printStackTrace()

close()

} finally {

try {

reader.close()

out.close()

} catch (IOException e) {

e.printStackTrace()

}

}

}

/**

* 关闭Socket

*/

public void close() {

if (client != null) {

try {

client.close()

} catch (IOException e) {

e.printStackTrace()

}

}

}

public static void main(String[] args) {

Client client = new Client(Server.host, Server.port)

if (client.isConnected()) {

client.sendFile("E:\\EKI.txt")

client.close()

}

}

}

package ch.socket.file

import java.net.Socket

import java.io.*

public class SocketConnection extends Thread{

private Socket client

private String filepath

public SocketConnection(Socket client, String filepath){

this.client = client

this.filepath = filepath

}

public void run(){

if(client == null) return

DataInputStream in = null

DataOutputStream writer = null

try{

//3、访问Socket对象的getInputStream方法取得客户端发送过来的数据流

in = new DataInputStream(new BufferedInputStream(client.getInputStream()))

String fileName = in.readUTF()//取得附带的文件名

if(filepath.endsWith("/") == false &&filepath.endsWith("\\") == false){

filepath += "\\"

}

filepath += fileName

//4、将数据流写到文件中

writer = new DataOutputStream(new BufferedOutputStream(new BufferedOutputStream(new FileOutputStream(new File(filepath)))))

int bufferSize = 2048

byte[] buf = new byte[bufferSize]

int read = 0

while((read=in.read(buf)) != -1){

writer.write(buf, 0, read)

}

writer.flush()

System.out.println("数据接收完毕")

}catch(IOException ex){

ex.printStackTrace()

}finally{

try{

in.close()

writer.close()

client.close()

}catch(IOException e){

e.printStackTrace()

}

}

}

}

JMS是指Java消息服务,Java Message Service的简称。

Java 消息服务(Java Message Service,JMS)应用程序接口是一个Java 平台中关于面向消息中间件(MOM)的API,用于在两个应用程序之间,或分布式系统中发送消息,进行异步通信。Java 消息服务是一个与具体平台无关的 API,绝大多数 MOM 提供商都对 JMS 提供支持。

Java 消息服务的规范包括两种消息模式,点对点和发布者/订阅者。许多提供商支持这一通用框架因此,程序员可以在他们的分布式软件中实现面向消息的操作,这些操作将具有不同面向消息中间件产品的可移植性。

扩展资料

JMS的优势:

1、异步

JMS天生就是异步的,客户端获取消息的时候,不需要主动发送请求,消息会自动发送给可用的客户端。

2、可靠

JMS保证消息只会递送一次。大家都遇到过重复创建消息问题,而JMS能帮你避免该问题。

在JMS中,消息的接收可以使用以下两种方式:

同步:使用同步方式接收消息的话,消息订阅者调用receive()方法。在receive()中,消息未到达或在到达指定时间之前,方法会阻塞,直到消息可用。

异步:使用异步方式接收消息的话,消息订阅者需注册一个消息监听者,类似于事件监听器,只要消息到达,JMS服务提供者会通过调用监听器的onMessage()递送消息。

参考资料来源:百度百科-Java消息服务

网络任何位置放一个SQL Server,客户端去连接SQL Server,做数据操作共享,这样算是一个B/S或者说一个C/S通信。服务器就是SQL Server,但这个不是点对点,叫分布式

传输命令用Socket类,他的作用是传送字节形式,分为UDP/TCP通信方式,这类方法实际上比JDBC还要耗费资源