Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/454.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
在Javascript中使用XmlHttpRequest逐字节下载_Javascript - Fatal编程技术网

在Javascript中使用XmlHttpRequest逐字节下载

在Javascript中使用XmlHttpRequest逐字节下载,javascript,Javascript,我希望实现前端功能,使用java脚本下载一组视频。在下载的视频上有很多进程。目前我正在使用XmlHttpRequest,它只能在下载整个视频后开始处理字节。下面是示例代码。我已经接受了任何http问题声明请求。处理从“xmlhttp.readyState==4”块开始 我试图找出是否有任何方法可以将响应分块下载,并在下载时开始处理它。这类似于我们使用java.io.Inputstream包可以做的事情。请参阅下面的代码,该代码逐字节读取数据块大小为1字节的数据 try {

我希望实现前端功能,使用java脚本下载一组视频。在下载的视频上有很多进程。目前我正在使用XmlHttpRequest,它只能在下载整个视频后开始处理字节。下面是示例代码。我已经接受了任何http问题声明请求。处理从“xmlhttp.readyState==4”块开始

我试图找出是否有任何方法可以将响应分块下载,并在下载时开始处理它。这类似于我们使用java.io.Inputstream包可以做的事情。请参阅下面的代码,该代码逐字节读取数据块大小为1字节的数据

try {
            URL url = new URL("http://www.thomas-bayer.com/sqlrest/CUSTOMER/1");
            HttpURLConnection conn = (HttpURLConnection)url.openConnection();
            InputStream is = conn.getInputStream();
            byte buff[] = new byte[1];
            int readData = 0;
            StringBuilder response = new StringBuilder();
            while (true) {
                readData = is.read(buff);
                if (readData == -1)
                    break;
                response.append(new String(buff, 0, readData));
            }
            is.close();
            conn.disconnect();
            System.out.println(response);
        } catch (MalformedURLException e) {
            // handle exception
        } catch (IOException e) {
            // handle exception
        }

请建议。提前感谢您的帮助。

Websockets或者使用UDP/Javascript,但Javascript中的XmlHttpRequest无法做到这一点。但是,您可以将文件拆分为较小的文件并发出多个请求。
try {
            URL url = new URL("http://www.thomas-bayer.com/sqlrest/CUSTOMER/1");
            HttpURLConnection conn = (HttpURLConnection)url.openConnection();
            InputStream is = conn.getInputStream();
            byte buff[] = new byte[1];
            int readData = 0;
            StringBuilder response = new StringBuilder();
            while (true) {
                readData = is.read(buff);
                if (readData == -1)
                    break;
                response.append(new String(buff, 0, readData));
            }
            is.close();
            conn.disconnect();
            System.out.println(response);
        } catch (MalformedURLException e) {
            // handle exception
        } catch (IOException e) {
            // handle exception
        }