Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/363.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java套接字,获取图像文件,但它不';t打开_Java_Sockets - Fatal编程技术网

Java套接字,获取图像文件,但它不';t打开

Java套接字,获取图像文件,但它不';t打开,java,sockets,Java,Sockets,这是我的第一个问题,所以我希望我写对了 我试图通过Java套接字发送一个byte[]数组,该数组包含一个映像 以下是发送文件的代码: public void WriteBytes(FileInputStream dis) throws IOException{ //bufferEscritura.writeInt(dis.available()); --- readInt() doesnt work correctly Write(String.valueOf((int)dis.

这是我的第一个问题,所以我希望我写对了

我试图通过Java套接字发送一个byte[]数组,该数组包含一个映像

以下是发送文件的代码:

public void WriteBytes(FileInputStream dis) throws IOException{
    //bufferEscritura.writeInt(dis.available()); --- readInt() doesnt work correctly
    Write(String.valueOf((int)dis.available()) + "\r\n");
    byte[] buffer = new byte[1024];
    int bytes = 0;
    while((bytes = dis.read(buffer)) != -1){
        Write(buffer, bytes);
    }
    System.out.println("Photo send!");
}
 public void Write(byte[] buffer, int bytes) throws IOException {
    bufferEscritura.write(buffer, 0, bytes);
}
public void Write(String contenido) throws IOException {
    bufferEscritura.writeBytes(contenido);
}
我的形象:

URL url = this.getClass().getResource("fuegos_artificiales.png");
FileInputStream dis = new FileInputStream(url.getPath());
sockManager.WriteBytes(dis);
获取图像文件的我的代码:

public byte[] ReadBytes() throws IOException{
DataInputStream dis = new DataInputStream(mySocket.getInputStream());
int size = Integer.parseInt(Read());
System.out.println("Recived size: "+ size);
byte[] buffer = new byte[size];
System.out.println("We are going to read!");
dis.readFully(buffer);
System.out.println("Photo received!");
return buffer;
byte[] array = tcpCliente.getSocket().LeerBytes();
FileOutputStream fos = new FileOutputStream("porfavor.png");
try {
    fos.write(array);
}
finally {
    fos.close();
}
}

要创建图像文件,请执行以下操作:

public byte[] ReadBytes() throws IOException{
DataInputStream dis = new DataInputStream(mySocket.getInputStream());
int size = Integer.parseInt(Read());
System.out.println("Recived size: "+ size);
byte[] buffer = new byte[size];
System.out.println("We are going to read!");
dis.readFully(buffer);
System.out.println("Photo received!");
return buffer;
byte[] array = tcpCliente.getSocket().LeerBytes();
FileOutputStream fos = new FileOutputStream("porfavor.png");
try {
    fos.write(array);
}
finally {
    fos.close();
}
图像文件已创建,但当我尝试打开它(例如,使用Paint)时,它会说它无法打开,因为它已损坏。。。 我还试着用记事本打开两张图片(原始和新的),它们里面有相同的数据

我不知道发生了什么

我希望你能帮助我

谢谢

  • 不要使用available()作为文件长度的度量。事实并非如此。Javadoc中有一个关于这一点的特别警告

  • 使用DataOutputStream.writeInt()写入长度,使用DataInputStream.readInt()读取长度,并使用相同的流读取图像数据。不要在同一个套接字上使用多个流

  • 在这方面:

    URL url = this.getClass().getResource("fuegos_artificiales.png");
    FileInputStream dis = new FileInputStream(url.getPath());
    
    第二行应该是:

    InputStream in = URL.openConnection.getInputStream();
    

    类资源不是文件。

    在notepand中打开文件不是一种好的比较方法。比较发送前和接收后字节数组的长度。是的,我比较了它,在发送ind bytes之前,我写入了文件的长度,在读取之前,我创建了一个与接收到的值大小相同的byte[]缓冲区。此外,生成的文件与原始文件的大小相同,因此我不知道发生了什么…您可以使用
    URLConnection.getContentLength()
    获取图像数据的大小。