不能使用main方法直接调用
public static boolean httpdownload(string httpurl, string savefile) { // 1.下载网络文件 int byteread; url url; try { url = new url(httpurl); } catch (malformedurlexception e1) { e1.printstacktrace(); return false; } try { //2.获取链接 urlconnection conn = url.openconnection(); //3.输入流 inputstream instream = conn.getinputstream(); //3.写入文件 fileoutputstream fs = new fileoutputstream(savefile); byte[] buffer = new byte[1024]; while ((byteread = instream.read(buffer)) != -1) { fs.write(buffer, 0, byteread); } instream.close(); fs.close(); return true; } catch (filenotfoundexception e) { e.printstacktrace(); return false; } catch (ioexception e) { e.printstacktrace(); return false; } } @test public void httpdownload() { httpdownload(http://video.zhihuishu.com/zhs/ablecommons/demo/201806/dddee1c446314b84a26c74a8def3c3c7.mp4,e:\\test/22.mp4); }
2.添加common-io依赖
import org.apache.commons.io.fileutils; import java.io.file;import java.net.url; public class testdownloadfile { public static void main(string[] args) throws exception{ string urlstr = https://img2018.cnblogs.com/i-beta/1278703/201911/1278703-20191128121650595-812419505.png; url url = new url(urlstr); string tempfilename = e://a.png; file temp = new file(tempfilename); fileutils.copyurltofile(url, temp); }}
以上就是java如何下载视频到本地的详细内容。
