gpt4 book ai didi

java - python服务器和java客户端: image is corrupted when sent from python server to java client

转载 作者:行者123 更新时间:2023-12-02 12:04:42 25 4
gpt4 key购买 nike

我有一个 python 服务器,我的 java 客户端将连接到该服务器,连接后,python 服务器将向 Java 客户端发送图像。我在 Java 端收到一个文件,但图像已损坏,我无法打开它。它的大小与原始图像相同,但已损坏。如果有人能指出我做错了什么,那将非常有帮助。提前致谢。

编辑:python服务器与python客户端一起工作,所以我认为它的问题与Java代码有关。链接到 python 客户端和服务器:http://www.bogotobogo.com/python/python_network_programming_server_client_file_transfer.php

以下是我的代码:

Python 服务器:

import socket                   # Import socket module
port = 2200 # Reserve a port for your service.
s = socket.socket() # Create a socket object
host = socket.gethostname() # Get local machine name
s.bind((host, port)) # Bind to the port
s.listen(5) # Now wait for client connection.
print 'Server listening....'
while True:
conn, addr = s.accept() # Establish connection with client.
print 'Got connection from', addr
data = conn.recv(1024)
print('Server received', repr(data))
filename='imagefile.jpg'
f = open(filename,'rb')
l = f.read(1024)
while (l):
conn.send(l)
print('Sent ',repr(l))
l = f.read(1024)
f.close()
print('Done sending')
conn.send('Thank you for connecting')
conn.close()

Java 客户端:

    String usr2ConnectDefault = "127.0.0.1";
InetAddress ip = InetAddress.getLocalHost();
int port2ConnectDefault = 2200;
Socket socket;
BufferedReader in;
PrintWriter out;
System.out.print(ip.getHostAddress());
socket = new Socket(ip.getHostAddress(), port2ConnectDefault);

System.out.println("Connected to server...sending echo string");
in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
out = new PrintWriter(socket.getOutputStream(), true);

out.println("Begin transfer:");
int sizeOfFile = 32336;

byte[] fileData = new byte[sizeOfFile];
for (int i = 0; i < sizeOfFile; i++)
{
byte bite = (byte) in.read();
fileData[i] = bite;
System.out.println(fileData[i]);
}
// save file to disk
System.out.println(fileData);
FileOutputStream fos = null;
fos = new FileOutputStream("fileImage.png");
fos.write(fileData);
fos.close();

out.close();
// verify if request is OK
String readString = in.readLine();
in.close();

socket.close();

最佳答案

在您的代码中:

l = f.read(1024)
while (l):
conn.send(l)
print('Sent ',repr(l))
l = f.read(1024)
f.close() # here

文件关闭得太早。将发送图像的前 1024 字节。您应该减少该行的一个缩进:

l = f.read(1024)
while (l):
conn.send(l)
print('Sent ',repr(l))
l = f.read(1024)
f.close()

而且,我认为你的最后三行应该添加一个缩进,以接受即将到来的客户。因此,while 语句应该是这样的:

while True:
conn, addr = s.accept() # Establish connection with client.
print 'Got connection from', addr
data = conn.recv(1024)
print('Server received', repr(data))
filename='imagefile.jpg'
f = open(filename,'rb')
l = f.read(1024)
while (l):
conn.send(l)
print('Sent ',repr(l))
l = f.read(1024)
f.close()
print('Done sending')
conn.send('Thank you for connecting')
conn.close()

在 Java 客户端中,您已对图像大小进行了硬编码:

int sizeOfFile =  32336;          

byte[] fileData = new byte[sizeOfFile];

并将整个数组写入文件:

fos = new FileOutputStream("fileImage.png");    
fos.write(fileData);

这就是为什么服务器发送 1024B 但客户端创建与原始大小相同的图像。

关于java - python服务器和java客户端: image is corrupted when sent from python server to java client,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46987483/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com