android-ftp-上传日志

本文介绍如何使用Android设备上传文件到FTP服务器,并提供了一个具体的实现案例。包括申请FTP空间、编写上传代码及调用方法。
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;
 }

3. 使用
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);
   }
  });
 }






评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值