在Android上要想访问ftp,可以使用busybox ftpget。但busybox ftpget只能下载单个文件。
Android上是有curl命令的,原生的curl是可以访问FTP服务查看文件列表及下载文件,但Android上的ftp功能已经被屏蔽了,见:
commit 251844e0e0f71b9270e7cbb063e8de158518092f
Author: Alex Deymo
Date: Tue Dec 19 14:16:42 2017 +0100
Disable FTP and FTPS.
We don't use FTP or FTPS either, so no need to compile support for FTP.
Bug: 70741465
Test: Applied update over HTTP. 'curl' command failed to fetch ftp:// URI.
Change-Id: Iea4e5278e269c094f4cc86353a36ac9572c7f3b2
Android的curl源码路径: external/curl/ 开启Android源码中curl的ftp功能,需要做如下修改:
diff --git a/lib/curl_config.h b/lib/curl_config.h
index 90a2a58..62274aa 100644
--- a/lib/curl_config.h
+++ b/lib/curl_config.h
@@ -25,9 +25,6 @@
/* to disable FILE */
/* #undef CURL_DISABLE_FILE */
-/* to disable FTP */
-#define CURL_DISABLE_FTP 1
-
/* to disable Gopher */
#define CURL_DISABLE_GOPHER 1
在编译后,curl可以支持FTP功能了。但是如何使用Curl来批量下载整个FTP服务器上的文件呢?
在网络上查找了一个curl递归下载的shell脚本参考了下,写出如下脚本PC测试可用,如Android使用需
#!/bin/bash
Url=$1 #ftp host url
User=$2 #ftp user
Password=$3 #ftp password
DownListTmpFile="/tmp/tmplist.txt" #tmp list file
#DownFileType="zip$|gz$"
UrlBack=""
DLBASE=`pwd` #download root path
WORKDIR=`pwd`
[ ! -f $DownListTmpFile ] && touch $DownListTmpFile || echo > $DownListTmpFile
CURL_URLS(){
echo UrlBack $UrlBack
Urls=`curl -u $User:$Password $Url/$UrlBack -s | awk '{printf "%s\n",$9}'`
echo Urls=$Urls
}
URL_LIST(){
CURL_URLS
for i in $Urls ;do
echo "$UrlBack$i" >> $DownListTmpFile
done
}
RECURSIVE_SEARCH_URL(){
UrlBackTmps=`cat $DownListTmpFile`
[[ "$UrlBackTmps" == "" ]] && echo "no more page for search" && exit 1
for j in $UrlBackTmps ;do
TEST=`curl -u $User:$Password $Url/$j/ -s | awk '{printf "%s\n",$2}'`
echo $Url $j
if [ -z "$TEST" ]; then
FilePath=$DLBASE/$j
[ ! -d `dirname $FilePath` ] && mkdir -p `dirname $FilePath`
[ ! -f $FilePath ] && cd `dirname $FilePath`
curl -u $User:$Password $Url/$j -O
else
UrlBack="$j/"
echo $UrlBack URL_LIST
URL_LIST
fi
UrlTmps=`grep -vE "$j$" $DownListTmpFile`
echo "$UrlTmps" > $DownListTmpFile
done
RECURSIVE_SEARCH_URL
}
URL_LIST $Urls
RECURSIVE_SEARCH_URL
cd $WORKDIR
特记录下脚本及Android curl 的使用,在Android系统上也已经验证可用,只需要修改下shell声明以及临时