java多线程网络服务器端向客户端发送文件

tcpServer.java

package com.tcp.server;

import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;
import java.util.List;

public class tcpServer {
	private final static String PATH="D:\\testio\\a\\a.txt";
	public static void main(String[] args) {
		List<Socket> list = new ArrayList<Socket>();
		try {
			ServerSocket server = new ServerSocket(8888);
			while(true){
				Socket socket = server.accept();
				list.add(socket);
				new Thread(new ServerSend(socket,PATH,list)).start();
			}
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}

ServerSend.java

package com.tcp.server;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.Socket;
import java.util.List;
import java.util.Scanner;

public class ServerSend implements Runnable {
	private Socket socket;
	private String path;
	private List<Socket> list;
	/**
	 * @param socket
	 * @param path
	 */
	public ServerSend(Socket socket, String path ,List<Socket> list) {
		super();
		this.socket = socket;
		this.path = path;
		this.list = list;
	}
	@Override
	public void run() {
		// TODO Auto-generated method stub
		File file = new File(path);
		InputStream is;
		try {
			is = new FileInputStream(file);
			BufferedReader br = new BufferedReader(new InputStreamReader(is));
			for (int i = 0; i < list.size(); i++) {
//				Socket sk =list.get(i);
				OutputStream os= socket.getOutputStream();
//			OutputStream os= sk.getOutputStream();
			BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(os));
			String str="fdas";
				while((str=br.readLine())!=null){
					bw.write(str);
					bw.newLine();
					bw.flush();
				}
			}
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
}

tcpClient.java

package com.tcp.server;

import java.io.IOException;
import java.net.Socket;
import java.net.UnknownHostException;

public class tcpClient {
	private final static String PATH="D:\\testio\\a\\client1.txt";
	public static void main(String[] args) {
		try {
			Socket socket= new Socket("localhost",8888);
			new Thread(new ClientRec(socket,PATH)).start();
		} catch (UnknownHostException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}

tcpClient2.java

package com.tcp.server;

import java.io.IOException;
import java.net.Socket;
import java.net.UnknownHostException;

public class tcpClient2 {
	private final static String PATH="D:\\testio\\a\\client2.txt";
	public static void main(String[] args) {
		try {
			Socket socket= new Socket("localhost",8888);
			new Thread(new ClientRec(socket,PATH)).start();
		} catch (UnknownHostException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}

clientRec.java

package com.tcp.server;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.Socket;

public class ClientRec implements Runnable {
	private Socket socket;
	private String path;
	/**
	 * @param socket
	 * @param path
	 */
	public ClientRec(Socket socket, String path) {
		super();
		this.socket = socket;
		this.path = path;
	}
	@Override
	public void run() {
		// TODO Auto-generated method stub
		InputStream is;
		try {
			is = socket.getInputStream();
			BufferedReader br = new BufferedReader(new InputStreamReader(is));
			File file = new File(path);
			OutputStream os = new FileOutputStream(file);
			BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(os));
			String msg=null;
			while((msg=br.readLine())!=null){
				bw.write(msg, 0, msg.length());
				bw.newLine();
				bw.flush();
				System.out.println(msg);
			}
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	
	}
}


  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值