1. 先申请ftp空间
申请地址:
http://free.3v.do/
申请后ftp信息
用户名:aizizaide,
密码:xxxxxxx(保密。。)
ftp地址:
004.3vftp.com
ftp端口:21
2. Android FTP util
private static String TAG = FTP.class.getSimpleName();
/**
* 通过ftp上传文件
*
* @param hostname
* ftp服务器地址 如: 192.168.1.110
* @param port
* 端口如 : 21
* @param username
* 登录名
* @param password
* 密码
* @param remoteDir
* 上到ftp服务器的磁盘路径 如/default-websit
* @param remoteFileName
* 上传到ftp服务器的文件的保存名称。
* @param locatFilePath
* 本地文件的绝对路径,如 /sdcard/123.txt
* @return
*/
public static String ftpUpload(String hostname, int port, String username,
String password, String remoteDir, String remoteFileName,
String locatFilePath) {
FTPClient ftpClient = new FTPClient();
FileInputStream fis = null;
String returnMessage = "0";
try {
ftpClient.connect(hostname, port);
boolean loginResult = ftpClient.login(username, password);
int returnCode = ftpClient.getReplyCode();
if (loginResult && FTPReply.isPositiveCompletion(returnCode)) {// 如果登录成功
Logcat.d(TAG, "@@@@ftp登录成功!!!");
ftpClient.makeDirectory(remoteDir);
// 设置上传目录
ftpClient.changeWorkingDirectory(remoteDir);
ftpClient.setBufferSize(1024);
ftpClient.setControlEncoding("UTF-8");
ftpClient.enterLocalPassiveMode();
fis = new FileInputStream(locatFilePath);
ftpClient.storeFile(remoteFileName, fis);
returnMessage = "文件上传成功"; // 上传成功
Logcat.d(TAG, "文件上传成功!!!");
} else {// 如果登录失败
Logcat.d(TAG, "ftp登录失败,请重试!");
returnMessage = "ftp登录失败,请重试!";
}
} catch (IOException e) {
e.printStackTrace();
Logcat.d(TAG, "FTP客户端出错!");
} finally {
try {
ftpClient.disconnect();
} catch (IOException e) {
e.printStackTrace();
Logcat.d(TAG, "关闭FTP连接发生异常!");
}
}
return returnMessage;
}
String name = savErrLog( msg);
uploadLog(name);
保存日志
/**
*
* 保存错误到文件,随后可以用ftp发送 最好在线程中执行
*
* @return 保存文件 的绝对路径
* @data 2015-12-11 下午1:30:29
*/
public static String savErrLog(String msg) {
CharSequence timestamp = DateFormat.format("yyyy-MM-dd_kk:mm:ss",
System.currentTimeMillis());
String name = Constant.PATH_LOGCAT + "/" + timestamp + ".log";
try {
FileOutputStream fos = new FileOutputStream(name);
fos.write(msg.getBytes());
fos.close();
} catch (Exception e) {
e.printStackTrace();
return "";
}
return name;
}
/**
* @param name
* 绝对路径
* @data 2015-12-11 下午1:36:32
*/
public static void uploadLog(final String name) {
SVTAppcation.mExecutorService.execute(new Runnable() {
@Override
public void run() {
String rName = name.substring(name.lastIndexOf("/") + 1);
FTP.ftpUpload("004.3vftp.com", 21, "aizizaide", <span style="font-family: 微软雅黑; font-size: 14px; line-height: 21px;">xxxxxxx</span>,
"/default-websit", rName, name);
}
});
}