Java 通过套接字发送 .jpg 或图像时连接重置

Java Connection Reset when sending .jpg or images over socket

我使用 java 程序通过套接字发送大型文本文档或其他文件没有问题,但是当我尝试发送 .jpg 文件或其他图像时,我得到 java.net.SocketException:连接重启。

通过套接字发送 229 KB 的文本文档时,我 运行 没有遇到任何问题,但是当我尝试发送 89 KB 的图像时,出现错误。我使用 while 循环来读取和写入文件。

这是有问题的服务器 class 的一部分(名为 EasyDataSend):

public EasyDataSend() throws IOException    
{
   port = 8080;
   server = new ServerSocket(port);

   socket = server.accept(); 

   dataOutputStream = new DataOutputStream(socket.getOutputStream());   
}

public void sendFile(String path) throws IOException
{       
   File file = new File(path);

   InputStream fileInputStream = new FileInputStream(file);
   OutputStream fileOutputStream = socket.getOutputStream();

   byte[] bytes = new byte[16 * 1024];

   int count;
   while ((count = fileInputStream.read(bytes)) > 0) 
   {
      fileOutputStream.write(bytes, 0, count);
   }        

   fileOutputStream.close();
   fileInputStream.close();   
   socket.close(); 
   server.close();
}

这是客户端的一部分 class(名称 EasyDataReceive):

public EasyDataReceive() throws UnknownHostException, IOException
{
   ip = "127.0.0.1";
   port = 8080; 
   socket = new Socket(ip,port);
}

public void receiveFile(String path) throws IOException, SocketException 
{
   File file = new File(path); 
   InputStream fileInputStream = socket.getInputStream();
   OutputStream fileOutputStream = new FileOutputStream(file);

   byte[] bytes = new byte[16*1024];

   int count;
   while ((count = fileInputStream.read(bytes)) > 0) 
   {
      fileOutputStream.write(bytes, 0, count);
   }

   fileOutputStream.close(); 
   fileInputStream.close();
   socket.close();        

}

这是我得到的错误:

Exception in thread "main" java.net.SocketException: Connection reset at java.net.SocketInputStream.read(Unknown Source) at java.net.SocketInputStream.read(Unknown Source) at java.net.SocketInputStream.read(Unknown Source) at EasyDataReceive.receiveFile(EasyDataReceive.java:111) at TesterClient.main(TesterClient.java:23)

此外,第 111 行实际上只是客户端中 while 循环的开始 class。我不想将整个 class 粘贴到 post 中。第 23 行只是我构建 EasyDataReceive 对象的测试 class 的一部分。

您的套接字应该在方法内部:receiveFile() 和 sendFile()。

例如,

public static void sendFile(String path) throws IOException {
        try {
            socket = new Socket("127.0.0.1", 8080);
            System.out.println("Connected");
            File file = new File(path);
            InputStream fileInputStream = new FileInputStream(file);
            OutputStream fileOutputStream = socket.getOutputStream();
            byte[] bytes = new byte[16 * 1024];
            int count;
            while ((count = fileInputStream.read(bytes)) > 0) {
                fileOutputStream.write(bytes, 0, count);
            }
            fileOutputStream.close();
            fileInputStream.close();
        } catch (UnknownHostException u) {
            System.out.println(u);
        } catch (IOException i) {
            System.out.println(i);
        } finally {
            try {
                 socket.close();
            } catch (IOException i) {
                System.out.println(i);
            }
        }
    }

或者您应该将套接字客户端传递给您的方法,即

private void receiveFile (Socket client) {
    ...
}