【发布时间】:2015-08-28 20:11:04
【问题描述】:
我一直在尝试创建一个在 Java 中运行客户端和在 Python 中运行服务器的程序。 我的总体目标是将图片从 Java 上的客户端上传到 Python 上的服务器并将其存储在 mysql 服务器上。我还没有尝试从 Python 上的图像转换为 mysql 上的 blob 和已经陷入上传到 python 阶段。这是以下代码:
客户端:(java)
client.send("upload##user##pass##"); //this is how i know that upload request has been sent.
String directory = "/home/michael/Pictures/"+field.getText();// a valid directory of a picture.
BufferedImage bufferedImage = null;
try {
bufferedImage = ImageIO.read(new File(directory));
} catch (IOException err) {
err.printStackTrace();
}
try {
ImageIO.write(bufferedImage, "png", client.socket.getOutputStream());//sending the picture via socket.
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
这是服务器:(Python)
elif mar[0]=="upload":##this is how i know that the request is to upload
buf = ''
while len(buf)<4:
buf += clientsock.recv(4-len(buf))
size = struct.unpack('!i', buf)
print "receiving %s bytes" % size
with open('tst.jpg', 'wb') as img:
while True:
data = clientsock.recv(1024)
if not data:
break
img.write(data)
print 'received, yay!'
此代码实际上不起作用并打印出我想发送的大量字节(图片大约为 2 gb) 我没有经常使用服务器/客户端,所以这段代码可能很糟糕。
【问题讨论】:
标签: java python mysql sockets server