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;
            }
    
        }
  • 相关阅读:
    栅格系统
    JQuery
    week 4
    week 1
    js嵌套,BOM,DOM,内置对象,数组,自定义对象,正则表达式
    week 2
    case when的两种用法
    获取当前路径
    parse,tryparse区别
    parse ,tryparse 续
  • 原文地址:https://www.cnblogs.com/maowuyu-xb/p/14741680.html
Copyright © 2011-2022 走看看