import java.io.*;
import java.net.*;
class TcpUploadPicServer
{
public static void main(String[] args) throws Exception
{
ServerSocket ss = new ServerSocket(9001);
Socket s = ss.accept();
//将客户端ip输出
String ip = s.getInetAddress().getHostAddress();
System.out.println(ip+".....connected");
//将客户端发送的图片数据存储到服务器
File file = new File("D:\\123.png");
BufferedInputStream bufIn = new BufferedInputStream((s.getInputStream()));
BufferedOutputStream bufOut = new BufferedOutputStream(new FileOutputStream(file));
byte[] buf = new byte[1024];
int len = 0;
while((len = bufIn.read(buf))!= -1){
bufOut.write(buf,0,len);
bufOut.flush();
}
//上传成功后,返回客户端“上传成功”信息
BufferedWriter bufw = new BufferedWriter(new OutputStreamWriter(s.getOutputStream()));
bufw.write("上传成功!");
bufw.flush();
s.shutdownOutput();
//关闭资源
bufOut.close();
s.close();
//ss.close();
}
}
import java.io.*;
import java.net.*;
class TcpUploadPicClient
{
public static void main(String[] args) throws Exception
{
Socket s = new Socket("192.168.6.250",5001);
File file= new File("E:"+File.separator+"_图视音"+File.separator
+"图片"+File.separator+"PIC Study"+File.separator+"0007.bmp");
BufferedInputStream bufIn =new BufferedInputStream(new FileInputStream(file));
BufferedOutputStream bufOut = new BufferedOutputStream(s.getOutputStream());
byte[] buf = new byte[1024];
int len = 0;
while((len = bufIn.read(buf))!= -1){
bufOut.write(buf,0,len);
bufOut.flush();
}
//告诉服务端发送数据结束
s.shutdownOutput();
BufferedReader bufr = new BufferedReader(new InputStreamReader(s.getInputStream()));
String line = null;
while((line = bufr.readLine())!=null){
System.out.println(line);
}
bufIn.close();
bufr.close();
s.close();
}
}