How to upload a WAV file using URLConnection

前端 未结 2 1982
走了就别回头了
走了就别回头了 2020-12-18 17:34

Curretly, I can upload .wav files by using this command in Terminal (MAC):

curl -H \'X-Arg: AccessKey=3f55abc5-2830-027ce-e328-4e74df5a3f8f\' --data-binary @         


        
相关标签:
2条回答
  • 2020-12-18 17:44

    It's simpler code that wrote for wit.ai I hope it helps:

    String requestURL = "https://api.wit.ai/speech?v=20160526";
    URL url = new URL(requestURL);
    HttpURLConnection httpConn = (HttpURLConnection) url.openConnection();
    httpConn.setUseCaches(false);
    httpConn.setDoOutput(true); // indicates POST method
    httpConn.setDoInput(true);
    
    httpConn.setRequestMethod("POST");
    httpConn.setRequestProperty("Connection", "Keep-Alive");
    httpConn.setRequestProperty("Cache-Control", "no-cache");
    httpConn.setRequestProperty("Authorization", "Bearer XXXXXXXXXXXXXXXXXXXXXX");;
    httpConn.setRequestProperty("Content-Type", "audio/wav");;
    File waveFile= new File("RecordAudio.wav");
    byte[] bytes = Files.readAllBytes(waveFile.toPath());
    DataOutputStream request = new DataOutputStream(httpConn.getOutputStream());
    request.write(bytes);
    request.flush();
    request.close();
    
    String response = "";
    // checks server's status code first
    int status = httpConn.getResponseCode();
    if (status == HttpURLConnection.HTTP_OK) {
        InputStream responseStream = new BufferedInputStream(httpConn.getInputStream());
    
        BufferedReader responseStreamReader
                = new BufferedReader(new InputStreamReader(responseStream));
    
        String line = "";
        StringBuilder stringBuilder = new StringBuilder();
    
        while ((line = responseStreamReader.readLine()) != null) {
            stringBuilder.append(line).append("\n");
        }
        responseStreamReader.close();
    
        response = stringBuilder.toString();
        httpConn.disconnect();
    } else {
        throw new IOException("Server returned non-OK status: " + status);
    }
    
    0 讨论(0)
  • 2020-12-18 17:48

    I finally found a way to do upload wav files. Some servers have slightly different ways of accepting or rejecting an upload request. In this other question URLConnection always returns 400 : Bad Request when I try to upload a .wav file I was trying to accomplish this same task on a different server, and proved to be more complicated than the one I am about to share in this answer, but take a look at it if you still cant make things work. There is no short way of directly "translating" a CURL request into java without messing with the NDK. For this question, this is the code that worked for me:

    final String ASR_URL="http:..... ";
    public class curlAudioToWatson extends AsyncTask<String, Void, String> {
        String asrJsonString="";
        @Override
        protected String doInBackground(String... params) {
            String result = "";
            try {
                loge("**** UPLOADING .WAV to ASR...");
                URL obj = new URL(ASR_URL);
                HttpURLConnection conn = (HttpURLConnection) obj.openConnection();
                conn.setRequestProperty("X-Arg", "AccessKey=3fvfg985-2830-07ce-e998-4e74df");
                conn.setRequestProperty("Content-Type", "audio/wav");
                conn.setRequestMethod("POST");
                conn.setDoInput(true);
                conn.setDoOutput(true);
                String wavpath=Environment.getExternalStorageDirectory().getAbsolutePath()+"/my AppFolder/"+StorageUtils.AUDIO_FILE_NAME+".wav"; //audio.wav";
                File wavfile = new File(wavpath);
                boolean success = true;
                if (wavfile.exists()) {
                    loge("**** audio.wav DETECTED: "+wavfile);
                }
                else{
                    loge("**** audio.wav MISSING: " +wavfile);
                }
    
                String charset="UTF-8";
                String boundary = Long.toHexString(System.currentTimeMillis()); // Just generate some unique random value.
                String CRLF = "\r\n"; // Line separator required by multipart/form-data.
    
                OutputStream output=null;
                PrintWriter writer=null;
                try {
                    output = conn.getOutputStream();
                    writer = new PrintWriter(new OutputStreamWriter(output, charset), true);
                    byte [] music=new byte[(int) wavfile.length()];//size & length of the file
                    InputStream             is  = new FileInputStream       (wavfile);
                    BufferedInputStream bis = new BufferedInputStream   (is, 16000);
                    DataInputStream dis = new DataInputStream       (bis);      //  Create a DataInputStream to read the audio data from the saved file
                    int i = 0;
                    copyStream(dis,output);
                }
                catch(Exception e){
    
                }
    
                conn.connect();
    
                int responseCode = conn.getResponseCode();
                logy("POST Response Code : " + responseCode + " , MSG: " + conn.getResponseMessage());
    
                if (responseCode == HttpURLConnection.HTTP_OK) { //success
                    BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
                    String inputLine;
                    StringBuffer response = new StringBuffer();
    
                    while ((inputLine = in.readLine()) != null) {
                        response.append(inputLine);
                    }
                    in.close();
                    loge("***ASR RESULT: " + response.toString());
    
    
                    JSONArray jresponse=new JSONObject(response.toString()).getJSONObject("Recognition").getJSONArray("NBest");
                    asrJsonString=jresponse.toString();
    
                    for(int i = 0 ; i < jresponse.length(); i++){
                        JSONObject jsoni=jresponse.getJSONObject(i);
                        if(jsoni.has("ResultText")){
                            String asrResult=jsoni.getString("ResultText");
                            //ActionManager.getInstance().addDebugMessage("ASR Result: "+asrResult);
                            loge("*** Result Text: "+asrResult);
                            result = asrResult;
                        }
                    }
                    loge("***ASR RESULT: " + jresponse.toString());
    
                } else {
                    BufferedReader in = new BufferedReader(new InputStreamReader(conn.getErrorStream()));
                    String inputLine;
                    StringBuffer response = new StringBuffer();
                    while ((inputLine = in.readLine()) != null) {
                        response.append(inputLine);
                    }
                    in.close();
                    loge("POST FAILED: " + response.toString());
                    result = "";
                }
            } catch (Exception e) {
                logy("HTTP Exception: " + e.getLocalizedMessage());
            }
            return result; //"Failed to fetch data!";
        }
    
        @Override
        protected void onPostExecute(String result) {
            if(!result.equals("")){
                logy("onPostEXECUTE SUCCESS, consuming result");
                sendTextInputFromUser(result);
                ActionManager.getInstance().addDebugMessage("***ASR RESULT: "+asrJsonString);
                runOnUiThread(new Runnable() {
                    @Override 
                    public void run() { 
    
                   } 
               });
            }else{
                logy("onPostEXECUTE FAILED");
            }
        }
    }
    
    
    public void copyStream( InputStream is, OutputStream os) {
        final int buffer_size = 4096;
        try {
    
            byte[] bytes = new byte[buffer_size];
            int k=-1;
            double prog=0;
            while ((k = is.read(bytes, 0, bytes.length)) > -1) {
                if(k != -1) {
                    os.write(bytes, 0, k);
                    prog=prog+k;
                    double progress = ((long) prog)/1000;///size;
                    loge("UPLOADING: "+progress+" kB");
                }
            }
            os.flush();
            is.close();
            os.close();
        } catch (Exception ex) {
            loge("File to Network Stream Copy error "+ex);
        }
    }
    
    0 讨论(0)
提交回复
热议问题