How to download and save a file from Internet using Java?

前端 未结 21 3354
情深已故
情深已故 2020-11-21 05:06

There is an online file (such as http://www.example.com/information.asp) I need to grab and save to a directory. I know there are several methods for grabbing a

21条回答
  •  刺人心
    刺人心 (楼主)
    2020-11-21 05:31

    Below is the sample code to download movie from internet with java code:

    URL url = new 
    URL("http://103.66.178.220/ftp/HDD2/Hindi%20Movies/2018/Hichki%202018.mkv");
        BufferedInputStream bufferedInputStream = new  BufferedInputStream(url.openStream());
        FileOutputStream stream = new FileOutputStream("/home/sachin/Desktop/test.mkv");
    
    
        int count=0;
        byte[] b1 = new byte[100];
    
        while((count = bufferedInputStream.read(b1)) != -1) {
            System.out.println("b1:"+b1+">>"+count+ ">> KB downloaded:"+new File("/home/sachin/Desktop/test.mkv").length()/1024);
            stream.write(b1, 0, count);
        }
    

提交回复
热议问题