【问题标题】:Android send image as binary streaming using socket.ioAndroid使用socket.io将图像作为二进制流发送
【发布时间】:2018-12-09 19:31:44
【问题描述】:

伙计们,我想在 Android 上使用 node.js 和 socket.io 将图像作为二进制流发送到服务器,有人可以帮忙吗?

【问题讨论】:

    标签: android node.js android-studio socket.io streaming


    【解决方案1】:

    Android 中有几个库可以上传图片和视频等大文件。除非您将其作为一种学习体验,否则几乎可以肯定使用其中之一会更容易。最近的一些包括:

    较旧的库是 apache MultiPartMine 库 - 请参阅此处的示例以获取视频而不是图像,但适用相同的原则:

    //Create a new Multipart HTTP request to upload the video
            HttpClient httpclient = new DefaultHttpClient();
            HttpPost httppost = new HttpPost(serverURL);
    
            //Create a Multipart entity and add the parts to it
            try {
                Log.d("VideoUploadTask doInBackground","Building the request for file: " + videoPath);
                FileBody filebodyVideo = new FileBody(new File(videoPath));
                StringBody title = new StringBody("Filename:" + videoPath);
                StringBody description = new StringBody("Test Video");
                MultipartEntity reqEntity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
                reqEntity.addPart("videoFile", filebodyVideo);
                reqEntity.addPart("title", title);
                reqEntity.addPart("description", description);
                httppost.setEntity(reqEntity);
            } catch (UnsupportedEncodingException e1) {
                //Log the error
                Log.d("VideoUploadTask doInBackground","UnsupportedEncodingException error when setting StringBody for title or description");
                e1.printStackTrace();
                return -1;
            }
    
            //Send the request to the server
            HttpResponse serverResponse = null;
            try {
                Log.d("VideoUploadTask doInBackground","Sending the Request");
                serverResponse = httpclient.execute( httppost );
            } catch (ClientProtocolException e) {
                //Log the error
                Log.d("VideoUploadTask doInBackground","ClientProtocolException");
                e.printStackTrace();
            } catch (IOException e) {
                //Log the error
                Log.d("VideoUploadTask doInBackground","IOException");
                e.printStackTrace();
            }
    
            //Check the response code
            Log.d("VideoUploadTask doInBackground","Checking the response code");
            if (serverResponse != null) {
                Log.d("VideoUploadTask doInBackground","ServerRespone" + serverResponse.getStatusLine());
                HttpEntity responseEntity = serverResponse.getEntity( );
                if (responseEntity != null) {
                    //log the response code and consume the content
                    Log.d("VideoUploadTask doInBackground","responseEntity is not null");
                    try {
                        responseEntity.consumeContent( );
                    } catch (IOException e) {
                        //Log the (further...) error...
                        Log.d("VideoUploadTask doInBackground","IOexception consuming content");
                        e.printStackTrace();
                    }
                } 
            } else {
                //Log that response code was null
                Log.d("VideoUploadTask doInBackground","serverResponse = null");
                return -1;
            }
    

    此处为完整示例:https://stackoverflow.com/a/32887541/334402

    同样,在节点端,您可以利用标准库来上传文件。一个例子是集合:

    Multer 是一个用于处理 multipart/form-data 的 node.js 中间件,主要用于上传文件。它被写在 busboy 的顶部以获得最大的效率。

    这里有一个经过测试(尽管是几年前..)的示例:https://stackoverflow.com/a/41567587/334402

    如果您确实想编写自己的服务器端解析器,此答案中有一些很好的最新信息:https://stackoverflow.com/a/23718166/334402

    【讨论】:

      猜你喜欢
      • 2016-07-10
      • 2016-09-16
      • 1970-01-01
      • 2014-06-07
      • 2016-03-07
      • 1970-01-01
      • 1970-01-01
      • 2020-05-04
      • 2013-07-07
      相关资源
      最近更新 更多