我遵循了网上找到的Mina Apache身份验证教程,当我让我的Android应用程序启动服务器时,它可以识别试图登录的用户,但是即使密码正确,它也给我提供了不正确的密码 users.properties文件。
我尝试遵循此处找到的实现: https://www.cnblogs.com/happyxiaoyu02/archive/2013/04/15/6818957.html 并阅读apache网站上的文档。
公共类FtpServerActivity扩展了活动{
private FtpServer mFtpServer;
private String ftpConfigDir= Environment.getExternalStorageDirectory().getAbsolutePath()+"/ftpConfig/";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
TextView tv=(TextView)findViewById(R.id.tvText);
String info="请通过浏览器或者我的电脑访问以下地址\n"+"ftp://"+getLocalIpAddress()+":2221\n";
tv.setText(info);
File f=new File(ftpConfigDir);
if(!f.exists())
f.mkdir();
copyResourceFile(R.raw.users, ftpConfigDir+"users.properties");
Config1();
}
public String getLocalIpAddress() {
String strIP=null;
try {
for (Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); en.hasMoreElements();) {
NetworkInterface intf = en.nextElement();
for (Enumeration<InetAddress> enumIpAddr = intf.getInetAddresses(); enumIpAddr.hasMoreElements();) {
InetAddress inetAddress = enumIpAddr.nextElement();
if (!inetAddress.isLoopbackAddress()) {
strIP= inetAddress.getHostAddress().toString();
}
}
}
} catch (SocketException ex) {
Log.e("msg", ex.toString());
}
return strIP;
}
private void copyResourceFile(int rid, String targetFile){
InputStream fin = ((Context)this).getResources().openRawResource(rid);
FileOutputStream fos=null;
int length;
try {
fos = new FileOutputStream(targetFile);
byte[] buffer = new byte[1024];
while( (length = fin.read(buffer)) != -1){
fos.write(buffer,0,length);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally{
if(fin!=null){
try {
fin.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(fos!=null){
try {
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
void Config1(){
FtpServerFactory serverFactory = new FtpServerFactory();
ListenerFactory factory = new ListenerFactory();
PropertiesUserManagerFactory userManagerFactory = new PropertiesUserManagerFactory();
String[] str ={"mkdir", ftpConfigDir};
try {
Process ps = Runtime.getRuntime().exec(str);
try {
ps.waitFor();
} catch (InterruptedException e) {
e.printStackTrace();
}
}catch (IOException e) {
e.printStackTrace();
}
String filename=ftpConfigDir+"users.properties";//"/sdcard/users.properties";
File files=new File(filename);
userManagerFactory.setFile(files);
serverFactory.setUserManager(userManagerFactory.createUserManager());
// set the port of the listener
factory.setPort(2221);
// replace the default listener
serverFactory.addListener("default", factory.createListener());
// start the server
FtpServer server = serverFactory.createServer();
this.mFtpServer = server;
try {
server.start();
} catch (FtpException e) {
e.printStackTrace();
}
}
void Config2(){
FtpServerFactory serverFactory = new FtpServerFactory();
ListenerFactory factory = new ListenerFactory();
// set the port of the listener
factory.setPort(2221);
// define SSL configuration
SslConfigurationFactory ssl = new SslConfigurationFactory();
ssl.setKeystoreFile(new File(ftpConfigDir+"ftpserver.jks"));
ssl.setKeystorePassword("password");
// set the SSL configuration for the listener
factory.setSslConfiguration(ssl.createSslConfiguration());
factory.setImplicitSsl(true);
// replace the default listener
serverFactory.addListener("default", factory.createListener());
PropertiesUserManagerFactory userManagerFactory = new PropertiesUserManagerFactory();
userManagerFactory.setFile(new File(ftpConfigDir+"users.properties"));
serverFactory.setUserManager(userManagerFactory.createUserManager());
// start the server
FtpServer server = serverFactory.createServer();
this.mFtpServer = server;
try {
server.start();
} catch (FtpException e) {
e.printStackTrace();
}
}
@Override
protected void onDestroy() {
super.onDestroy();
if(null != mFtpServer) {
mFtpServer.stop();
mFtpServer = null;
}
}
}
它确实在sdcard的目录中创建了users.properties文件。我尝试手动更改文件中的用户名和密码,但在尝试连接时始终会返回530身份验证失败。 这是我在家里运行旧的kitkat 4.4操作系统的小型android物联网设备。