1. 程式人生 > >java多執行緒網路伺服器端向客戶端傳送檔案

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();
		}
	
	}
}