我只是使用 Java 通过套接字发送部分图像
I just send parts of the image through socket using Java
我用了Here下面的代码,结果只发送了一半的图片,谁能告诉我原因吗?获取大小和读取大小的代码对我来说太复杂了,所以我搞不懂。
public class Send {
public static void main(String[] args) throws Exception {
Socket socket = new Socket("localhost", 13085);
OutputStream outputStream = socket.getOutputStream();
BufferedImage image = ImageIO.read(new File("C:\Users\Jakub\Pictures\test.jpg"));
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
ImageIO.write(image, "jpg", byteArrayOutputStream);
byte[] size = ByteBuffer.allocate(4).putInt(byteArrayOutputStream.size()).array();
outputStream.write(size);
outputStream.write(byteArrayOutputStream.toByteArray());
outputStream.flush();
System.out.println("Flushed: " + System.currentTimeMillis());
Thread.sleep(120000);
System.out.println("Closing: " + System.currentTimeMillis());
socket.close();
}
}
public class Receive {
public static void main(String[] args) throws Exception {
ServerSocket serverSocket = new ServerSocket(13085);
Socket socket = serverSocket.accept();
InputStream inputStream = socket.getInputStream();
System.out.println("Reading: " + System.currentTimeMillis());
byte[] sizeAr = new byte[4];
inputStream.read(sizeAr);
int size = ByteBuffer.wrap(sizeAr).asIntBuffer().get();
byte[] imageAr = new byte[size];
inputStream.read(imageAr);
BufferedImage image = ImageIO.read(new ByteArrayInputStream(imageAr));
System.out.println("Received " + image.getHeight() + "x" + image.getWidth() + ": " + System.currentTimeMillis());
ImageIO.write(image, "jpg", new File("C:\Users\Jakub\Pictures\test2.jpg"));
serverSocket.close();
}
}
正如@EJP 评论的那样,最可能的问题是在接收部分,inputStream.read(byte[] byteArray) 无法完全加载byteArray。如果我使用 DataInputStream dis=new DataInputStream(inputStream) 然后调用 dis.readFully(byteArray) 它将加载 byteArray。虽然我无法弄清楚 inputStream 不起作用的原因。希望有人能说明原因,我们将不胜感激。修改后的代码如下:
public class Send {
public static void main(String[] args) throws Exception {
Socket socket = new Socket("localhost", 13085);
OutputStream outputStream = socket.getOutputStream();
BufferedImage image = ImageIO.read(new File("test.jpg"));
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
ImageIO.write(image, "jpg", byteArrayOutputStream);
byte[] size = ByteBuffer.allocate(4).putInt(byteArrayOutputStream.size()).array();
outputStream.write(size);
outputStream.write(byteArrayOutputStream.toByteArray());
outputStream.flush();
System.out.println("Flushed: " + System.currentTimeMillis());
Thread.sleep(120000);
System.out.println("Closing: " + System.currentTimeMillis());
socket.close();
}
}
public class Receive {
public static void main(String[] args) throws Exception {
ServerSocket serverSocket = new ServerSocket(13085);
Socket socket = serverSocket.accept();
InputStream inputStream = socket.getInputStream();
DataInputStream dataInputStream=new DataInputStream(inputStream);
System.out.println("Reading: " + System.currentTimeMillis());
byte[] sizeAr = new byte[4];
dataInputStream.read(sizeAr);
int size = ByteBuffer.wrap(sizeAr).getInt();
byte[] imageAr = new byte[size];
dataInputStream.readFully(imageAr);
BufferedImage image = ImageIO.read(new ByteArrayInputStream(imageAr));
System.out.println("Received " + image.getHeight() + "x" + image.getWidth() + ": " + System.currentTimeMillis());
ImageIO.write(image, "jpg", new File("received.jpg"));
serverSocket.close();
}
}
摆脱字节数组流并直接使用套接字流ImageIO
。
问题是您假设 read()
填满了缓冲区,但如果您按照上述操作,您将完全摆脱 read()
。
发送长度也是毫无意义的,因为无论如何您之后都会关闭它们的套接字。流的结尾足以分隔图像。
也摆脱睡眠。您不需要在网络代码中休眠。
我用了Here下面的代码,结果只发送了一半的图片,谁能告诉我原因吗?获取大小和读取大小的代码对我来说太复杂了,所以我搞不懂。
public class Send {
public static void main(String[] args) throws Exception {
Socket socket = new Socket("localhost", 13085);
OutputStream outputStream = socket.getOutputStream();
BufferedImage image = ImageIO.read(new File("C:\Users\Jakub\Pictures\test.jpg"));
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
ImageIO.write(image, "jpg", byteArrayOutputStream);
byte[] size = ByteBuffer.allocate(4).putInt(byteArrayOutputStream.size()).array();
outputStream.write(size);
outputStream.write(byteArrayOutputStream.toByteArray());
outputStream.flush();
System.out.println("Flushed: " + System.currentTimeMillis());
Thread.sleep(120000);
System.out.println("Closing: " + System.currentTimeMillis());
socket.close();
}
}
public class Receive {
public static void main(String[] args) throws Exception {
ServerSocket serverSocket = new ServerSocket(13085);
Socket socket = serverSocket.accept();
InputStream inputStream = socket.getInputStream();
System.out.println("Reading: " + System.currentTimeMillis());
byte[] sizeAr = new byte[4];
inputStream.read(sizeAr);
int size = ByteBuffer.wrap(sizeAr).asIntBuffer().get();
byte[] imageAr = new byte[size];
inputStream.read(imageAr);
BufferedImage image = ImageIO.read(new ByteArrayInputStream(imageAr));
System.out.println("Received " + image.getHeight() + "x" + image.getWidth() + ": " + System.currentTimeMillis());
ImageIO.write(image, "jpg", new File("C:\Users\Jakub\Pictures\test2.jpg"));
serverSocket.close();
}
}
正如@EJP 评论的那样,最可能的问题是在接收部分,inputStream.read(byte[] byteArray) 无法完全加载byteArray。如果我使用 DataInputStream dis=new DataInputStream(inputStream) 然后调用 dis.readFully(byteArray) 它将加载 byteArray。虽然我无法弄清楚 inputStream 不起作用的原因。希望有人能说明原因,我们将不胜感激。修改后的代码如下:
public class Send {
public static void main(String[] args) throws Exception {
Socket socket = new Socket("localhost", 13085);
OutputStream outputStream = socket.getOutputStream();
BufferedImage image = ImageIO.read(new File("test.jpg"));
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
ImageIO.write(image, "jpg", byteArrayOutputStream);
byte[] size = ByteBuffer.allocate(4).putInt(byteArrayOutputStream.size()).array();
outputStream.write(size);
outputStream.write(byteArrayOutputStream.toByteArray());
outputStream.flush();
System.out.println("Flushed: " + System.currentTimeMillis());
Thread.sleep(120000);
System.out.println("Closing: " + System.currentTimeMillis());
socket.close();
}
}
public class Receive {
public static void main(String[] args) throws Exception {
ServerSocket serverSocket = new ServerSocket(13085);
Socket socket = serverSocket.accept();
InputStream inputStream = socket.getInputStream();
DataInputStream dataInputStream=new DataInputStream(inputStream);
System.out.println("Reading: " + System.currentTimeMillis());
byte[] sizeAr = new byte[4];
dataInputStream.read(sizeAr);
int size = ByteBuffer.wrap(sizeAr).getInt();
byte[] imageAr = new byte[size];
dataInputStream.readFully(imageAr);
BufferedImage image = ImageIO.read(new ByteArrayInputStream(imageAr));
System.out.println("Received " + image.getHeight() + "x" + image.getWidth() + ": " + System.currentTimeMillis());
ImageIO.write(image, "jpg", new File("received.jpg"));
serverSocket.close();
}
}
摆脱字节数组流并直接使用套接字流ImageIO
。
问题是您假设 read()
填满了缓冲区,但如果您按照上述操作,您将完全摆脱 read()
。
发送长度也是毫无意义的,因为无论如何您之后都会关闭它们的套接字。流的结尾足以分隔图像。
也摆脱睡眠。您不需要在网络代码中休眠。