首先看下服务器端,服务器端使用martini框架,仅建立一个简单的接收客户端post请求并保存客户端传过来的语音的后台服务:

packagemain                        //http://liuxp0827.blog.51cto.com/5013343/1412977                                                                                                               import(    "bufio"    "github.com/go-martini/martini"    "io/ioutil"    "log"    "net/http"    "os")                                                                                                                                                                                                  func main() {    m := martini.Classic()                                                                                                                                                                                                      m.Post("/wave", func(req *http.Request) {        file, _, err := req.FormFile("file")        iferr != nil {            log.Fatal("FormFile: ", err.Error())            os.Exit(2)        }        defer func() {            iferr := file.Close(); err != nil {                log.Fatal("Close: ", err.Error())                os.Exit(2)            }        }()                                                                                                                                                                                                          localFile, _ := os.Create("1.wav")        defer localFile.Close()        writer := bufio.NewWriter(localFile)        bytes, err := ioutil.ReadAll(file)        iferr != nil {            log.Fatal("ReadAll: ", err.Error())            os.Exit(2)        }                                                                                                                                                                                                          writer.Write(bytes)        writer.Flush()    })                                                                                                                                                                                                      http.ListenAndServe(":8080", m)}

   再来看下客户端的java代码,首先调用readWavform函数从本地读取语音文件到byte[],然后设置相应的POST头信息,最终发送数据:

importjava.io.ByteArrayOutputStream;importjava.io.File;importjava.io.FileInputStream;importjava.io.FileNotFoundException;importjava.io.IOException;importjava.io.InputStream;importjava.io.OutputStream;importjava.net.HttpURLConnection;importjava.net.URL;importjava.util.HashMap;importjava.util.Iterator;importjava.util.Map;                                                                                                             //http://liuxp0827.blog.51cto.com/5013343/1412977                                                              publicfinalclassupload {                                                                                                                                      publicstaticvoidmain(String[] args) {        Map parameters = newHashMap();                                                                        byte[] data = readWavform("C:\\Users\\PONPON\\Desktop\\test.wav");        doUploadFile("http://localhost:8080/wave", parameters,                Constants.FILEPARAM, "11.wav", "multipart/form-data;",                data);    }                                                                                                                                      publicstaticbyte[] readWavform(String filename) {                                                                                                                                          intregLen = 0;        byte[] regbuffer = null;        try{            FileInputStream inputsteam = newFileInputStream(newFile(filename));                                                                                                                                              regLen = inputsteam.available();            regbuffer = newbyte[regLen];            if((regLen = inputsteam.read(regbuffer, 0, regLen)) < 0) {                System.out.println("error when read pcm file.");            }                                                                                                                                          } catch(FileNotFoundException e) {            // TODO Auto-generated catch block            e.printStackTrace();        } catch(IOException e) {            // TODO Auto-generated catch block            e.printStackTrace();        }                                                                                                                                          returnregbuffer;    }                                                                                                                                      publicstaticString doUploadFile(String reqUrl,            Map parameters, String fileParamName,            String filename, String contentType, byte[] data) {        HttpURLConnection urlConn = null;        try{            urlConn = sendFormdata(reqUrl, parameters, fileParamName, filename,                    contentType, data);            String responseContent = newString(getBytes(urlConn));            returnresponseContent.trim();        } catch(Exception e) {            thrownewRuntimeException(e.getMessage(), e);        } finally{            if(urlConn != null) {                urlConn.disconnect();            }        }    }                                                                                                                                      privatestaticHttpURLConnection sendFormdata(String reqUrl,            Map parameters, String fileParamName,            String filename, String contentType, byte[] data) {        HttpURLConnection urlConn = null;        try{            URL url = newURL(reqUrl);            urlConn = (HttpURLConnection) url.openConnection();            urlConn.setRequestMethod("POST");            urlConn.setConnectTimeout(10000);// (单位:毫秒)jdk            urlConn.setReadTimeout(10000);// (单位:毫秒)jdk 1.5换成这个,读操作超时            urlConn.setDoOutput(true);            urlConn.setRequestProperty("connection", "keep-alive");                                                                                                                                              String boundary = "-----------------------------114975832116442893661388290519"; // 分隔符            urlConn.setRequestProperty("Content-Type",                    "multipart/form-data; boundary="+ boundary);                                                                                                                                              boundary = "--"+ boundary;            StringBuffer params = newStringBuffer();            if(parameters != null) {                for(Iterator iter = parameters.keySet().iterator(); iter                        .hasNext();) {                    String name = iter.next();                    String value = parameters.get(name);                    params.append(boundary + "\r\n");                    params.append("Content-Disposition: form-data; name=\""                            + name + "\"\r\n\r\n");                    params.append(value);                    params.append("\r\n");                }            }            StringBuilder sb = newStringBuilder();            sb.append(boundary);            sb.append("\r\n");            sb.append("Content-Disposition: form-data; name=\""+ fileParamName                    + "\"; filename=\""+ filename + "\"\r\n");            sb.append("Content-Type: "+ contentType + "\r\n\r\n");            byte[] fileDiv = sb.toString().getBytes("UTF-8");            byte[] endData = ("\r\n"+ boundary + "--\r\n").getBytes("UTF-8");            byte[] ps = params.toString().getBytes("UTF-8");            OutputStream os = urlConn.getOutputStream();            os.write(ps);            os.write(fileDiv);            os.write(data);            os.write(endData);            os.flush();            os.close();        } catch(Exception e) {            thrownewRuntimeException(e.getMessage(), e);        }        returnurlConn;    }    privatestaticbyte[] getBytes(HttpURLConnection urlConn) {        try{            InputStream in = urlConn.getInputStream();            ByteArrayOutputStream os = newByteArrayOutputStream();            byte[] buf = newbyte[1024];            for(inti = 0; (i = in.read(buf)) > 0;)                os.write(buf, 0, i);            in.close();            returnos.toByteArray();        } catch(Exception e) {            thrownewRuntimeException(e.getMessage(), e);        }    }}

   这只是简单的功能实现,后面可以用martini拓展,写一个简单的web语音识别服务,用android录音后发送POST请求,把语音数据发送到服务器处理识别,再返回json格式的识别结果。