使用Node js和Android通过Socket io传输大文件

时间:2018-10-25 11:33:27

标签: android node.js socket.io

我正在使用Nodejs和Android来传输大文件。 像视频大小+ 10MB

我使用此链接How to Create a Resumable Video Uploader in Node.js从html进行

所以我也想在Android中做到这一点!

这是服务器

这部分工作正常!

socket.on('Start', function (data) { //data contains the variables that we passed through in the html file
        var Name = data['Name'];
        Files[Name] = {  //Create a new Entry in The Files Variable
            FileSize : data['Size'],
            Data     : "",
            Downloaded : 0
        }
        var Place = 0;
        try{
            var Stat = fs.statSync('Temp/' +  Name);
            if(Stat.isFile())
            {
                Files[Name]['Downloaded'] = Stat.size;
                Place = Stat.size / 524288;
            }
        }
        catch(er){} //It's a New File
        fs.open("Temp/" + Name, "a", 0755, function(err, fd){
            if(err)
            {
                console.log(err);
            }
            else
            {
                Files[Name]['Handler'] = fd; //We store the file handler so we can write to it later
                socket.emit('MoreData', { 'Place' : Place, Percent : 0 });
            }
        });
    });

此处必须添加数据。

socket.on('Upload', function (data){
    var Name = data['Name'];
    Files[Name]['Downloaded'] += data['Data'].length;
    Files[Name]['Data'] += data['Data'];
    if(Files[Name]['Downloaded'] == Files[Name]['FileSize']) //If File is Fully Uploaded
    {
        fs.write(Files[Name]['Handler'], Files[Name]['Data'], null, 'Binary', function(err, Writen){
            //Get Thumbnail Here
        });
    }
    else if(Files[Name]['Data'].length > 10485760){ //If the Data Buffer reaches 10MB
        fs.write(Files[Name]['Handler'], Files[Name]['Data'], null, 'Binary', function(err, Writen){
            Files[Name]['Data'] = ""; //Reset The Buffer
            var Place = Files[Name]['Downloaded'] / 524288;
            var Percent = (Files[Name]['Downloaded'] / Files[Name]['FileSize']) * 100;
            socket.emit('MoreData', { 'Place' : Place, 'Percent' :  Percent});
        });
    }
    else
    {
        var Place = Files[Name]['Downloaded'] / 524288;
        var Percent = (Files[Name]['Downloaded'] / Files[Name]['FileSize']) * 100;
        socket.emit('MoreData', { 'Place' : Place, 'Percent' :  Percent});
    }
});

所以我确实知道如何从Android发送数据

socket.on("MoreData", new Emitter.Listener() {
    @Override
    public void call(Object... args) {
        try {
            JSONObject jsonObject = (JSONObject) args[0];
            String fileName = jsonObject.getString("Name");
            File f = new File(FileUtils.external, VIDEO_RECORDER_FOLDER_SENT + "/" + fileName);
            int partCounter = 1;
            List<File> result = new ArrayList<>();
            //int sizeOfFiles = jsonObject.getInt("Place") * 524288;
            int sizeOfFiles = jsonObject.getInt("Place") * 524288 + Math.min(524288, ((int)f.length()-jsonObject.getInt("Place") * 524288));
            byte[] buffer = new byte[sizeOfFiles];
            if(FileUtils.debuggableState()) {
                Log.e("Transfer-File-place", "" + jsonObject.getInt("Place") * 524288);
                Log.e("Transfer-File", "" + sizeOfFiles);
            }
            // create a buffer of bytes sized as the one chunk size

            BufferedInputStream bis = new BufferedInputStream(new FileInputStream(f));
            String name = f.getName();

            int tmp = 0;
            if ((bis.read(buffer)) > 0) {
                /*File newFile = new File(f.getParent(), name + "." + String.format("%03d", partCounter++));
                // naming files as <inputFileName>.001, <inputFileName>.002, ...
                FileOutputStream out = new FileOutputStream(newFile);
                out.write(buffer, 0, tmp); //tmp is chunk size. Need it for the last chunk,
                // which could be less then 1 mb.
                result.add(newFile);*/

                if(isInternetPresent &&  socket != null && socket.connected()) {
                    JSONObject sending = new JSONObject();
                    sending.put("Name", fileName);
                    sending.put("Data", buffer);
                    //sending.put("")
                    socket.emit("Upload", sending);
                }
            }
        } catch (Exception throwable) {
            if(FileUtils.debuggableState())
                throwable.printStackTrace();
        }
    }
});

有人可以帮助我吗?

谢谢!

0 个答案:

没有答案