首页 文章

为什么我的node.js没有从Java Android接收这个输出流?

提问于
浏览
0

我有一个类音频发送器,它连接到nodejs服务器并以POST方法模式上传音频文件 .

public class AudioSender implements Callable<JSONObject> {

String outputFile;

AudioSender(String fileLocation){
    outputFile=fileLocation;
}

public JSONObject call(){
    URL url = null;
    HttpURLConnection urlConnection = null;
    InputStream audioInputStream=null;
    JSONObject response=null;
    byte buffer[]=new byte[16];
    try {
        url = new URL("http://192.168.0.106:3000/upload");
    } catch (MalformedURLException e) {
        e.printStackTrace();
    } finally {
        try {
            urlConnection = (HttpURLConnection) url.openConnection();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            urlConnection.setDoOutput(true);
            urlConnection.setChunkedStreamingMode(16);
            urlConnection.setRequestProperty("Content-Type","multipart/form-data");
            urlConnection.setRequestProperty("Connection", "Keep-Alive");
            urlConnection.setRequestProperty("Cache-Control", "no-cache");
            urlConnection.setRequestProperty(
                    "Content-Type", "multipart/form-data;boundary=*****");
            try {
                OutputStream out = new BufferedOutputStream(urlConnection.getOutputStream());
                try {
                    audioInputStream = new BufferedInputStream(new FileInputStream(outputFile));
                    Log.d("hello","audioinputstream");
                } catch (FileNotFoundException e) {
                    e.printStackTrace();
                } finally {

                    while(audioInputStream.read(buffer)!=-1) {
                        out.write(buffer);
                        Log.d("buffer",buffer.toString());
                    }
                    try {
                        audioInputStream.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
                InputStream in = new BufferedInputStream(urlConnection.getInputStream());
                StringBuilder total = new StringBuilder();
                while(in.read(buffer)!=-1)
                    total.append(buffer);
                    Log.d("response",total.toString());
                try {
                    response = new JSONObject(total.toString());
                }catch(JSONException e){
                    Log.e("Response Parse Error", "Could not parse malformed JSON");
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    return response;
}

这是执行AudioSender可调用的上传 .

public void upload() {
    JSONObject response=null;
    AudioSender sender=new AudioSender(outputFile);
    FutureTask<JSONObject> uploadTask=new FutureTask<JSONObject>(sender);
    ExecutorService executorService= Executors.newFixedThreadPool(1);
    executorService.execute(uploadTask);
    Log.d("s","was here");
    while(true){
        if(uploadTask.isDone()){
            try{
                response=uploadTask.get();
            }catch(InterruptedException|ExecutionException e){
                e.printStackTrace();
                Log.e("ee","ee",e.getCause());

            }
        }
    }
}

我非常知道这不是节点js的错,但这里是服务器代码:app.post('/ upload',function(req,res){console.log(“有人叫!”); req.on('data ',function(chunk){console.log('res'); console.log(chunk);}); req.on('end',function(){console.log(“done!”); res . send({'name':'sg'});});});

当我调用upload()时,服务器控制台会打印一个名为!完成了!

我正在调试,发现我确实从服务器接收了响应的json对象 . 而且我不知道out.write(缓冲区)是否正在完成这项工作,但是调试它会显示缓冲区值正在改变并且与我的音频文件的大小相同 .

请不要建议使用ION或其他任何东西 .

1 回答

  • 0

    我通过设置URLConnection解决了这些问题,如下所示:

    boundary = "===" + System.currentTimeMillis() + "===";
                urlConnection.setUseCaches(false);
                urlConnection.setDoOutput(true);    // indicates POST method
                urlConnection.setDoInput(true);
                urlConnection.setRequestProperty("Content-Type","multipart/form-data; boundary=" + boundary);
    

相关问题