package cn.itcast_09; import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.net.Socket; import java.net.UnknownHostException; /* * TCP上传图片的客户端: * */ public class UploadImageDemo { public static void main(String[] args) throws IOException { //创建套接字 Socket s = new Socket("192.168.1.103",4444); //封装图片文件对象和通道流对象 BufferedInputStream is = new BufferedInputStream(new FileInputStream("C:\\a.png")); BufferedOutputStream os = new BufferedOutputStream(s.getOutputStream()); //读写数据 byte [] bys = new byte[1024]; int len; while((len=is.read(bys))!=-1){ os.write(bys, 0, len); os.flush(); } s.shutdownOutput(); //读取反馈 InputStream bos = s.getInputStream(); byte [] by = new byte[1024]; int lenth = 0; lenth=bos.read(by); String ss = new String(by,0,lenth); System.out.println(ss); //释放资源 is.close(); s.close(); } }