客户端
import java.io.*;import .Socket;/*** ### 作业3:* 完成文件上传案例学习* - 案例需求* 客户端:数据来自于本地文件,接收服务器反馈* 服务器:接收到的数据写入本地文件,给出反馈* - 案例分析* - 创建客户端对象,创建输入流对象指向文件,每读一次数据就给服务器输出一次数据,输出结束后使用shutdownOutput()方法告知服务端传输结束* - 创建服务器对象,创建输出流对象指向文件,每接受一次数据就使用输出流输出到文件中,传输结束后。使用输出流给客户端反馈信息* - 客户端接受服务端的回馈信息*/public class TcpSocket {public static void main(String[] args) throws IOException {Socket socket = new Socket("127.0.0.1", 10086);BufferedInputStream buffInput = new BufferedInputStream(new FileInputStream("C:\\Users\\Administrator\\Desktop\\Demo小案例\\共享文件夹\\IEDA背景图片\\img\\1.png"));BufferedOutputStream buffOut = new BufferedOutputStream(socket.getOutputStream());byte[] bytes = new byte[1024];int length;while ((length = buffInput.read(bytes)) != -1) {buffOut.write(bytes, 0, length);buffOut.flush();}buffInput.close();socket.shutdownOutput();//读取反馈BufferedReader reader = new BufferedReader(new InputStreamReader(socket.getInputStream()));String line;while ((line = reader.readLine()) != null) {System.out.println(line);}reader.close();socket.close();}}
服务端
import java.io.*;
import .ServerSocket;
import .Socket;
import java.util.UUID;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
/**
@author silence
*/
public class TcpServerSocket {
public static void main(String[] args) throws IOException {
//监听端口
ServerSocket socket = new ServerSocket(10086);
//创建线程池
ThreadPoolExecutor thread = new ThreadPoolExecutor(
5,
10,
60,
TimeUnit.SECONDS,
new ArrayBlockingQueue<>(5),
Executors.defaultThreadFactory(),
new ThreadPoolExecutor.AbortPolicy()
);
while (true) {//获得与客户端的连接对象Socket accept = socket.accept();//将连接对象交给线程处理thread.submit(() -> {ioFile(accept);});}
}
/**
@param accept 与客户端的连接对象
*/
private static void success(Socket accept) {
//字符输出
BufferedWriter writer = null;
try {
//通过于客户端的连接,获得网络连接对象,获得字节输出流
OutputStream out = accept.getOutputStream();
//通过转换流,转换到字符输出流
writer = new BufferedWriter(new OutputStreamWriter(out));
writer.write(“服务器接收文件成功”);
writer.flush();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (writer != null) {
try {
writer.close();
} catch (IOException e) {
e.printStackTrace();
}
}
try {
accept.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
/**
读取客户端的文件信息,写到本地@param accept 与客户端的连接对象
*/
private static void ioFile(Socket accept) {
//字节输入流
BufferedInputStream input = null;
//字节输出流
BufferedOutputStream out = null;
try {
//字节输入流
input = new BufferedInputStream(accept.getInputStream());
//字节输出流
out = new BufferedOutputStream(new FileOutputStream(“operate/src/operate424/operate/operate3/” + UUID.randomUUID() + “.png”));
//每次读取的字节数组
byte[] bytes = new byte[1024];
//每次读取的字节个数
int length;
while ((length = input.read(bytes)) != -1) {
out.write(bytes, 0, length);
}
System.out.println(“服务器成功接收到一个文件”);
//给客户端写成功信息
success(accept);
} catch (IOException e) {
e.printStackTrace();
} finally {
if (out != null) {
try {
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (input != null) {
try {
input.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}