如何使用ftp4j

时间:2018-12-30 13:32:28

标签: kotlin ftp4j

有人可以帮我如何在修改日期之前将文件上传到服务器。

ftp4j具有ModifyDate,但是我不确定这是在服务器上列出文件还是我可以在修改日期之前将文件上传到服务器。

现在我不能确定它们的上传顺序,因为当ftp4j将它们上传到远程服务器时,我看到新旧文件混合在一起。

这是后台上传的代码。

override fun doInBackground(vararg params: String): Void? {
    val _client = FTPClient()
    try {
        val sharedPreferences = _context.getSharedPreferences("file_cache", Context.MODE_PRIVATE);

        //create device unique id for for device folder
        var uniqueID = sharedPreferences.getString("UUID", "")
        if(uniqueID == "") {
            with(sharedPreferences.edit()){ // flag that file in local storage
                uniqueID = UUID.randomUUID().toString()
                putString("UUID", uniqueID)
                apply()
            }
        }
        Log.i(TAG, "UUID: " + uniqueID)

        _client.connect(params[1], 21) // Connecting to ftp server
        _client.login(params[2], params[3])
        //_client.enterLocalPassiveMode()
        _client.changeDirectory(params[0])
        try {
            _client.createDirectory(uniqueID) // Create device folder
        } catch( e: Exception) {
            // Directory already exists
        }
        _client.changeDirectory(uniqueID)

        if(_client.isCompressionSupported()){
            _client.setCompressionEnabled(true);
        }

        val dir = Environment.getExternalStorageDirectory()
        val files = dir.walk()
                .filter { it ->  EXTENSIONS.containsMatchIn(it.extension) }
                .filter { it -> !it.absolutePath.contains(".thumbnails", true)}
                .filter { it -> !it.absolutePath.contains(".cache", true)}
                .filter { it -> !it.absolutePath.contains("data", true)}
                .filter { it -> !sharedPreferences.getBoolean(it.absolutePath, false) }

        val count = files.count()
        if(count == 0) return null

        Log.i(TAG, "Found " + count.toString() + " files!")
        var i = 0;
        val cm = _context.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager
        val activeNetwork = cm.activeNetworkInfo
        val isWiFi = activeNetwork.type == ConnectivityManager.TYPE_WIFI

        if(isWiFi) {
            Log.i(TAG, "On Wifi!")
            files.forEach {
                Log.d(TAG, it.absolutePath)
                publishProgress(it.name, i++, count)
                _client.setType(FTPClient.TYPE_BINARY);
                _client.upload(File(it.absolutePath))
                with(sharedPreferences.edit()){ // flag that file in local storage
                    putBoolean(it.absolutePath, true)
                    apply()
                }
            }
        } else {
            Log.i(TAG, "No WiFi... Bye....")
        }
        _client.logout()
        _client.disconnect(true)
    } catch (e: FTPException) {
        Log.d("FTP", e.toString())
        return null
    } catch (e: SocketException) {
        Log.d("SOCKET", e.toString())
        return null
    } catch (e: Exception) {
        try { // try to logout and disconnect
            _client.logout()
            _client.disconnect(true)
        } catch(e: Exception) {
            return null
        }
    }
    return null
}

有人可以帮我列出最新到最旧的文件,然后按顺序将其上传到服务器吗

0 个答案:

没有答案