public class TCPCLeint {
public static void main(String[] args) throws IOException {
//创建客户端对象
Socket s = new Socket("127.0.0.1",9898);
//指定路径
FileInputStream fis = new FileInputStream("E:\\a.png");
//读取服务器数据
OutputStream out = s.getOutputStream();
byte[] bys = new byte[1024];
int len = 0;
while ((len=fis.read(bys))!=-1){
out.write(bys,0,len);
}
//告知书写完毕
s.shutdownOutput();
//书写到服务器
InputStream in = s.getInputStream();
len = in.read(bys);
System.out.println("服务器:" + new String(bys,0,len));
//释放资源
fis.close();
s.close();
in.close();
}
}import java.io.FileOutputStream;import java.io.IOException;
import java.io.InputStream;
import java.net.ServerSocket;
import java.net.Socket;
public class TCPServer {
public static void main(String[] args) throws IOException {
//创建服务器对象
ServerSocket server = new ServerSocket(9898);
//等待客户端连接,如果有客户端连接,获取客户端对象
Socket s = server.accept();
//当前在服务器中,读取数据
InputStream in = s.getInputStream();
//当前在服务器中,将数据写到流中
FileOutputStream fos = new FileOutputStream("E:\\xiangmu\\Copy.png");
byte[] bys = new byte[1024];
int len=0;
while ((len=in.read(bys))!=-1){
fos.write(bys,0,len);
}
//写完数据提升上传成功
s.getOutputStream().write("上传成功".getBytes());
//释放资源
fos.close();
s.close();
server.close();
}
}继续加油把,