zoukankan      html  css  js  c++  java
  • https url 下载

    import java.security.cert.CertificateException;
    import java.security.cert.X509Certificate;
    
    import javax.net.ssl.X509TrustManager;
    
    /**
     * 信任证书管理器
     * @author Administrator
     *
     */
    public class TrustAnyTrustManager implements X509TrustManager{
    
        @Override
        public void checkClientTrusted(X509Certificate[] arg0, String arg1) throws CertificateException {
            
        }
    
        @Override
        public void checkServerTrusted(X509Certificate[] arg0, String arg1) throws CertificateException {
            
        }
    
        @Override
        public X509Certificate[] getAcceptedIssuers() {
            return new X509Certificate[] {};
        }
    
    }
    import javax.net.ssl.HostnameVerifier;
    import javax.net.ssl.SSLSession;
    
    public class TrustAnyHostnameVerifier implements HostnameVerifier {
    
        @Override
        public boolean verify(String arg0, SSLSession arg1) {
            return true;
        }
    
    }
    void downloadSound(String downloadPath) {
            try {
                if(downloadPath != null && downloadPath != "") {
                    TrustManager[] tm = { new TrustAnyTrustManager() };
                    SSLContext sc = SSLContext.getInstance("SSL", "SunJSSE");
                    sc.init(null, tm, new java.security.SecureRandom());
                    
                    String filename = downloadPath.substring(downloadPath.lastIndexOf("/") + 1);
                    URL url = new URL(downloadPath);
                    HttpsURLConnection conn = (HttpsURLConnection)url.openConnection();
                    conn.setSSLSocketFactory(sc.getSocketFactory());
                    conn.setHostnameVerifier(new TrustAnyHostnameVerifier());
                    conn.setDoOutput(true);
                    conn.setDoInput(true);
                    conn.connect();
                    
                    InputStream in = conn.getInputStream();
                    BufferedInputStream bis = new BufferedInputStream(in);
                    BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(new File("D:" + File.separator + filename)));
                    byte[] buffer = new byte[1024];
                    int read;
                    while((read = bis.read(buffer)) != -1 ) {
                        bos.write(buffer, 0, read);
                    }
                    bos.close();
                    bis.close();
                }
                
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

     加一个需求:将从url读取到的.wav转换成mp3文件

    //创建临时文件
    File temp = File.createTempFile(filename, ".wav");
    BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(temp));

    byte[] buffer = new byte[1024];
    int read;
    while((read = bis.read(buffer)) != -1 ) {
    bos.write(buffer, 0, read);  //将读取到的wav音频文件写入临时文件
    }
    AudioUtils.transform(temp, "D:" + File.separator + destName + ".mp3");
    bos.close();
    bis.close();
    temp.deleteOnExit();//程序退出删除临时文件

        public static File transform(File source , String destFileName) throws Exception {
            File target = new File(destFileName);
            
            AudioAttributes audio = new AudioAttributes(); 
            audio.setCodec("libmp3lame");  
            audio.setBitRate(new Integer(36000));        //
            audio.setChannels(new Integer(2));          //1 mono 单声道    2 stereo 立体声
            audio.setSamplingRate(new Integer(44100));  //设置编码进程的采样率值
            
            EncodingAttributes attrs = new EncodingAttributes();  
            attrs.setFormat("mp3");  
            attrs.setAudioAttributes(audio);  
            
            Encoder encoder = new Encoder();  
            encoder.encode(source, target, attrs);  
        
            return target;
        }
    温故而知新
  • 相关阅读:
    洛谷 P4071 [SDOI2016]排列计数
    问题 G: 【一本通提高同余问题】计算器
    问题 A: 【一本通提高组合数学】Bullcow 牡牛和牝牛
    浅谈卢卡斯定理(非扩展)
    2019西安联训B层 Day 6练习题 问题 C: 扩展欧几里得
    react使用lazy()和Suspense实现根据路由进行代码分割
    react-loadable 使用高阶组件动态import组件,实现代码分割(code-splitting)
    react angular vue流行度对比
    react 服务端渲染(ssr) 框架 Next.js
    超级字符串内class正则匹配替换 可以用于css modules
  • 原文地址:https://www.cnblogs.com/Uzai/p/10273321.html
Copyright © 2011-2022 走看看