gpt4 book ai didi

java - Java中通过Socket编程传输图像文件

转载 作者:行者123 更新时间:2023-12-02 06:03:50 24 4
gpt4 key购买 nike

在我的代码中,客户端向服务器发送图像,服务器应该能够接收它并显示图像名称(此处的HelloNewPic.jpg)并将图像保存在桌面中。

服务器:

public class Server1 {

ServerSocket serverSocket = null;
Socket socket = null;
DataInputStream dataInputStream = null;

public Server1() {
try {
serverSocket = new ServerSocket(6002);
System.out.println("Server is Waiting for request...");
socket = serverSocket.accept();
System.out.println("Connected with: " + socket.getInetAddress());
dataInputStream = new DataInputStream(socket.getInputStream());
System.out.println("Server Read from client: " + dataInputStream.readUTF());
BufferedImage image = ImageIO.read(socket.getInputStream());
System.out.println("Server: Image received from client.");

} catch (Exception e) {
e.printStackTrace();
}
}

public static void main(String[] args) {
new Server1();
}
}

客户:

public class Client1 {

Socket clientSocket = null;
OutputStream outputStream = null;
DataOutputStream dataOutputStream = null;
InputStream inputStream = null;

public Client1() {
try {
clientSocket = new Socket("localhost", 6002);
outputStream = clientSocket.getOutputStream();
dataOutputStream = new DataOutputStream(outputStream);
inputStream = clientSocket.getInputStream();

dataOutputStream.writeUTF("Hello");
System.out.println("Client to server says: Hello");
BufferedImage image = ImageIO.read(new File("HelloNewPic.jpg"));
ImageIO.write(image, "JPG", clientSocket.getOutputStream());
System.out.println("Client: Image sent to server");

} catch (Exception e) {
e.printStackTrace();
}
}

public static void main(String[] args) {
new Client1();
}
}

在客户端,也不异常(exception):

Client to server says: Hello
Client: Image sent to server

但服务器端出现异常:

Server is Waiting for request...
Connected with: /127.0.0.1
Server Read from client: Hello
java.net.SocketException: Connection reset
at java.net.SocketInputStream.read(SocketInputStream.java:189)
at java.net.SocketInputStream.read(SocketInputStream.java:121)
at javax.imageio.stream.FileCacheImageInputStream.readUntil(FileCacheImageInputStream.java:141)
at javax.imageio.stream.FileCacheImageInputStream.read(FileCacheImageInputStream.java:187)
at com.sun.imageio.plugins.jpeg.JPEGImageReader.readImage(Native Method)
at com.sun.imageio.plugins.jpeg.JPEGImageReader.readInternal(JPEGImageReader.java:1158)
at com.sun.imageio.plugins.jpeg.JPEGImageReader.read(JPEGImageReader.java:966)
at javax.imageio.ImageIO.read(ImageIO.java:1448)
at javax.imageio.ImageIO.read(ImageIO.java:1352)
at networking.Server1.<init>(Server1.java:23)
at networking.Server1.main(Server1.java:32)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:601)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:120)

最佳答案

  1. 您的主要问题是客户端永远不会关闭连接,它只是退出,这会导致某些操作系统执行中止关闭,从而在接收器处产生“连接重置”。

  2. 您不需要仅使用 ImageIO 来发送图片。只需读取和写入字节即可。使用 ImageIO 只会浪费大量时间和空间。服务器应该能够使用 ImageIO 作为图像读取字节,无论 ImageIO 发送它们还是直接来自 JPEG 文件。

关于java - Java中通过Socket编程传输图像文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22454354/

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