Ftp上傳和下載Apk腳本

通過 gradle重命名并上傳apk
  • 在module里build.gradle的android閉包中添加
configurations {
     ftpAntTask
}

dependencies閉包中添加

ftpAntTask("org.apache.ant:ant-commons-net:1.8.4") {
    module("commons-net:commons-net:1.4.1") {
         dpendencies "oro:oro:2.0.8:jar"
    }
}
  • 添加拷貝task
String outName
task assembleAndCopyApk(type: Copy) {
    // 拷貝apk
    from(originalFilePath) 
    into(destFilePath)
    // 重命名
    outName = "${releaseTime()}.apk"
    rename(orginalFileName, outName)
    // 編譯命令
    dependsOn xxx
}
  • 添加releaseTime函數(shù)
def releaseTime() {
    return new Date().format("MMdd_HHmm")
}
  • 添加FTP上傳task
task uploadToFtp {
    // 先執(zhí)行編譯,拷貝,重命名
    dependsOn 'assembleAndCopyApk'
    doLast {
         ant {
                taskdef(name: 'ftp', 
                    classname:  'org.apache.tools.ant.taskdefs.optional.net.FTP', 
                    classpath: configurations.ftpAntTask.asPath)
                ftp (server: "xxx.xxx.xxx.xxx",  //ftp地址
                      userid: "xxxx",  //用戶名
                      password: "xxxx", //密碼
                      remoteDir: "/") {
                      fileset(dir: outputDir) {
                              include(name: outName)
                      }
                  }
         }
    }
uploadToFtp.setGroup(group)
使用python腳本下載和安裝apk
from ftplib import FTP
import os, sys, string, datetime, time
import socket

class MYFTP:
    def __init__(self, rootdir_local, hostaddr, username, password, remotedir, port=21):
        self.hostaddr = hostaddr
        self.username = username
        self.password = password
        self.remotedir = remotedir
        self.port = port
        self.ftp = FTP()
        self.file_list = []

        self.rootdir_local = rootdir_local
        self.local_files = []
        self.remote_files = []

    # self.ftp.set_debuglevel(2)
    def __del__(self):
        self.ftp.close()

    # self.ftp.set_debuglevel(0)
    def login(self):
        ftp = self.ftp
        try:
            timeout = 300
            socket.setdefaulttimeout(timeout)
            ftp.set_pasv(True)
            print (u'開始連接到 %s' % (self.hostaddr))
            ftp.connect(self.hostaddr, self.port)
            print (u'成功連接到 %s' % (self.hostaddr))
            print (u'開始登錄到 %s' % (self.hostaddr))
            ftp.login(self.username, self.password)
            print (u'成功登錄到 %s' % (self.hostaddr))
            debug_print(ftp.getwelcome())
        except Exception:
            print (u'連接或登錄失敗')
        try:
            ftp.cwd(self.remotedir)
        except(Exception):
            print (u'切換目錄失敗')

    def margeFile(self):
        temp = []
        try:
            for f in self.remote_files:
                if f in self.local_files:
                    temp.append(f)
            for f in temp:
                self.remote_files.remove(f)
        except Exception as e:
            print (e)
        print (self.remote_files)

    def get_localFileList(self):
        localFiles = os.listdir(self.rootdir_local)
        for fileName in localFiles:
            # print ('本地文件列表:', fileName)
            self.local_files.append(fileName)

    def get_remoteFileList(self):
        print (u'讀取服務(wù)器文件列表')
        print (self.ftp.dir())
        try:
            self.remote_files = self.ftp.nlst()
        except Exception as e:
            print (e)
        for name in self.remote_files:
            print ('服務(wù)端文件:', name)

    def download_file(self, localfile, remotefile):
        debug_print(u'>>>>>>>>>>>>下載文件 %s <<<<<<<<<<<<' % localfile)
        file_handler = open(localfile, 'wb')
        self.ftp.retrbinary(u'RETR %s' % (remotefile), file_handler.write)
        file_handler.close()

    def download_marge_files(self):
        name = ''
        for f in self.remote_files:
            self.download_file(rootdir_local + '/' + f, f)
            name = f
        print ('下載Apk:', name);
        cmdStr = 'adb install -r F:/%s' %name
        print (cmdStr)
        d = os.system(cmdStr)
        print ('安裝APK: ', d)

def debug_print(s):
    print (s)

if __name__ == '__main__':
    timenow = time.localtime()
    datenow = time.strftime('%Y-%m-%d', timenow)
    # 配置如下變量
    hostaddr = 'xxx.xxx.xxx.xxx'  # ftp地址
    username = 'xxx'  # 用戶名
    password = 'xxx'  # 密碼
    port = 21  # 端口號(hào)
    rootdir_local = 'F:/'  # 本地目錄
    rootdir_remote = '/'  # 遠(yuǎn)程目錄

    f = MYFTP(rootdir_local, hostaddr, username, password, rootdir_remote, port)
    f.login()
    f.get_localFileList()
    f.get_remoteFileList()
    f.margeFile()
    f.download_marge_files()
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容