java 并发上传文件_javase知识分享-53-基于TCP协议的文件并发上传

该博客介绍了如何使用Java实现一个并发处理TCP协议文件上传的服务器。通过创建ServerSocket监听客户端连接,利用线程池处理并发请求,接收到文件信息后将其保存至本地,并在接收完成后向客户端发送确认信息。
摘要由CSDN通过智能技术生成

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

}

}

}

}

}

}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值