package com.bjsxt.net1116;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
public class MultipleUploadServer {
public static void main(String[] args) throws Exception {
receiveFile1();
}
private static void receiveFile() throws Exception{
ServerSocket ss = null;
try {
ss = new ServerSocket(1777);
while(true){
Socket socket = ss.accept();
//启动一个线程,用于处理和当前生成的socket 对象 的通信的任务。
new ServerThread(socket).start();
}
} finally {
ss.close();
}
}
//线程池实现
private static void receiveFile1() throws Exception{
ServerSocket ss = null;
ExecutorService pool = Executors.newCachedThreadPool();
try {
ss = new ServerSocket(1777);
while(true){
Socket socket = ss.accept();
//启动一个线程,用于处理和当前生成的socket 对象 的通信的任务。
//new ServerThread(socket).start();
pool.execute(new ServerRunnable(socket));
}
} finally {
ss.close();
}
}
static class ServerThread extends Thread{
private Socket socket;
public ServerThread(Socket socket) {
this.socket = socket;
}
@Override
public void run() {
try {
//接收客户端的一个上传文件的信息,并给客户端一个反馈
//创建一个文件夹
File file = new File("c:/server");
if(!file.exists())
file.mkdir();
//先读取文件的名字
DataInputStream dis = new DataInputStream(socket.getInputStream());
String fileName = dis.readUTF();
String ip = socket.getInetAddress().getHostAddress();
long time = System.currentTimeMillis();
fileName = ip+"_" + time + "_" + fileName;
//本地生成的新的文件
file = new File(file,fileName);
//读取客户端发送的字节数据,写到本地
BufferedInputStream bis = new BufferedInputStream(socket.getInputStream());
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(file));
byte[] buf = new byte[1000];
int count = bis.read(buf);
while(count != -1){
//写出到本地
bos.write(buf, 0, count);
bos.flush();
Thread.sleep(20);
count = bis.read(buf);
}
//接收完毕之后,给客户端反馈
DataOutputStream dos = new DataOutputStream(socket.getOutputStream());
dos.writeUTF("服务器接收完毕了!");
bos.close();
} catch(Exception e){
e.printStackTrace();
}finally {
if(socket != null){
try {
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
//线程池 实现 对应的类
static class ServerRunnable implements Runnable{
private Socket socket;
public ServerRunnable(Socket socket) {
this.socket = socket;
}
@Override
public void run() {
try {
//接收客户端的一个上传文件的信息,并给客户端一个反馈
//创建一个文件夹
File file = new File("c:/server");
if(!file.exists())
file.mkdir();
//先读取文件的名字
DataInputStream dis = new DataInputStream(socket.getInputStream());
String fileName = dis.readUTF();
String ip = socket.getInetAddress().getHostAddress();
long time = System.currentTimeMillis();
fileName = ip+"_" + time + "_" + fileName;
//本地生成的新的文件
file = new File(file,fileName);
//读取客户端发送的字节数据,写到本地
BufferedInputStream bis = new BufferedInputStream(socket.getInputStream());
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(file));
byte[] buf = new byte[1000];
int count = bis.read(buf);
while(count != -1){
//写出到本地
bos.write(buf, 0, count);
bos.flush();
Thread.sleep(20);
count = bis.read(buf);
}
//接收完毕之后,给客户端反馈
DataOutputStream dos = new DataOutputStream(socket.getOutputStream());
dos.writeUTF("服务器接收完毕了!");
bos.close();
} catch(Exception e){
e.printStackTrace();
}finally {
if(socket != null){
try {
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
}