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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22454354/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 15:48:39  来源:igfitidea点击:

Transfer image file through Socket Programming in Java

javasockets

提问by Sunrise

In my code, client sent an image to server and server should be able to receive it and show the image name (HelloNewPic.jpgin here) and also save image in desktop.

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

Server:

服务器:

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();
}
}

Client:

客户:

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();
}
}

In client side, There is no exception:

在客户端,也不例外:

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

But exception occur on server side:

但服务器端发生异常:

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)

采纳答案by user207421

  1. Your main problem is that the client never closes the connection, it just exits, which causes some OS's to do an abortive close, which yields a 'connection reset' at the receiver.

  2. You don't need to use ImageIO just to send a picture. Just read and write the bytes. Using ImageIO just wastes time, a lot of it, and space. The server should be able to readthe bytes with ImageIO as an image, whether ImageIO sent them or they came direct from a JPEG file.

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

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