通过链接下载音频文件到本地

  • Post author:
  • Post category:其他



    /**
     * TODO 下载文件到本地 ;
     *
     * @param fileUrl   远程地址 ;
     * @param fileLocal 本地路径 ;
     * @throws Exception ;
     * @author nadim  ;
     * @date Sep 11, 2015 11:45:31 AM ;
     */

    public static void downloadFile(String fileUrl, String fileLocal) throws Exception {
        URL url = new URL(fileUrl);

        HttpURLConnection urlCon = (HttpURLConnection) url.openConnection();
        urlCon.setConnectTimeout(6000);
        urlCon.setReadTimeout(6000);
        int code = urlCon.getResponseCode();
        if (code != HttpURLConnection.HTTP_OK) {
            throw new Exception("文件读取失败");
        }

        //读文件流;

        DataInputStream in = new DataInputStream(urlCon.getInputStream());

        DataOutputStream out = new DataOutputStream(new FileOutputStream(fileLocal));

        byte[] buffer = new byte[2048];

        int count = 0;

        while ((count = in.read(buffer)) > 0) {
            out.write(buffer, 0, count);
        }

        out.close();
        in.close();
    }



版权声明:本文为uknowzxt原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。