Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/341.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/codeigniter/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用java将映像从客户端移动到服务器_Java_Image Upload - Fatal编程技术网

使用java将映像从客户端移动到服务器

使用java将映像从客户端移动到服务器,java,image-upload,Java,Image Upload,我正在创建一个桌面客户机-服务器应用程序,在该应用程序中,我捕获由渲染器渲染的jpg图像中的帧,并将其存储在客户端。 现在我需要上传图像到服务器上。 我试过这个 为每个捕获的图像放置一个单独的线程以将其直接上载到服务器,但这非常耗时。此外,我还尝试在停止捕获后从客户端上传所有图像,但这不是我想要的情况 所以,有没有一种方法可以有效地将直接捕获的图像上传到服务器 对于捕获图像,我使用BuffereImage和ImageIO.write方法 提前感谢通过套接字上传图像是在服务器上上传图像的最快方式,

我正在创建一个桌面客户机-服务器应用程序,在该应用程序中,我捕获由渲染器渲染的jpg图像中的帧,并将其存储在客户端。 现在我需要上传图像到服务器上。 我试过这个 为每个捕获的图像放置一个单独的线程以将其直接上载到服务器,但这非常耗时。此外,我还尝试在停止捕获后从客户端上传所有图像,但这不是我想要的情况

所以,有没有一种方法可以有效地将直接捕获的图像上传到服务器

对于捕获图像,我使用BuffereImage和ImageIO.write方法


提前感谢

通过套接字上传图像是在服务器上上传图像的最快方式,因为数据将以字节流的形式传递给服务器

下面是简单的Socket客户端和Socket服务器来实现图像上传

客户端

 public class ImageUploadSocketClient {
   public static void main(String[] args) throws Exception {
    Socket socket = new Socket("localhost",6666);
    OutputStream outputStream = socket.getOutputStream();
    BufferedImage image = ImageIO.read(new File("path to image /your_image.jpg"));
    ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
    ImageIO.write(image, "jpg", byteArrayOutputStream);
    byte[] size = ByteBuffer.allocate(4).putInt(byteArrayOutputStream.size()).array();
    outputStream.write(size);
    outputStream.write(byteArrayOutputStream.toByteArray());
    outputStream.flush();
    socket.close();
  }
}
服务器

public class ImageUploadSocketRunnable implements Runnable{       
    public static final String dir="path to store image";
    Socket soc=null;
   ImageUploadSocketRunnable(Socket soc){
     this.soc=soc;
   }
    @Override
    public void run() {
    InputStream inputStream = null;
       try {
           inputStream = this.soc.getInputStream();
           System.out.println("Reading: " + System.currentTimeMillis());
           byte[] sizeAr = new byte[4];
           inputStream.read(sizeAr);
           int size = ByteBuffer.wrap(sizeAr).asIntBuffer().get();
           byte[] imageAr = new byte[size];
           inputStream.read(imageAr);
           BufferedImage image = ImageIO.read(new ByteArrayInputStream(imageAr));
           System.out.println("Received " + image.getHeight() + "x" + image.getWidth() + ": " + System.currentTimeMillis());
           ImageIO.write(image, "jpg", new File(dir+System.currentTimeMillis()+".jpg"));
           inputStream.close();
       } catch (IOException ex) {
           Logger.getLogger(ImageUploadSocketRunnable.class.getName()).log(Level.SEVERE, null, ex);
       }

    }

    public static void main(String[] args) throws Exception {
        ServerSocket serverSocket = new ServerSocket(13085);
        while(true){
        Socket socket = serverSocket.accept();
        ImageUploadSocketRunnable imgUploadServer=new ImageUploadSocketRunnable(socket);
        Thread thread=new Thread(imgUploadServer);
        thread.start();
        }
    }

}
在服务器上,您应该为不同的客户端套接字创建不同的线程,这样您就可以从不同的客户端实现并发图像上载


希望上面的示例能对您有所帮助。

您如何将每个帧[图像]从客户端上载到服务器,我的意思是使用哪种协议?您如何将图像发送到服务器?你能发布代码吗?我对发送部分感兴趣。是套接字、servlet还是其他什么东西?如果您正在发送,您可以微调缓冲区大小(8192或一些大的数字),并按照以下帖子发送:好的,谢谢@ravindra,我会尝试socket@dev我使用http协议上传ImagesHanks dev,这有助于我上传单个图像,但正如我提到的,我要上传多个图像,我面临的连接重置问题是的,我上传它在单线程。实际上我想在一个连接中发送多个图像文件,因为为每个图像创建连接是不可行的。