java微信server录音下载到自己server
程序员文章站
2023-12-21 14:01:58
本文实例为大家分享了java微信server录音下载到自己server的具体代码,供大家参考,具体内容如下
/**
* @author why
*
*...
本文实例为大家分享了java微信server录音下载到自己server的具体代码,供大家参考,具体内容如下
/** * @author why * */ public class voicedownload { /** * * 依据文件id下载文件 * * * * @param mediaid * * 媒体id * * @throws exception */ public static inputstream getinputstream(string accesstoken, string mediaid) { inputstream is = null; string url = "http://file.api.weixin.qq.com/cgi-bin/media/get? access_token=" + accesstoken + "&media_id=" + mediaid; try { url urlget = new url(url); httpurlconnection http = (httpurlconnection) urlget .openconnection(); http.setrequestmethod("get"); // 必须是get方式请求 http.setrequestproperty("content-type", "application/x-www-form-urlencoded"); http.setdooutput(true); http.setdoinput(true); system.setproperty("sun.net.client.defaultconnecttimeout", "30000");// 连接超时30秒 system.setproperty("sun.net.client.defaultreadtimeout", "30000"); // 读取超时30秒 http.connect(); // 获取文件转化为byte流 is = http.getinputstream(); } catch (exception e) { e.printstacktrace(); } return is; } /** * * 获取下载图片信息(jpg) * * * * @param mediaid * * 文件的id * * @throws exception */ public static void saveimagetodisk(string accesstoken, string mediaid, string picname, string picpath) throws exception { inputstream inputstream = getinputstream(accesstoken, mediaid); byte[] data = new byte[10240]; int len = 0; fileoutputstream fileoutputstream = null; try { fileoutputstream = new fileoutputstream(picpath+picname+".amr"); while ((len = inputstream.read(data)) != -1) { fileoutputstream.write(data, 0, len); } } catch (ioexception e) { e.printstacktrace(); } finally { if (inputstream != null) { try { inputstream.close(); } catch (ioexception e) { e.printstacktrace(); } } if (fileoutputstream != null) { try { fileoutputstream.close(); } catch (ioexception e) { e.printstacktrace(); } } } } }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。