更新时间:2022-09-12 13:40:35
这个片段的url:
那么这个片段一共有多少个片段呢?在所有片段开始下载之前,有这样一个请求:chunklist即是视频片段的清单。
private void download(){ URL task = null; String path = DownloadLauncher.LOCALPATH + this.mIndex + DownloadLauncher.POSTFIX; String url = this.mTask; try { task = new URL(url); DataInputStream dataInputStream = new DataInputStream(task.openStream()); FileOutputStream fileOutputStream = new FileOutputStream(new File(path)); ByteArrayOutputStream output = new ByteArrayOutputStream(); byte[] buffer = new byte[1024]; int length; while ((length = dataInputStream.read(buffer)) > 0) { output.write(buffer, 0, length); } fileOutputStream.write(output.toByteArray()); dataInputStream.close(); fileOutputStream.close(); System.out.println("File: " + this.mIndex + " downloaded ok"); } catch (MalformedURLException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } }
private static void run() throws IOException{ FileInputStream in = null; String destFile = DownloadLauncher.LOCALPATH + DownloadLauncher.MERGED; FileOutputStream out = new FileOutputStream(destFile,true); for( int i = 0; i <= DownloadLauncher.LAST; i++){ byte[] buf = new byte[1024]; int len = 0; String sourceFile = DownloadLauncher.LOCALPATH + i + DownloadLauncher.POSTFIX; in = new FileInputStream(sourceFile); while( (len = in.read(buf)) != -1 ){ out.write(buf,0,len); } } out.close(); } public static void main(String[] args) { try { run(); } catch (IOException e) { e.printStackTrace(); } System.out.println("Merged ok!"); }
完整的代码在我的github上:
https://github.com/i042416/JavaTwoPlusTwoEquals5/tree/master/src/flick