package com.hbtx.ftpupload;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPReply;
import android.app.Activity;
import android.os.Bundle;
import android.os.Environment;
import android.view.Menu;
import android.view.View;
public class FtpActivity extends Activity {
private String url = "192.168.1.79";
private String port = 21 + "";
private String username = "hbtx"; // 登录名
private String password = "test";
private String remotePath = "//ftp";// 上到ftp服务器的磁盘路径
private String fileNamePath;/*
* ="/mnt/sdcard/";//要上传的文件路径
*/
private String fileName = "kuwo_catalog_log.txt";// 要上传的文件名
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_ftp);
fileNamePath = Environment.getExternalStorageDirectory()
.getAbsolutePath() + "/";
System.out.println(fileNamePath);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.ftp, menu);
return true;
}
public void upLoad(View view) {
new Thread() {
public void run() {
ftpUpload(url, port, username, password, remotePath,
fileNamePath, fileName);
};
}.start();
}
/**
* 通过ftp上传文件
*
* @param url
* ftp服务器地址 如: 192.168.1.110
* @param port
* 端口如 : 21
* @param username
* 登录名
* @param password
* 密码
* @param remotePath
* 上到ftp服务器的磁盘路径
* @param fileNamePath
* 要上传的文件路径
* @param fileName
* 要上传的文件名
* @return
*/
public String ftpUpload(String url, String port, String username,
String password, String remotePath, String fileNamePath,
String fileName) {
FTPClient ftpClient = new FTPClient();
FileInputStream fis = null;
String returnMessage = "0";
try {
ftpClient.connect(url, Integer.parseInt(port));
boolean loginResult = ftpClient.login(username, password);
int returnCode = ftpClient.getReplyCode();
if (loginResult && FTPReply.isPositiveCompletion(returnCode)) {// 如果登录成功
ftpClient.makeDirectory(remotePath);
// 设置上传目录
ftpClient.changeWorkingDirectory(remotePath);
ftpClient.setBufferSize(1024);
ftpClient.setControlEncoding("UTF-8");
boolean type = ftpClient
.setFileType(org.apache.commons.net.ftp.FTP.BINARY_FILE_TYPE);
System.out.println(type);
// 使用被动模式设为默认
ftpClient.enterLocalPassiveMode();
// 设置模式
ftpClient.setFileTransferMode(org.apache.commons.net.ftp.FTP.STREAM_TRANSFER_MODE);
fis = new FileInputStream(fileNamePath + fileName);
ftpClient.storeFile(fileName, fis);
returnMessage = "1"; // 上传成功
} else {// 如果登录失败
returnMessage = "0";
}
} catch (IOException e) {
e.printStackTrace();
throw new RuntimeException("FTP客户端出错!", e);
} finally {
// IOUtils.closeQuietly(fis);
try {
fis.close();
ftpClient.disconnect();
} catch (IOException e) {
e.printStackTrace();
throw new RuntimeException("关闭FTP连接发生异常!", e);
}
}
return returnMessage;
}
}
首先用到了一款ftp服务器:
然后就是配置ftp服务器,以前没有用过,摸索调试了两天才弄好!
直接上图:
然后
注意权限:
最重要的是这个:
全部都勾选上,不然后会出现一下错误
:
注意:查看ftp日志在这里查看:
配置基本就是这些。
下面是android代码: