Java原生下载文件

java 文章 2022-03-03 16:16 445 0 全屏看文

AI助手支持GPT4.0

public static void main(String[] args) throws MalformedURLException, IOException {
		InputStream is = new URL("https://xxxx.com/upgcxcode/01/70/373597001/373597001-1-208.mp4").openStream();
		//创建一个文件输出流
        FileOutputStream fos = new FileOutputStream("D:\\test\\test.mp4");
        //创建一个缓冲区
        byte buffer[] = new byte[1024];
        //判断输入流中的数据是否已经读完的标识
        int length = 0;
        //循环将输入流读入到缓冲区当中,(len=in.read(buffer))>0就表示in里面还有数据
        while((length = is.read(buffer))>0){
            //使用FileOutputStream输出流将缓冲区的数据写入到指定的目录(savePath + "\\" + filename)当中
            fos.write(buffer, 0, length);
        }

}


-EOF-

AI助手支持GPT4.0