python服务器和java客户端:从python服务器发送到java客户端时,映像已损坏

时间:2017-10-28 07:52:45

标签: java python sockets client-server

我有一个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();

2 个答案:

答案 0 :(得分:1)

在您的代码中:

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的原因,但客户端会创建与原始图像大小相同的图像。

答案 1 :(得分:0)

解决方案是从python服务器读取整个文件,然后将其发送到Java客户端,并在Java端将其读取为BufferedImage,并使用ImageIO.write保存。