2017-10-28 154 views
0

我有一个python服务器,我的java客户端将连接到并连接后,python服务器将发送图像到Java客户端。我正在Java端获得一个文件,但图像已损坏,无法打开。它与原始图像大小相同,但已损坏。如果有人能指出我做错了什么,那将会非常有帮助。谢谢你提前。python服务器和java客户端:从python服务器发送到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(); 
+0

在Python服务器上,'f.close()'调用应该在while(l)'循环之外。这是张贴在这里的错字还是代码中的错误? – User

+1

进行了更改。对不起,不正确的缩进。 – Student

+1

在**服务器上**不读只读'1024'读取'os.path.getsize(filename)'然后在**客户端上接收相同的大小** –

回答

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 statment应该是这样的:

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

+0

我已经在python代码中提出了改变。它仍然会创建一个损坏的图像。 – Student

+0

此外,我打印字节,因为我收到他们在Java代码。它的印刷数量,但最后它的打印数量为-1。 – Student

+0

@学生我很抱歉无法解决您的问题。我认为你应该首先确定问题的出处 - 服务器还是客户端?所以请尝试通过另一个程序(如nc)发送/接收文件。 – CSM

0

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