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;
    }

 

posted @ 2019-01-15 17:51  new_boys  阅读(1527)  评论(0编辑  收藏  举报