不讲废话上代码
1 /** 2 * 上传语音消息至sae服务器 3 * 4 * @param requestUrl 上传链接 5 * @param requestMethod http请求方式 6 */ 7 8 public static boolean httpRequestUploadVoice(String requestUrl, String requestMethod, String fileName){ 9 int byteread = 0; 10 boolean isUpload = false; 11 try { 12 URL url = new URL(requestUrl); 13 HttpURLConnection httpUrlConn = (HttpURLConnection) url.openConnection(); 14 15 httpUrlConn.setDoOutput(true); 16 httpUrlConn.setDoInput(true); 17 httpUrlConn.setUseCaches(false); 18 // 设置请求方式(GET/POST) 19 httpUrlConn.setRequestMethod(requestMethod); 20 21 if ("GET".equalsIgnoreCase(requestMethod)) 22 httpUrlConn.connect(); 23 24 25 26 27 InputStream inStream = httpUrlConn.getInputStream(); 28 ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); 29 byte[] buffer = new byte[1204]; 30 int length; 31 while ((byteread = inStream.read(buffer)) != -1) { 32 outputStream.write(buffer, 0, byteread); 33 } 34 //sae 不支持追加 所以就只能放到一个 字节数组里 35 byte[] res = outputStream.toByteArray(); 36 //上传到 sae 37 SaeStorage st = new SaeStorage(); 38 //三个参数 domain - 存储域名 fileName - 文件名 content - 文件内容 39 isUpload = st.write("test",fileName, res); 40 41 inStream.close(); 42 inStream = null; 43 httpUrlConn.disconnect(); 44 45 } catch (FileNotFoundException e) { 46 e.printStackTrace(); 47 } catch (IOException e) { 48 e.printStackTrace(); 49 } 50 51 return isUpload; 52 53 }