好的,对于我的服务器程序我试图发送15张图片,代码:
try{
//t1.start();
for(int i =1; i<16; i++){
baos = new ByteArrayOutputStream();
BufferedImage img = ImageIO.read(new File("myimage"+i+".jpg"));
ImageIO.write(img, "jpg", baos);
baos.flush();
imageInByte=baos.toByteArray();
outToClient.write(imageInByte,0,imageInByte.length);
outToClient.flush();
//System.out.println(Arrays.toString(imageInByte));
//ImageIO.write(img, "jpg", outToClient);
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
这似乎工作正常,它的客户端我有问题。接收图像的代码:
try
{
Socket sock = new Socket("127.0.0.1", 8001);
DataInputStream inFromServer = new DataInputStream(sock.getInputStream());
System.out.println("networking established");
byte[] message = new byte[120947];
while(true){
inFromServer.read(message);
BufferedImage image = ImageIO.read(new ByteArrayInputStream(message));
File outputfile = new File("saved1.jpg");
ImageIO.write(image, "jpg", outputfile);
}
}catch(IOException ex){
ex.printStackTrace();
}
好的所以我知道while(true)循环不会工作,我已经尝试了很多替代方法,如if语句来检查datainputstream的字节等,但我没有喜悦。有谁知道如何从服务器连续接收byte []数组?任何帮助将不胜感激。