autojs连接ftp,上传下载文件

这里调用的是edtftpj.jar
下载地址
https://enterprisedt.com/products/edtftpj/download/
首先加载jar

// 加载jsoup.jar
runtime.loadJar("./edtftpj.jar");
importClass(java.io.IOException)
importClass(com.enterprisedt.net.ftp.FTPClient)
importClass(com.enterprisedt.net.ftp.FTPConnectMode)
importClass(com.enterprisedt.net.ftp.FTPException)
importClass(com.enterprisedt.net.ftp.FTPTransferType)
importClass(com.enterprisedt.net.ftp.FTPFile)
importClass(com.enterprisedt.net.ftp.FileTransferClient)
importClass(com.enterprisedt.net.ftp.WriteMode)
importClass(org.apache.commons.logging.Log)
importClass(org.apache.commons.logging.LogFactory)
importClass(java.io.File)
importClass(java.text.ParseException)

自定义

/**
 * 连接ftp服务器
 *
 * @return 连接成功标识
 */
function connectServer() {
    var flag = false;
    client = new FileTransferClient();
    client.setUserName("admin");
    client.setPassword("123456");
    client.setRemoteHost("110.42.181.89");
    client.setRemotePort(21);
    client.setTimeout(1200);

    //client.setEventListener( new UploadListener(client));
    client.getAdvancedSettings().setTransferBufferSize(1000);
    client.getAdvancedSettings().setTransferNotifyInterval(5000);
    client.getAdvancedSettings().setControlEncoding("GBK");
    client.connect();
    flag = true;

    return flag;
}

/**
 * 关闭FTP客户端连接
 *
 * @return 关闭成功标识
 */
function disconnect() {
    var flag = false;
    client.disconnect();
    flag = true;
    return flag;
}

/**
 * 上传文件
 *
 * @param file 文件
 */
function ftpUploadFile(filePath) { //文件路径
    var fileName = files.getName(filePath);
        client.uploadFile(filePath, fileName, WriteMode.RESUME);
        var len = client.getSize(fileName);
        log("上传文件" + filePath + "完成,大小为" + len + "字节!");
   
}
/**
 * 下载文件
 *
 * @param localFileName    本地文件名
 * @param remoteFileName 远程文件名
 */
function ftpDownload(localFileName, remoteFileName) {
    client.downloadFile(localFileName, remoteFileName, WriteMode.OVERWRITE);
    log("下载成功")
}

测试结果

//连接服务器
connectServer()
//上传文件
ftpUploadFile("/storage/emulated/0/脚本/Ftp/Ftp.zip")
//下载文件
ftpDownload("/storage/emulated/0/脚本/Ftp/1.zip","./Ftp.zip")

效果

  • 1
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值