zoukankan      html  css  js  c++  java
  • java实现 视频下载功能

    参考:

    https://www.cnblogs.com/IT-study/p/9239717.html

    类似的实现方式:

     public static boolean downRecord(String urlString,String filename,int timeout) {
            boolean ret = false;
            File file = new File(filename);
            try {
                if(file.exists()){
                    ret = true;
                }else{
                    System.out.println("文件下载操作");
                    // 构造URL
                    URL url = new URL(urlString);
                    // 打开连接
                    HttpURLConnection con = (HttpURLConnection )url.openConnection();
                    con.setConnectTimeout(timeout);
                    con.setReadTimeout(timeout);
                    con.connect();
                    int contentLength = con.getContentLength();
                    System.out.println("打印文件的长度");
                    System.out.println(contentLength);
                    // 输入流
                    InputStream is = con.getInputStream();
                    // 1K的数据缓冲
                    byte[] bs = new byte[1024];
                    // 读取到的数据长度
                    int len;
                    // 输出的文件流
    
                    File file2=new File(file.getParent());
                    file2.mkdirs();
                    if(file.isDirectory()){
    
                    }else{
                        file.createNewFile();//创建文件
                    }
                    OutputStream os = new FileOutputStream(file);
                    // 开始读取
                    while ((len = is.read(bs)) != -1) {
                        os.write(bs, 0, len);
                    }
                    // 完毕,关闭所有链接
                    os.close();
                    is.close();
                    if(contentLength != file.length()){
                        file.delete();
                        ret = false;
                    }else{
                        ret = true;
                    }
                }
            } catch (IOException e) {
                file.delete();
                ret = false;
            }finally {
                return ret;
            }
    
        }
  • 相关阅读:
    磁盘管理
    TCP/IP四层模型
    OSI七层模型详解
    kvm虚拟机
    mount 文件挂载
    ORA-01017: 用户名/口令无效; 登录被拒绝
    mybatis配置文件形式
    Spring+mybatis整合
    xmlBean学习二
    xmlBean学习一
  • 原文地址:https://www.cnblogs.com/maowuyu-xb/p/14741680.html
Copyright © 2011-2022 走看看