代码之家  ›  专栏  ›  技术社区  ›  Chris Thompson

将视频流传输到服务器

  •  2
  • Chris Thompson  · 技术社区  · 16 年前

    我试图使用Java将视频从另一台机器传输到服务器,对于初学者,我只是尝试使用原始套接字连接和字节流传输文件。然而,事情并非如此。如果我以这种方式传输文件,那么我机器上的3MB文件在服务器上就变成5MB。我试着用一个视频文件来做这个,结果文件在我下载的时候实际上是“播放”的,长度正确,但是没有图像。代码如下:

    public static void main(String[] args){
        Socket sock = null;
    
        try{
            System.out.println("Connecting...");
            sock = new Socket("server.com", 8080);
            InputStream is = new FileInputStream(new File("Bear.wmv"));
            byte[] bytes = new byte[1024];
    
            OutputStream stream = sock.getOutputStream();
    
            int count = is.read(bytes, 0, 1024);
            while (count != -1){
                stream.write(bytes, 0, 1024);
                count = is.read(bytes, 0, 1024);
            }
    
            is.close();
            stream.close();
            sock.close();
    
        }catch (Exception e){
            e.printStackTrace();
        }
    }
    

    服务器(接收器):

    public static void main(String[] args){
        ServerSocket sock = null;
        try {
            sock = new ServerSocket(8080);
        } catch (IOException e) {
            System.out.println("Could not instantiate socket:");
            e.printStackTrace();
            return;
        }
    
        Socket clientSock = null;
        while(true){
    
            try{
    
                System.out.println("Waiting for connection...");
                clientSock = sock.accept();
                final Socket fin = clientSock;
                System.out.println("Connection accepted");
                System.out.println("Spawning thread...");
                Thread trd = new Thread(new Runnable(){
                    public void run(){
                        try {
                            try {
                                Thread.sleep(5000);
                            } catch (InterruptedException e) {
                                e.printStackTrace();
                            }
    
                            System.out.println("Receiving video...");
                            File video = new File("test.wmv");
                            FileOutputStream fos = new FileOutputStream(video);
                            byte[] data = new byte[1024];
                            int count = fin.getInputStream().read(data, 0, 1024);
                            while (count != -1){
                                fos.write(data, 0, 1024);
                                count = fin.getInputStream().read(data, 0, 1024);
                            }
                            fos.close();
                            fin.close();
                            System.out.println("Done receiving");
                        } catch (IOException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        }}
                });
                trd.start();
    
            }catch(IOException e){
                System.out.println("Could not accept");
                e.printStackTrace();
            }
    
    
        }
    
    
    
    }
    

    有什么想法吗?提前谢谢!

    1 回复  |  直到 16 年前
        1
  •  4
  •   Chris Thompson    16 年前

    所以我解决了问题。

    我将服务器更改为只写入“计数”字节

    while (count != -1){
           fos.write(data, 0, count);
           count = fin.getInputStream().read(data, 0, 1024);
    }
    

    现在它起作用了:)

    谢谢