首页 > 解决方案 > Android使用socket.io将图像作为二进制流发送

问题描述

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

标签: androidnode.jsandroid-studiosocket.iostreaming

解决方案


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


推荐阅读