我正在尝试将图像从Socket服务器(Java)发送到套接字客户端(Python),但客户端中收到的图像大小不正确且无法打开。我不知道我做错了什么。有什么建议吗?
服务器(Java):
...
BufferedImage image = ImageIO.read (new File("c:\\tmp\\File.png"));
ImageIO.write(image, "png", connectionSocket.getOutputStream());
...
客户端(Python):
try:
client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
client_socket.connect( host , port )
data = client_socket.recv( 1024 )
fp = open ( path, "wb" )
while data:
data = client_socket.recv( 1024 )
if data:
if not data: break
fp.write ( data )
fp.flush()
fp.close()
client_socket.close()
except:
print "could not connect to %s:%s " %( host, port
答案 0 :(得分:1)
将图像转换为base64字符串(Java),然后传输字符串并返回图像数据(python)。
在java中,从base64字符串中获取Image:
public static Image image(String base64Img) {
byte[] b = DatatypeConverter.parseBase64Binary(base64Img);
ByteArrayInputStream s = new ByteArrayInputStream(b);
return new Image(s);
}