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