发送图像时发生错误客户端 - 服务器 - 客户端是“损坏的JPEG数据:4350674标记0xd9之前的无关字节”我可以更改什么?这是我的代码:
服务器
InputStream is = klienciRemoteScreen[1].getInputStream();
OutputStream os = klienciRemoteScreen[0].getOutputStream();
byte[] bytesize = new byte[4];
is.read(bytesize);
int size = ByteBuffer.wrap(bytesize).asIntBuffer().get();
byte [] image= new byte [size];
byte[]bufferimage = new byte [1];
os.write(bytesize);
os.flush();
int count;
while((count = is.read(image)) > 0)
{
os.write(image);
os.flush();
}
os.close();
客户端将图片发送到服务器
OutputStream os = noguiklient.sockRemoteScreen.getOutputStream();
Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
Rectangle rectangle = new Rectangle(dim);
Robot robot = new Robot();
BufferedImage image = robot.createScreenCapture(rectangle);
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
ImageIO.write(image, "jpg", byteArrayOutputStream);
byte[] size =
ByteBuffer.allocate(4).putInt(byteArrayOutputStream.size()).array();
int sizeas = ByteBuffer.wrap(size).asIntBuffer().get();
os.write(size);
os.write(byteArrayOutputStream.toByteArray());
os.close();
客户端从服务器接收
JFrame okno = new JFrame();
okno.setSize(1300, 1000);
okno.setTitle(" Remote screen");
okno.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
JPanel panel = new JPanel();
panel.setSize(1300,1000);
panel.setLayout(null);
okno.add(panel);
okno.setVisible(true);
InputStream is = Klient.sockRemoteScreen.getInputStream();
File photo = new File ("photo") ;
FileOutputStream fos = new FileOutputStream(photo);
byte[] buffer = new byte[4];
is.read(buffer);
int size = ByteBuffer.wrap(buffer).asIntBuffer().get();
byte[] image = new byte [size];
int count;
int i =0 ;
while((count = is.read(image)) > 0)
{
fos.write(image);
fos.flush();
}
ImageIcon screen = new ImageIcon("photo");
Image imagefinal = screen.getImage();
imagefinal =
imagefinal.getScaledInstance(okno.getWidth(),okno.getHeight(), Image.SCALE_SMOOTH);
BufferedImage(width,height,BufferedImage.TYPE_INT_ARGB);
Graphics graphics = panel.getGraphics();
while(true)
{
graphics.drawImage(imagefinal, 0, 0, panel.getWidth(), panel.getHeight(), panel);
}
当我尝试使用ImageIO发送时,一切都很好但我想尝试使用字节并且存在问题。 谢谢:))