首页 文章

Android使用socket.io将图像作为二进制流发送

提问于
浏览
0

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

1 回答

  • 0

    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

相关问题