录音自动上传到服务器,怎么实现手机录音之后,将录音的音频文件上传到服务器上...

你先得确定服务器用什么协议啊,HTTP,webservice,socket等等,如果用http一般两种方式,一个是java自带的urlhttpconnection,还有就是阿帕奇的httpclient。

代码片段

// 使用POST方法提交数据,必须大写

conn.setRequestMethod(POST);

// 需要输出流

conn.setDoOutput(true);

// 需要输入流

conn.setDoInput(true);

// 连接超时,10秒

conn.setConnectTimeout(10 * 1000);

// 读取超时,10秒

conn.setReadTimeout(10 * 1000);

// 打开输出流,写入数据

out = conn.getOutputStream();

out.write(data);

out.flush();

// 以上

conn.connect();

if (conn.getResponseCode() == 200) {

in = conn.getInputStream();

// TODO 读取数据

// 参考

int contentLength = conn.getContentLength();

ByteArrayOutputStream buf = new ByteArrayOutputStream(

contentLength 0 ? contentLength : 1024);

byte[] buffer = new byte[1024];

while ((contentLength = in.read(buffer)) != -1) {

buf.write(buffer, 0, contentLength);

}

// 可选

buf.flush();

return buf.toByteArray();

}

} catch (MalformedURLException e) {

e.printStackTrace();

} catch (IOException e) {

e.printStackTrace();

} finally {

if (out != null) {

try {

out.close();

} catch (IOException e) {

e.printStackTrace();

}

}

if (in != null) {

try {

in.close();

} catch (IOException e) {

e.printStackTrace();

}

}

if (conn != null) {

conn.disconnect();

}

// 错误的写法

// try {

// in.close();

// out.close();

// } catch (IOException e) {

// // TODO Auto-generated catch block

// e.printStackTrace();

// }

}

//尽量不要返回null 避免空指针异常

return new byte[0];

}

服务器在getpost里面接收可以转为btye数组,然后在转为文件

取消

评论

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值