Day62.URL编程 -Java常用类、集合、IO#

    科技2022-08-16  114

    URL编程

    public class URLTest1 { public static void main(String[] args){ HttpURLConnection openConnection = null; InputStream is = null; BufferedOutputStream bos = null; try { URL url = new URL("https://www.bilibili.com/video/BV1Kb411W75N?p=629"); openConnection = (HttpURLConnection) url.openConnection(); openConnection.connect(); is = openConnection.getInputStream(); bos = new BufferedOutputStream(new FileOutputStream("c.mp4")); byte[] buffer = new byte[1024]; int len; while ((len = is.read(buffer)) != -1){ bos.write(buffer,0,len); } System.out.println("下载完成"); } catch (IOException e) { e.printStackTrace(); } finally { if (is != null){ try { is.close(); } catch (IOException e) { e.printStackTrace(); } } if (bos != null){ try { bos.close(); } catch (IOException e) { e.printStackTrace(); } } if (openConnection != null){ try { openConnection.disconnect(); } catch (Exception e) { e.printStackTrace(); } } } } }
    Processed: 0.009, SQL: 9