在Javascript中使用XmlHttpRequest按字节下载字节

时间:2015-09-11 04:30:16

标签: javascript

我希望使用java脚本实现前端功能以下载视频集。在下载的视频上运行了大量进程。目前我正在使用XmlHttpRequest,它只能在下载整个视频后开始处理字节。下面是示例代码。我已经对问题陈述采取了任何http请求。处理开始于" xmlhttp.readyState == 4"块。

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
        }

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

python /path/to/file/to/run.py

请建议。感谢您的帮助。

0 个答案:

没有答案