gpt4 book ai didi

java - 通过 Socket 发送图像

转载 作者:行者123 更新时间:2023-12-01 05:09:19 25 4
gpt4 key购买 nike

我在通过套接字(客户端-服务器)发送图像时遇到一个小问题,我只收到“UTF”文本,但没有收到图像对象,代码有问题吗?,

此代码仅发送 UTF txt,并在服务器端接收它,我使用 UTF 文本在服务器端识别图像(名称),识别后可以将图像对象发送到客户端

    /*
* ServerSide
*
*/
package Interface_class;

import configuraciones.procesador;
import java.awt.image.BufferedImage;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.SocketTimeoutException;
import javax.imageio.ImageIO;

/**
*
* @author TheCoder
*/
public class img_monitor extends Thread{
ServerSocket serverSocket;
Socket server;
BufferedImage bimg;
byte[] bytes;
public img_monitor()
{
try{
//Opening server socket
serverSocket = new ServerSocket(6066);
System.out.println("Conectado al Socket!");
}
catch(IOException ex){
System.out.println("Error en: "+ex);

}
}

public void run()
{
//This class gets the path from a property file (works well)
procesador obj = new procesador();
obj.UBARCHIVO_CONFIG();

while(true)
{
try
{

server = serverSocket.accept();
System.out.println("Nuevo cliente conectado!");
DataInputStream din=new DataInputStream(server.getInputStream());
DataOutputStream dout=new DataOutputStream(server.getOutputStream());
//Receiving image "name" from client as txt
String nombre = din.readUTF();
System.out.println(nombre);
//Using path+name of image to identify and send over socket
bimg = ImageIO.read(new File(obj.getRuta_arch_img()+nombre));
System.out.println(obj.getRuta_arch_img()+nombre);
ImageIO.write(bimg,"JPG",dout);
System.out.println("Image sent!!!");
// server.close();

//lblimg.setIcon(img);
}
catch(IOException e)
{
e.printStackTrace();
break;
}
catch(Exception ex)
{
System.out.println(ex);
}
}
}

}

客户端

    /*
* Client Side
*
*/
package Interface_class;

import java.awt.image.BufferedImage;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.SocketTimeoutException;
import javax.imageio.ImageIO;

/**
*
* @author TheCoder
*/
public class Obtener_imagen extends Thread {

public Obtener_imagen() {
System.out.println("Loading Socket!");

}

public void run() {
//The class below gets info from database
CLIENTINFO_CLASS obj = new CLIENTINFO_CLASS();
obj.consulta();
try {
Socket socket = new Socket("localhost", 6066);
// DataInputStream din = new DataInputStream(socket.getInputStream());
DataOutputStream dout = new DataOutputStream(socket.getOutputStream());
//Using this to send image "name" to server, so it can identify and send image to client
dout.writeUTF(obj.getImg_name());
BufferedImage img = ImageIO.read(ImageIO.createImageInputStream(socket.getInputStream()));
System.out.println("Image received!!!!");
// socket.close();
} catch (IOException ex) {
System.out.println("Error al abrir el socket" + ex);
}
}
}

最佳答案

发送图像后,您不会关闭套接字,因此客户端永远不会停止读取。服务器应该关闭套接字输出流,并在finally block 中关闭套接字本身。

客户端还应该在finally block 中关闭他的套接字。

编辑:您不需要使用 ImageIO 在服务器上读取和写入图像文件,除非您要更改格式。只需复制字节即可。

关于java - 通过 Socket 发送图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12242033/

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