服务器端
package com.shu.net;
import javax.imageio.stream.FileImageOutputStream;
import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
public class TcpServerDemo {
public static void main(String[] args) {
ServerSocket serverSocket = null;
Socket socket = null;
InputStream inputStream = null;
FileImageOutputStream fileImageOutputStream = null;
// FileOutputStream fileOutputStream = null;
try {
// 1. 创建服务
serverSocket = new ServerSocket(9999);
// 2. 监听客户端的连接
socket = serverSocket.accept();
// 3. 获取输入流
inputStream = socket.getInputStream();
// 4. 输出文件
fileImageOutputStream = new FileImageOutputStream(new File("src/com/shu/net/receive.png"));
// fileOutputStream = new FileOutputStream(new File("src/com/shu/net/receive.png"));
byte[] buffer = new byte[1024];
int len;
while ((len = inputStream.read(buffer)) != -1) {
fileImageOutputStream.write(buffer,0,len);
// fileOutputStream.write(buffer,0,len);
}
// 5. 通知客户端已经接受完毕
OutputStream outputStream = socket.getOutputStream();
outputStream.write("Reveived Successfully".getBytes());
} catch (Exception e) {
e.printStackTrace();
} finally {
// 6. 关闭资源
if(fileImageOutputStream!=null){
try {
fileImageOutputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
// if(fileOutputStream!=null){
// try {
// fileOutputStream.close();
// } catch (IOException e) {
// e.printStackTrace();
// }
// }
if(inputStream!=null){
try {
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(socket!=null){
try {
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(serverSocket!=null){
try {
serverSocket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
客户端
package com.shu.net;
import javax.imageio.stream.FileImageOutputStream;
import java.io.*;
import java.net.InetAddress;
import java.net.Socket;
public class TcpClientDemo {
public static void main(String[] args) {
Socket socket = null;
OutputStream outputStream = null;
FileImageOutputStream fileImageOutputStream = null;
InputStream inputStream = null;
ByteArrayOutputStream byteArrayOutputStream = null;
try {
// 1. 获取服务端的地址和端口号
socket = new Socket(InetAddress.getLocalHost(),9999);
// 2. 创建一个输出流
outputStream = socket.getOutputStream();
// 3. 创建一个文件流用来读取文件
fileImageOutputStream = new FileImageOutputStream(new File("src/com/shu/net/Grid.png"));
// 4. 通过输出流写出文件
byte[] buffer = new byte[1024];
int len;
while ((len=fileImageOutputStream.read(buffer))!=-1){
outputStream.write(buffer,0,len);
}
// 5. 通知服务器, 已经结束传输
socket.shutdownOutput();
// 6. 确定服务端吧接受完毕后 才断开连接
inputStream = socket.getInputStream();
byteArrayOutputStream = new ByteArrayOutputStream();
byte[] buffer2 = new byte[1024];
int len2;
while ((len2=inputStream.read(buffer2))!=-1){
byteArrayOutputStream.write(buffer2,0,len2);
}
System.out.println(byteArrayOutputStream.toString());
} catch (Exception e) {
} finally {
// 7. 关闭资源
try {
if(byteArrayOutputStream!=null){
byteArrayOutputStream.close();
}
} catch (IOException e) {
e.printStackTrace();
}
try {
if(inputStream!=null){
inputStream.close();
}
} catch (IOException e) {
e.printStackTrace();
}
try {
if(fileImageOutputStream!=null){
fileImageOutputStream.close();
}
} catch (IOException e) {
e.printStackTrace();
}
try {
if(outputStream!=null){
outputStream.close();
}
} catch (IOException e) {
e.printStackTrace();
}
try {
if(socket!=null){
socket.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}