一、表示Socket服务端的类:Server.java
public class Server extends Thread{
public static int port = 6789;
public static String host = "10.60.1.127";
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();
}
}
Socket客户端处理类:SocketConnection.java
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();
}
}
}
}
二、表示客户端的类:Client.java
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();
}
}
}
分享到:
相关推荐
C#Socket文件传输,将文件名和文件保存到一个byte[]中传输,再通过解析获取文件名和文件保存到本地文件夹中
博文链接:https://chenjumin.iteye.com/blog/218471
VC++下socket传输文件的完整demo以及源码
实现了Socket传输文件,由客户端发送给服务端,文件可以是任何形式,可以参考学习。
delphi使用socket通讯例子-文件传输, delphi使用socket通讯例子-文件传输
c++ 编写的 mfc socket 文件传输例子 已通过编译 下载可直接使用 c++语言
关于C语言 socket数据 文件传输的例子,包括发送端和接受端,可以传输文件或者数据
比较好的利用socket实现文件传输功能。在Linux下测试可用。可以用来研究学习socket
最近找到了一个socket传文件的例子,感觉不错,传上来,供大家以后参考
Java基于Socket文件传输示例:服务器端和客户端
这个是我自己编写的网络传输,主要可以传输文件和信息(支持多文件多用户传输)。用C#编写,使用了Socket。包含了一个传输类,一个数据库连接类。是学习网络传输的好帮手。(完整源代码,采用VS2005编写)绝对可以...
可能工作中很少会用到Java写传输文件的程序,但是当这一需求来临之时,你可能会发现用Socket来传输大文件并不像想象中简单。最近花了不少功夫才实现了这一功能,能够快速、无差错地传输2G大的文件。现发上来跟有需要...
在vc++.net环境下开发的一个使用套接字进行文件传输的例子。
本文介绍的简单的例子中我们将通过socket来实现文件的网络传输和接收功能。要实现这两个功能我们需要通过vs.net建立两个实例来完成。一个发送端,一个接收端。
socket 文件传输,基于TCP的文件传输,是个不错的例子。
矿用产量数据采集应用实例,使用HPsocket框架结构,通过UDP协议接收从服务器上传的数据并进行解码
Socket进行文件传输的例子 服务器先向客户端传文件 客户端也可向服务器端传文件
socket编程的简单例子,属于socket编程入门级(本人正在学习中),感兴趣的朋友可以下载看看。 程序在ubuntu9.04下编译的,因为类库的不同,windows下VC编程调试通不过。