TCP 服务器文件上传 java

package xc;

import xc.Text.ck;
import xc.Text.hb;
import xc.Text.lw;
import xc.Text.sz;

import java.io.*;
import java.net.*;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Scanner;
import java.util.concurrent.*;

public class text {
    public static void main(String[] args) throws Exception {
        //获取本机ip和设置端口号
        Socket socket=new Socket("127.0.0.1",10086);
        //用Buffered输入流读取文件并注入Buffered输出流(记得用flush清空缓冲区)
        BufferedInputStream bis = new BufferedInputStream
                (new FileInputStream("C:\\Users\\35602\\javalx\\educate\\aaa\\屏幕截图_20221226_163426.png"));
        BufferedOutputStream bos = new BufferedOutputStream(socket.getOutputStream());
        byte[] bytes = new byte[1024];
        int len=0;
        while ((len=bis.read(bytes))!=-1){
            bos.write(bytes,0,len);
        }
        bos.flush();
        //暂停输出流
        socket.shutdownOutput();
        //BufferedReader读取服务器传过来的的数据
        BufferedReader br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
        String str = br.readLine();
        System.out.println(str);
        bis.close();
        socket.close();
    }
}
package xc.Text;

import java.io.*;
import java.net.Socket;
import java.util.UUID;

public class myrunable implements Runnable{
    //用构造方法获取socket对象
    Socket socket;
    public myrunable(Socket socket) {
        this.socket=socket;
    }
    @Override
    public void run() {
        //用Buffered输入流读取文件并注入Buffered输出流(记得用flush清空缓冲区)
        try {
                BufferedInputStream bis = new BufferedInputStream(socket.getInputStream());
                UUID uuid = UUID.randomUUID();
                String name = uuid.toString().replace("-", "");
                BufferedOutputStream bos = new BufferedOutputStream
                        (new FileOutputStream
                                ("C:\\Users\\35602\\javalx\\educate\\bbb\\"+name+".png"));
                int len;
                byte[] bytes = new byte[1024];
                while ((len=bis.read(bytes))!=-1){
                    bos.write(bytes,0,len);
                }
                //清空输出流
                bos.flush();
                //用BufferedWriter回复socket信息
                BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
                bw.write("接收成功");
                bw.newLine();
                bw.flush();
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            if (socket!=null){
                try {
                    socket.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

    }
}
package xc.Text;

import java.io.*;
import java.net.*;
import java.util.UUID;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

public class getdata {
    public static void main(String[] args) throws IOException {
        ServerSocket ss = new ServerSocket(10086);
        ThreadPoolExecutor pool = new ThreadPoolExecutor(
                3,
                16,
                60,
                TimeUnit.SECONDS,
                new ArrayBlockingQueue<>(2),
                Executors.defaultThreadFactory(),
                new ThreadPoolExecutor.AbortPolicy()
        );

        while (true){
            Socket socket = ss.accept();
            pool.submit(new myrunable(socket));
        }

    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值