| package com.nets;
 import java.io.*;
 import java.net.*;
 public class PictureClient {
 /**
 * 上传图片
 * 客户端:
 * 1.服务端点
 * 2.读取客户端已有的图片数据
 * 3.通过Socekt输出流将数据发给服务端
 * 4.读取服务端反馈信息
 * 5.关闭流资源
 *
 */
 public static void main(String[] args) throws Exception{
 Socket s=new Socket(InetAddress.getLocalHost().getHostName(),10012);
 FileInputStream fis=new FileInputStream("1.jpg");
 OutputStream out=s.getOutputStream();
 
 byte[]buf=new byte[1024];
 int len=0;
 
 while((len=fis.read(buf))!=-1){
 out.write(buf,0,len);
 }
 //告诉服务端数据已写完,相当于发送-1,不然,服务端一直在接收客户端的数据,
 s.shutdownOutput();
 
 InputStream in=s.getInputStream();
 byte[]bufIn=new byte[1024];
 int num=in.read(buf);
 System.out.println(new String(buf,0,num));
 
 }
 }
 
 
 |