package cn.itcast_09; /* * 使用TCP接收图片: * * */ import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.FileOutputStream; import java.io.OutputStream; import java.net.ServerSocket; import java.net.Socket; public class UploadImageServer { public static void main(String[] args) throws Exception { //创建服务器套接字 ServerSocket ss = new ServerSocket(4444); //监听客户端的连接请求 Socket s = ss.accept(); //封装图片的对象和管道流对象 BufferedOutputStream bos = new BufferedOutputStream( new FileOutputStream("bb.png")); BufferedInputStream bis = new BufferedInputStream( s.getInputStream()); //读写数据 byte [] bys = new byte[1024]; int len = 0; while((len=bis.read(bys))!=-1){ bos.write(bys, 0, len); bos.flush(); } //发送反馈 OutputStream os = s.getOutputStream(); byte [] by = "上传完成!".getBytes(); int lenth = 0; os.write(by); //释放资源 bos.close(); s.close(); } }