1 客户端发送一个文件(图片文件)给服务端,服务端保存在本地
package test2;
import java.io.*;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.UnknownHostException;
public class TCPtest {
public static void main(String[] args) {
Socket socket= null;
OutputStream outputStream= null;
FileInputStream fis= null;
try {
socket = new Socket(InetAddress.getByName("127.0.0.1"),9090);
outputStream = socket.getOutputStream();
fis = new FileInputStream(new File("D:\\java\\2021.01.28library\\src\\test2\\1603688981208.gif"));
byte []bytes=new byte[1024];
int len;
while ((len=fis.read(bytes))!=-1){
outputStream.write(bytes,0,len);
}
System.out.println("图片已发送!");
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
outputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
class server1{
public static void main(String[] args) {
ServerSocket ss= null;
Socket sk= null;
InputStream is= null;
FileOutputStream fos= null;
try {
ss = new ServerSocket(9090);
sk = ss.accept();
is = sk.getInputStream();
fos = new FileOutputStream(new File("1.gif"));
byte[]buffer=new byte[1024];
int len;
while ((len=is.read(buffer))!=-1){
fos.write(buffer,0,len);
}
System.out.println("图片已接收!");
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
sk.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
ss.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
2 客户端给服务器发送图片,服务器接受后返回已收到
package test2;
import java.io.*;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
public class TCPtest2 {
public static void main(String[] args) {
Socket socket= null;
OutputStream outputStream= null;
FileInputStream fis= null;
try {
socket = new Socket(InetAddress.getByName("127.0.0.1"),9090);
outputStream = socket.getOutputStream();
fis = new FileInputStream(new File("D:\\java\\2021.01.28library\\src\\test2\\1603688981208.gif"));
byte []bytes=new byte[1024];
int len;
while ((len=fis.read(bytes))!=-1){
outputStream.write(bytes,0,len);
}
System.out.println("图片已发送!");
socket.shutdownOutput();
InputStream is=socket.getInputStream();
ByteArrayOutputStream byteArrayOutputStream=new ByteArrayOutputStream();
byte[]buffer=new byte[1024];
int len1;
while ((len1=is.read(buffer))!=-1){
byteArrayOutputStream.write(buffer,0,len1);
}
System.out.println(byteArrayOutputStream.toString());
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
outputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
class server2{
public static void main(String[] args) {
ServerSocket ss= null;
Socket sk= null;
InputStream is= null;
FileOutputStream fos= null;
try {
ss = new ServerSocket(9090);
sk = ss.accept();
is = sk.getInputStream();
fos = new FileOutputStream(new File("2.gif"));
byte[]buffer=new byte[1024];
int len;
while ((len=is.read(buffer))!=-1){
fos.write(buffer,0,len);
}
System.out.println("图片已接收!");
OutputStream outputStream=sk.getOutputStream();
outputStream.write("你好,文件已接收!".getBytes());
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
sk.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
ss.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
2 客户端:发送文件,数据来自于纯文本文件 ;服务器:接收到的数据写入文本文件
package demo2;
import java.io.*;
import java.net.Socket;
public class Client {
public static void main(String[] args) {
Socket socket= null;
BufferedReader bufferedReader= null;
try {
socket = new Socket("127.0.0.1",9090);
File file=new File("send.txt");
if(!file.exists()){
file.createNewFile();
}
bufferedReader = new BufferedReader(new FileReader(file));
BufferedWriter bufferedWriter=new BufferedWriter((new OutputStreamWriter(socket.getOutputStream())));
String linestr=null;
int linenum=1;
while ((linestr=bufferedReader.readLine())!=null){
bufferedWriter.write(linestr);
System.out.println("成功发送第"+linenum+"行");
bufferedWriter.newLine();
bufferedWriter.flush();
linenum++;
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
bufferedReader.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
package demo2;
import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
public class Server {
public static void main(String[] args) {
BufferedReader bufferedReader= null;
BufferedWriter bufferedWriter= null;
try {
ServerSocket serverSocket=new ServerSocket(9090);
Socket socket=serverSocket.accept();
bufferedReader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
bufferedWriter = new BufferedWriter(new FileWriter("receive.txt"));
String linestr=null;
int linenum=1;
while ((linestr=bufferedReader.readLine())!=null){
bufferedWriter.write(linestr);
System.out.println("成功接收第"+linenum+"行");
bufferedWriter.newLine();
bufferedWriter.flush();
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
bufferedWriter.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
bufferedReader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}