您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息

java如何下载视频到本地

2024/6/21 20:49:42发布20次查看
1.加载servlet容器
不能使用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如何下载视频到本地的详细内容。
该用户其它信息

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录 Product