我在 Python 中有我的服务器,在 android (java) 中有客户端,我正在尝试通过套接字发送图像。它给了我一个错误图像文件被截断
服务器端 (Python)// 图像被成功录制,我们可以打印图像尺寸,但是当尝试一点一点地读取它时,它说图像被截断。
server_socket = socket.socket()
server_socket.bind((ip,port))
server_socket.listen(1)
print("server is listening")
while True:
(client_socket,client_adress) = server_socket.accept()
print("Socket completed")
client_input = client_socket.recv(65536)
print("data recieve ")
b=base64.b64decode(client_input)
#print(b)
imageStream = io.BytesIO(client_input)
imageFile = Image.open(imageStream)
# i = Image.fromarray(imageFile, mode='L').convert('1')
imageFile.LOAD_TUNCATED_IMAGES=True
print(imageFile.size)
try:
#this is the image ,here you cna verify via image size
modelRes=test(imageFile)
modelRes=modelRes[0]
modelRes= int(modelRes[0])
data = database(decode(imageFile))
print(data)
#json_obj = [{ "model_res": modelRes , "decode_data": data}]
x = { "model_res": modelRes, "decode_res": data}
# convert into JSON:
data = json.dumps(x)
print('data is'+data)
client_socket.send(bytes(data,encoding="utf-8"))
except Exception as e:
print(e)
client_socket.close()
server_socket.close()
客户端(android(java))(图像正在成功发送,但在服务器端它给出了错误)
protected String doInBackground(File... voids) { File file= voids[0];
Log.d("Sos","creating scoket ");
System.out.println("Client data: "+"creating scoket ");
try {
socket=new Socket(ip, port);
System.out.println("Socket donee : ");
byte[] bArray = new byte[(int) file.length()];
try{
fis = new FileInputStream(file);
fis.read(bArray);
fis.close();
}catch(IOException ioExp){
ioExp.printStackTrace();
}
Log.d("Data to bytes"," images has been converted to byte");
out = new DataOutputStream(socket.getOutputStream());
out.write(bArray);
Log.d("Data send"," Data ahs been send Successfully");
InputStreamReader streamReader= new InputStreamReader(socket.getInputStream());
BufferedReader reader= new BufferedReader(streamReader);
String value= reader.readLine();
System.out.println(value);
Log.d("Data Revieve"," Recieve data Successfully");
reader.close();
dataRec="";
dataRec=value;
Log.d("Socket closed "," Socket has been Successfully");
socket.close();
}catch (IOException e){
e.printStackTrace();
}
return null;
}