我想把图像Node.js服务器发送到安卓客户端。
我使用的是Node.js和安卓设备之间的REST服务。
我可以发送图像使用node.js模块'fs‘和接收安卓设备。
没关系,但我有200多张图片,每幅图像的大小在1KB到2KB之间。这是很小的图像。所以我不想一个一个地寄出。太慢了,所以我很好奇如果我".rar“所有的图像文件(大约2MB),我能发送一次并在安卓设备上显示图像吗?
或者没有".rar“就可以发送一次?
发布于 2014-10-09 13:05:02
当然,您可以将它们压缩到存档(任何类型)中,并在设备上解压缩它们。
使用nodejs-zip,您可以生成压缩档案。压缩示例(取自这里)
var http = require('http'),
nodejszip = require('../lib/nodejs-zip');
http.createServer(function (req, res) {
var file = 'compress-example.zip',
arguments = ['-j'],
fileList = [
'assets/image_1.jpg',
'assets/image_2.jpg',
'assets/image_3.jpg',
'assets/image_4.jpg',
'assets/image_5.jpg',
'assets/image_6.jpg',
'assets/image_7.jpg',
'assets/image_8.jpg',
'assets/image_9.jpg',
'assets/image_10.jpg',
'assets/image_11.jpg',
'assets/image_12.jpg',
'assets/image_13.jpg',
'assets/image_14.jpg'];
var zip = new nodejszip();
zip.compress(file, fileList, arguments, function(err) {
if (err) {
throw err;
}
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end('Complete.\n');
});
}).listen(8000);在设备上,您可以像这样解压缩文件(取自这里)。
public class Decompress {
private String _zipFile;
private String _location;
public Decompress(String zipFile, String location) {
_zipFile = zipFile;
_location = location;
_dirChecker("");
}
public void unzip() {
try {
FileInputStream fin = new FileInputStream(_zipFile);
ZipInputStream zin = new ZipInputStream(fin);
ZipEntry ze = null;
while ((ze = zin.getNextEntry()) != null) {
Log.v("Decompress", "Unzipping " + ze.getName());
if(ze.isDirectory()) {
_dirChecker(ze.getName());
} else {
FileOutputStream fout = new FileOutputStream(_location + ze.getName());
for (int c = zin.read(); c != -1; c = zin.read()) {
fout.write(c);
}
zin.closeEntry();
fout.close();
}
}
zin.close();
} catch(Exception e) {
Log.e("Decompress", "unzip", e);
}
}
private void _dirChecker(String dir) {
File f = new File(_location + dir);
if(!f.isDirectory()) {
f.mkdirs();
}
}
}https://stackoverflow.com/questions/26278800
复制相似问题