对象输入流仅通过网络获取一个文件?

发布于 2024-10-05 15:14:03 字数 4036 浏览 4 评论 0原文

我创建了一个基本的客户端服务器,它将通过网络发送指定目录中的图像文件。该代码上周有效,但今天我回到它,似乎我只在服务器端获得一个文件,即使客户端打印出它已发送目录中的所有图像文件。 它可能是客户端代码中的某些内容,但我认为它是服务器端的某些内容。 非常感谢任何帮助,如果您有更有效的解决方案,我很乐意根据需要更改我的代码。我的代码如下:

ImageServer

        package com.encima.network.server;

        import java.io.*;
        import java.net.*;

        public class ImageServer{

     ServerSocket ss;
     Socket s;
     ObjectOutputStream oos;
     int port = 4440;

     public ImageServer() throws IOException {
      try {
       ss  = new ServerSocket(port);
       System.out.println("Server started on Port: " + port);
      } catch(IOException e) {
       System.out.println("Serevr: Port-" + port  + " not available, exiting.");
       System.exit(0);
      }

      System.out.println("Server: Waiting for Client Connection...");

      while(true) {
       try {
        s = ss.accept();
        new ImageHandler(s);
       } catch (IOException e) {
        e.printStackTrace();
       }
      }
     }

     public static void main(String[] args) throws IOException {
      ImageServer is = new ImageServer();
     }

        }

ImageHandler

    package com.encima.network.server;

    import java.awt.image.BufferedImage;
    import java.io.FileOutputStream;
    import java.io.ObjectInputStream;
    import java.net.Socket;

    import javax.imageio.ImageIO;

    public class ImageHandler implements Runnable {

     Socket s;
     int count = 0;

     public ImageHandler(Socket socket) {
      s = socket;
      Thread t = new Thread(this);
      t.start();
     }

     @Override
     public void run() {

      try {
       ObjectInputStream ois = new ObjectInputStream(s.getInputStream());
       FileOutputStream fos = new FileOutputStream("image" + System.nanoTime() + ".jpg");
       count++;
       //BufferedImage in = ImageIO.read(ois);
       //ImageIO.write(in, "jpg", fos);

       int ch = 0;
        while(true) {
         ch = ois.read();
          if(ch == -1) {
           break;
          }
         fos.write(ch);
        }   
       fos.flush();
      } catch (Exception e) {
       e.printStackTrace();
      }
     }
    }



Finally, the ImageClient

    package com.encima.network.client;

    import java.awt.image.BufferedImage;
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.IOException;
    import java.io.ObjectOutputStream;
    import java.net.Socket;

    import javax.imageio.ImageIO;

    import com.encima.network.ImageFilter;


    public class ImageClient {

     Socket s;
     String ip = "localhost";
     int port = 4440;
     ObjectOutputStream oos;

     public ImageClient(File[] files) throws IOException, ClassNotFoundException, InterruptedException {

      try {
       s = new Socket(ip, port);
       System.out.println("Client connected to Server via " + ip + " on port 80");
      } catch (Exception e) {
       System.out.println("Client: Cannot find Host: " + ip + ". Exiting.");
       System.exit(0);
      }

      oos = new ObjectOutputStream(s.getOutputStream());

      for(File f: files) {
       sendFile(f);
      }
      oos.close();
       //System.out.println("Written Image " + i + " of " + files.length);
     }

     public static void main(String[] args) throws IOException, ClassNotFoundException, InterruptedException {
      File dir = new File("/Users/christophergwilliams/Dropbox/PhD/Projects/PhD/Year 1/GSN/images");
      File[] files = dir.listFiles(new ImageFilter());
      ImageClient ic = new ImageClient(files);
     }

     public void sendFile(File file) throws IOException {
      FileInputStream fis = new FileInputStream(file);
      //BufferedImage b = ImageIO.read(file);
      //ImageIO.write(b, "jpg", oos);
      int ch = 0;
       while(true) {
        ch = fis.read();
        if(ch == -1) {
         break;
        }
        oos.write(ch);
       }
      oos.flush();
      System.out.println("Image Sent");

     }


}

我知道需要阅读大量代码,但我非常感谢我能在这方面获得的任何帮助!

我可能是错的,但是为了效率和网络流量,将图像作为 zip 从客户端发送到服务器是否有益?

I have created a basic Client Server that will send image files in a specified directory over a network. The code worked last week but I came back to it today and it seems that I am only getting one file on the server side, even though the client prints out that it has sent all the image files in the directory.
It may be something in the client code but I think it is something on the server side.
Any help is greatly appreciated and if you have a more efficient solution, I am happy to change my code as necessary. My code is below:

