弄了两天了,一直解决不了,希望高手帮看看!
只能接受一张图片,接受完就出现EOFException了
谢谢了!
服务端:
只能接受一张图片,接受完就出现EOFException了
谢谢了!
服务端:
.................... while(true) { System.out.println("PictureDataThread run while! start"); byte[] displayImage = DisplayImage(); try { out.writeInt(DISPLAYDATA_ID); System.out.println("displayImage.length" + displayImage.length); out.write(displayImage); out.flush(); System.out.println("PictureDataThread run while end!"); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } public byte[] DisplayImage() { Rectangle rectangle = new Rectangle(0,0,screenSize.width,screenSize.height); BufferedImage image = robot.createScreenCapture(rectangle); ByteArrayOutputStream arrayOutputStream = new ByteArrayOutputStream(); try { ImageIO.write(image, "jpg", arrayOutputStream); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } byte[] data = arrayOutputStream.toByteArray(); System.out.println("DisplayImage"); return data; }
客户端:
............................ public static byte[] readStream() throws Exception { ByteArrayOutputStream outStream = new ByteArrayOutputStream(); byte[] buffer = new byte[1024]; int len = 0; while ((len = in.read(buffer)) != -1) { outStream.write(buffer,0,len); } return outStream.toByteArray(); } private String uri = "storage/sdcard0/DCIM/Camera/"; public void saveFile(Bitmap bm, String fileName) throws IOException { File dirFile = new File(uri); if (!dirFile.exists()) { dirFile.mkdir(); } File myCaptureFile = new File(uri + fileName); BufferedOutputStream bos = new BufferedOutputStream( new FileOutputStream(myCaptureFile)); bm.compress(Bitmap.CompressFormat.JPEG, 80, bos); bos.flush(); } .................................. while (true) { int readDisplay = in.readInt(); //这行报EOFException! if (readDisplay == DISPLAYDATA_ID) { Log.d(TAG, "diaplayData_start"); byte[] readBytes = null; try { readBytes = readStream(); } catch (Exception e) { // TODO Auto-generated catch // block e.printStackTrace(); } if (readBytes != null) { bmp = BitmapFactory.decodeByteArray(readBytes, 0, readBytes.length); int i = 1; i++; String picName = "pic" + i; saveFile(bmp, picName); Log.d(TAG, "diaplayData_end"); in.close(); clientSocket.close(); Log.d(TAG, "in,clientSocket close!!"); } } } ..................... class ConnectTask extends AsyncTask<String, Void, Boolean> { @Override protected Boolean doInBackground(String... arg0) { // TODO Auto-generated method stub Log.d(TAG, "ConnectTask doInBackGround"); try { clientSocket = new Socket(ipAddress, port); } catch (UnknownHostException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } return true; } @Override protected void onPostExecute(Boolean result) { // TODO Auto-generated method stub Log.d(TAG, "ConnectTask onPostExecute"); try { out = new DataOutputStream(clientSocket.getOutputStream()); in = new DataInputStream(clientSocket.getInputStream()); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }
解决方案
30
用了输入输出流 为什么没有执行关闭?