日期:2014-05-20 浏览次数:20893 次
public void run() { try { //获取当前时间 Calendar rightNow = Calendar.getInstance(); Date time = rightNow.getTime(); System.out.println(time.toString()); //开始连接 System.out.println(time.toString()+"-->S: Connecting..." ); ServerSocket serverSocket = new ServerSocket(SERVERPORT); while (true) { //开始接收 for (int j = 0; j < 10; j++) { //String imgname = "test"; Socket client = serverSocket.accept(); System.out.println(time.toString()+"-->S: Receiving..."); //输出为图像 try { InputStream stream = client.getInputStream(); FileOutputStream bos = new FileOutputStream( "d:\\imgtest\\test"+j+".jpg "); // FileOutputStream bos = new FileOutputStream( "d:\\test01.jpg "); int bytesRead = 0; // while(stream.read()!=-1) // bytesRead++; // System.out.println(bytesRead+"bytes"); byte[] buffer = new byte[8192]; bytesRead = 0; while ((bytesRead = stream.read(buffer, 0, 8192)) != -1) { bos.write(buffer, 0, bytesRead); } bos.close(); stream.close(); } catch(Exception e) { System.out.println(time.toString()+"-->S: Error"); e.printStackTrace(); } finally { client.close(); System.out.println(time.toString()+"-->S: Done."); } } } } catch (Exception e) { System.out.println("S: Error"); e.printStackTrace(); } }