ImageServer

        package com.encima.network.server;

        import java.io.*;
        import java.net.*;

        public class ImageServer{

     ServerSocket ss;
     Socket s;
     ObjectOutputStream oos;
     int port = 4440;

     public ImageServer() throws IOException {
      try {
       ss  = new ServerSocket(port);
       System.out.println("Server started on Port: " + port);
      } catch(IOException e) {
       System.out.println("Serevr: Port-" + port  + " not available, exiting.");
       System.exit(0);
      }

      System.out.println("Server: Waiting for Client Connection...");

      while(true) {
       try {
        s = ss.accept();
        new ImageHandler(s);
       } catch (IOException e) {
        e.printStackTrace();
       }
      }
     }

     public static void main(String[] args) throws IOException {
      ImageServer is = new ImageServer();
     }

        }

ImageHandler

    package com.encima.network.server;

    import java.awt.image.BufferedImage;
    import java.io.FileOutputStream;
    import java.io.ObjectInputStream;
    import java.net.Socket;

    import javax.imageio.ImageIO;

    public class ImageHandler implements Runnable {

     Socket s;
     int count = 0;

     public ImageHandler(Socket socket) {
      s = socket;
      Thread t = new Thread(this);
      t.start();
     }

     @Override
     public void run() {

      try {
       ObjectInputStream ois = new ObjectInputStream(s.getInputStream());
       FileOutputStream fos = new FileOutputStream("image" + System.nanoTime() + ".jpg");
       count++;
       //BufferedImage in = ImageIO.read(ois);
       //ImageIO.write(in, "jpg", fos);

       int ch = 0;
        while(true) {
         ch = ois.read();
          if(ch == -1) {
           break;
          }
         fos.write(ch);
        }   
       fos.flush();
      } catch (Exception e) {
       e.printStackTrace();
      }
     }
    }



Finally, the ImageClient

    package com.encima.network.client;

    import java.awt.image.BufferedImage;
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.IOException;
    import java.io.ObjectOutputStream;
    import java.net.Socket;

    import javax.imageio.ImageIO;

    import com.encima.network.ImageFilter;


    public class ImageClient {

     Socket s;
     String ip = "localhost";
     int port = 4440;
     ObjectOutputStream oos;

     public ImageClient(File[] files) throws IOException, ClassNotFoundException, InterruptedException {

      try {
       s = new Socket(ip, port);
       System.out.println("Client connected to Server via " + ip + " on port 80");
      } catch (Exception e) {
       System.out.println("Client: Cannot find Host: " + ip + ". Exiting.");
       System.exit(0);
      }

      oos = new ObjectOutputStream(s.getOutputStream());

      for(File f: files) {
       sendFile(f);
      }
      oos.close();
       //System.out.println("Written Image " + i + " of " + files.length);
     }

     public static void main(String[] args) throws IOException, ClassNotFoundException, InterruptedException {
      File dir = new File("/Users/christophergwilliams/Dropbox/PhD/Projects/PhD/Year 1/GSN/images");
      File[] files = dir.listFiles(new ImageFilter());
      ImageClient ic = new ImageClient(files);
     }

     public void sendFile(File file) throws IOException {
      FileInputStream fis = new FileInputStream(file);
      //BufferedImage b = ImageIO.read(file);
      //ImageIO.write(b, "jpg", oos);
      int ch = 0;
       while(true) {
        ch = fis.read();
        if(ch == -1) {
         break;
        }
        oos.write(ch);
       }
      oos.flush();
      System.out.println("Image Sent");

     }


}

I am aware that it is a lot of code to read through but I do appreciate any help I can get on this!

I may be wrong but, for the sake of efficiency and network traffic, would it be beneficial to send the images as a zip from the client to the server?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

寻找一个思念的角度 2024-10-12 15:14:03

你为什么要使用ObjectInputStream?您没有读取或写入任何序列化对象 - 只是原始二进制数据。使用提供的任何InputStream,并从中读取。

无论如何,这并不是什么大问题。最大的问题是您只是将多个文件写入一个流,而没有指示一个文件应该在哪里完成以及下一个文件应该从哪里开始。您希望如何拆分多个文件?选项:

  • 在文件之间使用分隔符(非常难看 - 您可能必须转义任何看起来像分隔符的数据)
  • 为每个文件添加其长度前缀
  • 在不同的连接上发送每个文件

(您还在阅读和一次写入一个字节。使用接受字节数组的读/写重载。)

Why are you using ObjectInputStream at all? You're not reading or writing any serialized objects - just raw binary data. Use whatever InputStream is provided, and read from that.

Anyway, that's not the big problem. The big problem is that you're just writing several files to one stream, with no indication of where one file is meant to finish and the next one is meant to start. How were you expecting to split the multiple files up? Options:

  • Use a delimiter between files (very ugly - you'd have to potentially escape any data which looked like the delimiter as you went along)
  • Prefix each file with its length
  • Send each file on a different connection

(You're also reading and writing a single byte at a time. Use the overloads of read/write which accept byte arrays.)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文