android - Android使用socket.io将图像作为二进制流发送
问题描述
伙计们,我想在android上使用node.js和socket.io将图像作为二进制流发送到服务器,有人可以帮忙吗?
解决方案
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
推荐阅读
- javascript - jQuery 函数不可逆。[网站购物车功能]
- mysql - mysql认证
- regex - SED - 当文件中不存在特定字符串时,在特定模式后插入保存的值
- c# - 在周一至周日上午 7:20 至晚上 8:30 之间每 x 分钟触发一次的 Cron 表达式
- graphics - 3d 引擎如何决定多边形在模型中的位置?
- python - 尝试使用数据库查询时,Django 通道 Async Websocket 抛出错误
- python - 如何在 Python/Opencv 中模拟按键?
- html - 响应式图像顶部的响应式文本叠加
- javascript - 无法在嵌套组件上设置道具
- eclipse - AWS Toolkit for Eclipse 2.0 IAM 角色支持?