本文实例为大家分享了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(); } } } } }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持移动技术网。
如对本文有疑问, 点击进行留言回复!!
ScrollView和RecyclerView的滑动事件处理
配置JAVA环境+安装Android Studio全过程+踩坑记录
Android P Camera2当SD卡被拔出来自动切换到内部存储
android 多个edittext 判空监听 让Button动态是否可点击
Android开源项目滚轮选择器WheelPicker的基本用法总结
网友评论