From: snickersun Date: Sat, 22 Feb 2020 18:58:39 +0000 (+0800) Subject: 1 Battery日志添加电压值 X-Git-Url: http://git.snicker.com.cn/sitemap.xml?a=commitdiff_plain;h=6b4a80552c92cce05d99190a74177ea663b31638;p=icsupport.git 1 Battery日志添加电压值 2 添加FTP上传日志支持,短信命令触发 #logctl#=current #logctl#=reset #logctl#=icfota.snicker.com.cn,ftpuser,iclog #logctl#=upload,all #logctl#=upload,APIlog --- diff --git a/app/app.iml b/app/app.iml index 19e98f4e..1a534bf2 100644 --- a/app/app.iml +++ b/app/app.iml @@ -103,6 +103,7 @@ + @@ -121,12 +122,5 @@ - - - - - - - \ No newline at end of file diff --git a/app/libs/ftp4j-1.7.2.jar b/app/libs/ftp4j-1.7.2.jar new file mode 100644 index 00000000..fca26eca Binary files /dev/null and b/app/libs/ftp4j-1.7.2.jar differ diff --git a/app/src/main/java/com/enqualcomm/support/Config.java b/app/src/main/java/com/enqualcomm/support/Config.java index fbc4fa61..c20d55d9 100644 --- a/app/src/main/java/com/enqualcomm/support/Config.java +++ b/app/src/main/java/com/enqualcomm/support/Config.java @@ -179,7 +179,19 @@ public class Config { public static String work_mode = "2@1"; private static final int yunosuuid = 0; - + + + public static final String DefaultHost="watch.jiai.pro"; + public static final int DefaultPort=8090; + + public static final String DefaultLogFtpServer="icfota.snicker.com.cn"; + public static final String DefaultLogFtpUser="ftpuser"; + public static final String DefaultLogFtpPWD="iclog"; + + + public static final String DefaultWiFiSSID="Xplay6"; + public static final String DefaultWiFiPWD="87654321"; + static { } @@ -291,6 +303,61 @@ public class Config { final static String KeyHeartBeatCycle="ServerHeartBeatCycle"; + + + final static String KeyLogFtpServer="FtpServerHost"; + final static String KeyLogFtpUser="FtpServerUser"; + final static String KeyLogFtpPWD="FtpServerPWD"; + + + final static String KeyWiFiSSID="WiFiSSID"; + final static String KeyWiFiPasswd="WiFiPWD"; + + + public static String getWiFiSSID(Context context){ + return SharPreferenceUtil.getPrefString(context,KeyWiFiSSID,DefaultWiFiSSID); + } + + public static void setWiFiSSID(Context context,String ssid){ + SharPreferenceUtil.setPrefString(context,KeyWiFiSSID,ssid); + } + + public static String getWiFiPasswd(Context context){ + return SharPreferenceUtil.getPrefString(context,KeyWiFiPasswd,DefaultWiFiPWD); + } + + public static void setWiFiPasswd(Context context,String pwd){ + SharPreferenceUtil.setPrefString(context,KeyWiFiPasswd,pwd); + } + + + public static String getFtpUser(Context context){ + return SharPreferenceUtil.getPrefString(context,KeyLogFtpUser,DefaultLogFtpUser); + } + + public static void setFtpUser(Context context,String user){ + SharPreferenceUtil.setPrefString(context,KeyLogFtpUser,user); + } + + public static String getFtpServer(Context context){ + return SharPreferenceUtil.getPrefString(context,KeyLogFtpServer,DefaultLogFtpServer); + } + + public static void setFtpServer(Context context,String host){ + SharPreferenceUtil.setPrefString(context,KeyLogFtpServer,host); + } + + public static String getFtpPWD(Context context){ + return SharPreferenceUtil.getPrefString(context,KeyLogFtpPWD,DefaultLogFtpPWD); + } + public static void setFtpPWD(Context context,String PWD){ + SharPreferenceUtil.setPrefString(context,KeyLogFtpPWD,PWD); + } + + + + + public static void setAlarmCycle(Context context, int cycle){ SharPreferenceUtil.setPrefInt(context,KeyAlarmCycle,cycle); } diff --git a/app/src/main/java/com/enqualcomm/support/ICSmsManager.java b/app/src/main/java/com/enqualcomm/support/ICSmsManager.java index a2ec87a0..2e486a1f 100644 --- a/app/src/main/java/com/enqualcomm/support/ICSmsManager.java +++ b/app/src/main/java/com/enqualcomm/support/ICSmsManager.java @@ -20,7 +20,13 @@ import com.enqualcomm.support.utils.APNUtil; import com.enqualcomm.support.utils.DeviceInfoUtil; import com.enqualcomm.support.utils.Logger; import com.enqualcomm.support.utils.ServiceUtil; +import com.enqualcomm.support.utils.TimeUtil; +import com.enqualcomm.support.utils.Util; +import com.enqualcomm.support.utils.ftpUtils; +import java.io.File; +import java.net.MalformedURLException; +import java.net.URL; import java.util.ArrayList; import java.util.Iterator; import java.util.concurrent.TimeUnit; @@ -190,9 +196,9 @@ public class ICSmsManager { Logger.i(sms + " len " + tmp.length); if (tmp.length == 2) { if (tmp[1].equals("reset#")) { - Config.setProtocolServerHost(mContext, Runtime.DefaultHost); - Config.setProtocolServerAddr(mContext, "", Runtime.DefaultPort); - reply = "reset ServerIP to " + Runtime.DefaultHost + ":" + Runtime.DefaultPort + " success! "; + Config.setProtocolServerHost(mContext, Config.DefaultHost); + Config.setProtocolServerAddr(mContext, "", Config.DefaultPort); + reply = "reset ServerIP to " + Config.DefaultHost + ":" + Config.DefaultPort + " success! "; Config.loadServerSettings(runtime); CoreService.getInstance().disconnect_server(); CoreService.getInstance().ReConnectToServer(); @@ -299,7 +305,121 @@ public class ICSmsManager { } else { sendSMS(phonenumber, "skipmovecheck format error!"); } - } else { + }else if(sms.startsWith("#connectwifi#=")){ + String tmp[] = sms.split("="); + if (tmp.length == 2) { + if (tmp[1].equals("current")) { + sendSMS(phonenumber, "SkipMoveCheck " + LocationClient.getSkipMoveCheck() + "!"); + } else if (tmp[1].equals("reset")) { + LocationClient.setSkipMoveCheck(true); + sendSMS(phonenumber, "set SkipMoveCheck " + LocationClient.getSkipMoveCheck() + " success!"); + } else { + + + sendSMS(phonenumber, "skipmovecheck format error!"); + } + } else { + sendSMS(phonenumber, "skipmovecheck format error!"); + } + }else if(sms.startsWith("#logctl#=")){ + String tmp[] = sms.split("="); + if (tmp.length == 2) { + if (tmp[1].equals("current")) { + sendSMS(phonenumber, "Current FtpLogServer " + Config.getFtpServer(mContext) + ", user " + Config.getFtpUser(mContext) + ", pwd " + Config.getFtpPWD(mContext)); + }else if (tmp[1].equals("reset")) { + + Config.setFtpServer(mContext, Config.DefaultLogFtpServer); + Config.setFtpUser(mContext, Config.DefaultLogFtpUser); + Config.setFtpPWD(mContext, Config.DefaultLogFtpPWD); + sendSMS(phonenumber, "reset FtpLogServer to " + Config.getFtpServer(mContext) + ", user " + Config.getFtpUser(mContext) + ", pwd " + Config.getFtpPWD(mContext)); + }else if (tmp[1].startsWith("upload,")){ + + String cmdl[] = tmp[1].split(","); + if (cmdl.length == 2) { + String logfile=cmdl[1]; + + if (logfile.isEmpty()|| logfile.equals("all")){ + String filelist[]={"APILog","GPS","LocationAlarm","Battery","log"}; +// String filelist[]={"APILog","LocationAlarm","Battery"}; + String time=TimeUtil.getSystemTime3(); + StringBuilder sb=new StringBuilder(); + Observable.timer(500L, TimeUnit.MILLISECONDS).subscribe(new Action1() { + public void call(Long param1Long) { + sendSMS(phonenumber, "start Upload allLogfile [APILog log GPS LocationAlarm Battery] to " + Config.getFtpServer(mContext)); + for (String file : filelist) { + + if (Util.LogfileExist(runtime, file)) { + + String filepath = Util.getLogfilePath(runtime, file); + URL serverurl = null; + try { + serverurl = new URL(Util.getServerLogfilePath(Config.getFtpServer(mContext), time, file)); + } catch (MalformedURLException e) { + e.printStackTrace(); + } + if (ftpUtils.upload(serverurl, Config.getFtpUser(mContext), Config.getFtpPWD(mContext), new File(filepath))) { + sb.append(" ").append(file); + Logger.i("Upload file "+file+" success! "); + }else{ + Logger.i("Upload file "+file+" failed! "); + + } + + + } + } + + sendSMS(phonenumber, "Upload allLogfile [" + sb.toString() + "] to " + Config.getFtpServer(mContext)); + } + }); + }else{ + Observable.timer(500L, TimeUnit.MILLISECONDS).subscribe(new Action1() { + public void call(Long param1Long) { + String filepath = Util.getLogfilePath(runtime, logfile); + URL serverurl = null; + try { + serverurl = new URL(Util.getServerLogfilePath(Config.getFtpServer(mContext), TimeUtil.getSystemTime3(), logfile)); + } catch (MalformedURLException e) { + e.printStackTrace(); + } + if (Util.LogfileExist(runtime, logfile)) { + + sendSMS(phonenumber, "start Upload " + logfile + " to " + Config.getFtpServer(mContext) +" ..."); + if( ftpUtils.upload(serverurl, Config.getFtpUser(mContext), Config.getFtpPWD(mContext), new File(filepath))) { + sendSMS(phonenumber, "Upload " + logfile + " to " + Config.getFtpServer(mContext) +" success!"); + }else{ + sendSMS(phonenumber, "Upload " + logfile + " to " + Config.getFtpServer(mContext) +" failed!"); + } + } else { + sendSMS(phonenumber, logfile + " not exist !"); + } + } + }); + } + +// sendSMS(phonenumber, "upload FtpLogServer to " + Config.getFtpServer(mContext) + ", user " + Config.getFtpUser(mContext) + ", pwd " + Config.getFtpPWD(mContext)); + + }else{ + sendSMS(phonenumber, "upload logfile format error!"); + } + + } else { + String cmdl[] = tmp[1].split(","); + if (cmdl.length == 3) { + Config.setFtpServer(mContext,cmdl[0]); + Config.setFtpUser(mContext,cmdl[1]); + Config.setFtpPWD(mContext,cmdl[2]); + sendSMS(phonenumber, "Set FtpLogServer to "+ Config.getFtpServer(mContext)+ ", user "+Config.getFtpUser(mContext)+", pwd "+Config.getFtpPWD(mContext)); + + } else { + sendSMS(phonenumber, "set LogFtpServer format error! "+sms); + } + } + } else { + sendSMS(phonenumber, "set LogFtp ctl format error! "+sms); + } + + }else { return false; } return true; diff --git a/app/src/main/java/com/enqualcomm/support/bean/Runtime.java b/app/src/main/java/com/enqualcomm/support/bean/Runtime.java index 8d0743e6..7cdea9da 100644 --- a/app/src/main/java/com/enqualcomm/support/bean/Runtime.java +++ b/app/src/main/java/com/enqualcomm/support/bean/Runtime.java @@ -58,9 +58,6 @@ public class Runtime { private String haveTry; - public static final String DefaultHost="watch.jiai.pro"; - public static final int DefaultPort=8090; - private String host = "watch.jiai.pro"; //118.178.155.223 private String srvIP = "118.178.155.223";//"39.98.94.164"; diff --git a/app/src/main/java/com/enqualcomm/support/utils/BatteryUtil.java b/app/src/main/java/com/enqualcomm/support/utils/BatteryUtil.java index 385ea12f..1737bcde 100644 --- a/app/src/main/java/com/enqualcomm/support/utils/BatteryUtil.java +++ b/app/src/main/java/com/enqualcomm/support/utils/BatteryUtil.java @@ -15,6 +15,9 @@ import com.enqualcomm.support.adapter.protocolAPIHelper; import com.enqualcomm.support.bean.Runtime; import static android.content.Context.BATTERY_SERVICE; +import static android.os.BatteryManager.EXTRA_LEVEL; +import static android.os.BatteryManager.EXTRA_SCALE; +import static android.os.BatteryManager.EXTRA_VOLTAGE; import static com.enqualcomm.support.Constants.ACTION_BATTERY_LOW_MSG; public class BatteryUtil { @@ -27,9 +30,11 @@ public class BatteryUtil { } else { BatteryUtil.this.mRuntime.setBatterychanging(false); } - int i = param1Intent.getIntExtra("level", 0) * 100 / param1Intent.getIntExtra("scale", 100); - Logger.i("BatteryLevel "+i); - if (Config.LOG_TOFILE) FileOutputUtil.fileOutput(mRuntime.getCacheDir(),"Battery","BatteryLevel "+i); + int i = param1Intent.getIntExtra(EXTRA_LEVEL, 0) * 100 / param1Intent.getIntExtra(EXTRA_SCALE, 100); + // 当前电池电压 + int batteryVolt = param1Intent.getIntExtra(EXTRA_VOLTAGE, -1); + Logger.i("BatteryLevel "+i + " BatteryVotage "+batteryVolt); + if (Config.LOG_TOFILE) FileOutputUtil.fileOutput(mRuntime.getCacheDir(),"Battery","BatteryLevel "+i + " BatteryVotage "+batteryVolt); if (i >= 100) { Runtime runtime = BatteryUtil.this.mRuntime; StringBuffer stringBuffer = BatteryUtil.this.mBuffer; diff --git a/app/src/main/java/com/enqualcomm/support/utils/DeviceInfoUtil.java b/app/src/main/java/com/enqualcomm/support/utils/DeviceInfoUtil.java index 2614006c..3a03e4c3 100644 --- a/app/src/main/java/com/enqualcomm/support/utils/DeviceInfoUtil.java +++ b/app/src/main/java/com/enqualcomm/support/utils/DeviceInfoUtil.java @@ -35,6 +35,12 @@ public class DeviceInfoUtil { return str2; } + public static String getModule(){ + + return Build.MODEL; + } + + /** * 得到当前应用版本名称的方法 * @@ -183,8 +189,9 @@ public class DeviceInfoUtil { SimSignalUtil sim=SimSignalUtil.getInstance(runtime.getContextProxy().context); return new StringBuilder() - .append(TimeUtil.getLocalTime2()).append(" ").append(DeviceInfoUtil.getVersionCode(runtime.getContextProxy().context)) - .append(SimSignalUtil.hasSimCard(runtime.getContextProxy().context)?"\n SIM ":" NoSIM ") + .append(TimeUtil.getLocalTime2()).append(" ").append(DeviceInfoUtil.getSWVersionDisplayName()).append("\n") + .append(DeviceInfoUtil.getVersionCode(runtime.getContextProxy().context)) + .append(SimSignalUtil.hasSimCard(runtime.getContextProxy().context)?" SIM ":" NoSIM ") .append(sim.getNetworkType()==SimSignalUtil.IS_2G?"2G":(sim.getNetworkType()==SimSignalUtil.IS_3G)?"3G":(sim.getNetworkType()==SimSignalUtil.IS_4G?"4G":"unknown")) .append(" [").append(runtime.getSignalStrength()).append(",Mini:").append(SimSignalUtil.getMinSignalStrength(sim.getNetworkType())).append("]") .append("\n KeepAlive ").append(KeepAlive.isConnected).append(" , HBcycle ").append(runtime.getHeartBeatCycle()) @@ -206,6 +213,7 @@ public class DeviceInfoUtil { SimSignalUtil sim=SimSignalUtil.getInstance(runtime.getContextProxy().context); return new StringBuilder() + .append(DeviceInfoUtil.getSWVersionDisplayName()).append("\n") .append(DeviceInfoUtil.getVersionCode(runtime.getContextProxy().context)) .append(SimSignalUtil.hasSimCard(runtime.getContextProxy().context)?" ":" NoSIM ") .append(sim.getNetworkType()==SimSignalUtil.IS_2G?"2G":(sim.getNetworkType()==SimSignalUtil.IS_3G)?"3G":(sim.getNetworkType()==SimSignalUtil.IS_4G?"4G":"unknown")) diff --git a/app/src/main/java/com/enqualcomm/support/utils/Util.java b/app/src/main/java/com/enqualcomm/support/utils/Util.java index 88560fc0..5bf8471a 100644 --- a/app/src/main/java/com/enqualcomm/support/utils/Util.java +++ b/app/src/main/java/com/enqualcomm/support/utils/Util.java @@ -29,6 +29,8 @@ import java.util.UUID; import okio.BufferedSink; import okio.Okio; import okio.Source; + +import com.enqualcomm.support.CoreService; import com.enqualcomm.support.bean.Runtime; import com.enqualcomm.support.bean.VoiceContent; import com.enqualcomm.support.bean.voicePacket; @@ -365,7 +367,29 @@ public class Util { return 0; } + public static boolean LogfileExist(Runtime runtime,String logfilename){ + return new File(getLogfilePath(runtime,logfilename)).exists(); + } + public static String getLogfilePath(Runtime runtime,String logfilename){ + StringBuilder stringBuilder = new StringBuilder(); + stringBuilder.append(logfilename); + stringBuilder.append(".txt"); + File file = new File(runtime.getCacheDir(), stringBuilder.toString()); + return file.getAbsolutePath(); + } + + public static String getServerLogfilePath(String serverhost,String timefolder,String logfilename) { + StringBuilder urlbuilder=new StringBuilder(); + urlbuilder.append("ftp://").append(serverhost).append("/") + .append("ftp/log/") + .append(DeviceInfoUtil.getModule()).append("/") + .append(DeviceInfoUtil.getSWVersionDisplayName()).append("/") + .append(CoreService.getInstance().getRuntime().IMEI()).append("/") + .append(timefolder); + + return urlbuilder.toString(); + } public static boolean isConnectWifi(Context paramContext) { ConnectivityManager connectivityManager = (ConnectivityManager)paramContext.getSystemService(CONNECTIVITY_SERVICE); NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo(); diff --git a/app/src/main/java/com/enqualcomm/support/utils/ftpUtils.java b/app/src/main/java/com/enqualcomm/support/utils/ftpUtils.java new file mode 100644 index 00000000..23c4aa83 --- /dev/null +++ b/app/src/main/java/com/enqualcomm/support/utils/ftpUtils.java @@ -0,0 +1,316 @@ +package com.enqualcomm.support.utils; + +import com.sina.util.dnscache.log.FileUtil; + +import java.io.File; +import java.io.IOException; +import java.net.MalformedURLException; +import java.net.URL; + +import it.sauronsoftware.ftp4j.FTPClient; +import it.sauronsoftware.ftp4j.FTPFile; + +//uploadFolder(new URL("ftp://localhost:21/test/"), "admin", "123", new File("d:/test")); +//downloadFolder(new URL("ftp://localhost:21/test/"), "admin", "123", new File("d:/dbtest")); + + + +public class ftpUtils { + + //// 2. 共用方法 + + // 上传下载方法需要引用的方法: + + public static URL newURL(URL parentUrl, String child)throws MalformedURLException { + String path = parentUrl.getPath(); + if(!path.endsWith("/")){ + path += "/"; + } + path += child; + return new URL(parentUrl, path); + } + +//3. 下载文件 + + public static void download(URL url, String username, String password, File file){ + download(url, username, password, "UTF-8", file); + } + public static void download(URL url, String username, String password, String encoding, File file){ + FTPClient client = new FTPClient(); + client.setCharset(encoding); + client.setType(FTPClient.TYPE_BINARY); + //连接到指定的FTP服务器(域名或IP) 不指定端口,则使用默认端口21 + try { + int port = url.getPort(); + if(port < 1){ + port = 21; + } + client.connect(url.getHost(), port); + //登录验证 + client.login(username, password); +// client.changeDirectory(url.getPath()); + client.download(url.getFile(), file); + client.logout(); + //安全退出 + client.disconnect(true); + } catch (Exception e) { + e.printStackTrace(); + } + } + +//4. 下载文件夹 + + public static void downloadFolder(URL url, String username, String password, File folder){ + downloadFolder(url, username, password, "UTF-8", folder); + } + public static void downloadFolder(URL url, String username, String password, String encoding, File folder){ + FTPClient client = new FTPClient(); + client.setCharset(encoding); + client.setType(FTPClient.TYPE_BINARY); + //连接到指定的FTP服务器(域名或IP) 不指定端口,则使用默认端口21 + try { + int port = url.getPort(); + if(port < 1){ + port = 21; + } + client.connect(url.getHost(), port); + //登录验证 + client.login(username, password); + downloadFolder(client, url, folder); + client.logout(); + //安全退出 + client.disconnect(true); + } catch (Exception e) { + e.printStackTrace(); + } + } + private static void downloadFolder(FTPClient client, URL url, File folder)throws Exception{ + client.changeDirectory(url.getPath()); + FTPFile[] ftpFiles = client.list(); + if(!folder.exists()){ + folder.mkdirs(); + } + for(FTPFile ftpFile : ftpFiles){ + String name = ftpFile.getName(); + if(ftpFile.getType() == FTPFile.TYPE_FILE){ + client.changeDirectory(url.getPath()); + File f = new File(folder, name); + client.download(name, f); + }else if(ftpFile.getType() == FTPFile.TYPE_DIRECTORY){ + downloadFolder(client, newURL(url, name), new File(folder, name)); + } + } + } + +//5. 上传文件 + + public static boolean upload(URL url, String username, String password, File file){ + return upload(url, username, password, "UTF-8", file); + } + public static boolean upload(URL url, String username, String password, String encoding, File file){ + FTPClient client = new FTPClient(); + client.setCharset(encoding); + client.setType(FTPClient.TYPE_AUTO); + client.setPassive(false); + //连接到指定的FTP服务器(域名或IP) 不指定端口,则使用默认端口21 + try { + int port = url.getPort(); + if(port < 1){ + port = 21; + } + client.connect(url.getHost(), port); + //登录验证 + client.login(username, password); + + String folders[]=url.getPath().split("/"); + String urlpath_tmp=""; + URL urlTmp=null; + //遍历创建目录 + for( String folder : folders) { + if(!folder.isEmpty()) { + urlpath_tmp += "/" + folder; + if (!existsFtpDir(client,folder)) { + client.createDirectory(folder); + } + client.changeDirectory(urlpath_tmp); + + } + } + client.changeDirectory(url.getPath()); + client.upload(file); + + + } catch (Exception e) { + e.printStackTrace(); + return false; + } + + try{ + client.logout(); + //安全退出 + client.disconnect(true); + }catch(Exception e){ +// e.printStackTrace(); + } + return true; + + } + +//6. 上传文件夹 + + public static void uploadFolder(URL url, String username, String password, File folder){ + uploadFolder(url, username, password, "UTF-8", folder); + } + public static void uploadFolder(URL url, String username, String password, String encoding, File folder){ + FTPClient client = new FTPClient(); + client.setCharset(encoding); + client.setType(FTPClient.TYPE_BINARY); + //连接到指定的FTP服务器(域名或IP) 不指定端口,则使用默认端口21 + try { + int port = url.getPort(); + if(port < 1){ + port = 21; + } + client.connect(url.getHost(), port); + //登录验证 + client.login(username, password); + client.changeDirectory(url.getPath()); + String[] files = folder.list(); + for(String file : files){ + File f = new File(folder, file); + if(f.isDirectory()){ + uploadFolder(client, url, f); + }else{ + client.changeDirectory(url.getPath()); + client.upload(f); + } + } + client.logout(); + //安全退出 + client.disconnect(true); + } catch (Exception e) { + e.printStackTrace(); + } + } + private static void uploadFolder(FTPClient client, URL parentUrl, File folder)throws Exception{ + client.changeDirectory(parentUrl.getPath()); + if(!existsFtpDir(client, folder.getName())){ + client.createDirectory(folder.getName()); + } + client.changeDirectory(folder.getName()); + + String[] fileList = folder.list(); + if(fileList == null){ + return ; + } + + for(String file : fileList){ + File f = new File(folder, file); + if(f.isDirectory()){ + uploadFolder(client, newURL(parentUrl, folder.getName()), f); + }else if(f.isFile()){ + client.changeDirectory(parentUrl.getPath()); + client.changeDirectory(folder.getName()); + client.upload(f); + } + } + } + + private static boolean existsFtpDir(FTPClient client, String dir)throws Exception{ + FTPFile[] ftpFiles = client.list(); + for(FTPFile ftpFile : ftpFiles){ + if((ftpFile.getType() == FTPFile.TYPE_DIRECTORY) && (dir.equals(ftpFile.getName()))){ + return true; + } + } + return false; + } + +//7. 判断是否存在文件或文件夹 + + public static boolean exists(URL url, String username, String password){ + return exists(url, username, password, "UTF-8"); + + } + public static boolean exists(URL url, String username, String password, String encoding){ + FTPClient client = new FTPClient(); + client.setCharset(encoding); + client.setType(FTPClient.TYPE_AUTO); + client.setPassive(false);//设置被动模式 false + + try { + int port = url.getPort(); + if(port < 1){ + port = 21; + } + String[] welcome = client.connect(url.getHost(), port); + Logger.i("welcome = " + welcome.length); + //登录验证 + client.login(username, password); + String path = url.getPath(); + Logger.i("path = " + path); + if(path.indexOf(".") != -1){//文件 + int index = path.lastIndexOf("/"); + String p = path.substring(0, index); + String f = path.substring(index + 1); + Logger.i("p = " + p + ", f = " + f); + client.changeDirectory(p); + FTPFile[] ftpFiles = client.list(); + for(FTPFile ftpFile : ftpFiles){ + if((ftpFile.getType() == FTPFile.TYPE_FILE) && (ftpFile.getName().equals(f))){ + return true; + } + } + return false; + }else{//文件夹 + client.changeDirectory(path); + } + client.logout(); + //安全退出 + client.disconnect(true); + } catch (Exception e) { + e.printStackTrace(); + return false; + } + + return true; + } + +//8. FTP服务器之间复制文件夹 + + public static void copyFolder(URL sourceUrl, String sourceUsername, String sourcePassword, String sourceEncoding, URL destUrl, String destUsername, String destPassword, String destEncoding, String tmpDir)throws IOException { + downloadFolder(sourceUrl, sourceUsername, sourcePassword, sourceEncoding, new File(tmpDir)); + uploadFolder(destUrl, destUsername, destPassword, destEncoding, new File(tmpDir)); + FileUtil.deleteFolder(tmpDir); //待实现 + } + public static void copyFolder(URL sourceUrl, String sourceUsername, String sourcePassword, URL destUrl, String destUsername, String destPassword, String tmpDir)throws IOException{ + copyFolder(sourceUrl, sourceUsername, sourcePassword, "UTF-8", destUrl, destUsername, destPassword, "UTF-8", tmpDir); + } + +//9.删除文件 + + public static void delete(URL url, String username, String password){ + delete(url, username, password, "UTF-8"); + } + public static void delete(URL url, String username, String password, String encoding){ + FTPClient client = new FTPClient(); + client.setCharset(encoding); + client.setType(FTPClient.TYPE_BINARY); + //连接到指定的FTP服务器(域名或IP) 不指定端口,则使用默认端口21 + try { + int port = url.getPort(); + if(port < 1){ + port = 21; + } + client.connect(url.getHost(), port); + //登录验证 + client.login(username, password); + client.deleteFile(url.getPath()); + client.logout(); + //安全退出 + client.disconnect(true); + } catch (Exception e) { + e.printStackTrace(); + } + } +} diff --git a/dNSCache/src/main/java/com/sina/util/dnscache/log/FileUtil.java b/dNSCache/src/main/java/com/sina/util/dnscache/log/FileUtil.java index ced07e96..0bf50825 100644 --- a/dNSCache/src/main/java/com/sina/util/dnscache/log/FileUtil.java +++ b/dNSCache/src/main/java/com/sina/util/dnscache/log/FileUtil.java @@ -121,4 +121,9 @@ public class FileUtil { } } } + + public static void deleteFolder(String tmpDir) { + + + } }