• 全部
  • 音视频
  • Camera
  • UI
  • SDK
  • Android Studio
  • 问答

Android上面部署Apache FTPServer在电脑上访问目录为空

Eden_sion2019 2021-05-20 06:10:45
package example.lesson1.transfer;

import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.core.app.RemoteInput;

import android.Manifest;
import android.content.Context;
import android.content.IntentFilter;
import android.content.pm.PackageManager;
import android.content.res.Resources;
import android.content.res.loader.ResourcesLoader;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.os.Bundle;
import android.os.Environment;
import android.os.Looper;
import android.widget.TextView;
import android.widget.Toast;

import org.apache.commons.net.ftp.FTPClientConfig;
import org.apache.ftpserver.FtpServer;
import org.apache.ftpserver.FtpServerFactory;
import org.apache.ftpserver.ftplet.Authority;
import org.apache.ftpserver.ftplet.DefaultFtplet;
import org.apache.ftpserver.ftplet.FileSystemFactory;
import org.apache.ftpserver.ftplet.FileSystemView;
import org.apache.ftpserver.ftplet.FtpException;
import org.apache.ftpserver.ftplet.FtpReply;
import org.apache.ftpserver.ftplet.FtpRequest;
import org.apache.ftpserver.ftplet.FtpSession;
import org.apache.ftpserver.ftplet.Ftplet;
import org.apache.ftpserver.ftplet.FtpletResult;
import org.apache.ftpserver.ftplet.User;
import org.apache.ftpserver.ftplet.UserManager;
import org.apache.ftpserver.listener.ListenerFactory;
import org.apache.ftpserver.usermanager.PropertiesUserManagerFactory;
import org.apache.ftpserver.usermanager.impl.BaseUser;
import org.apache.ftpserver.usermanager.impl.WritePermission;
import org.apache.log4j.lf5.util.Resource;

import java.io.FileOutputStream;
import java.io.IOException;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class MainActivity extends AppCompatActivity {
private FtpServer mFtpServer = null;
private String intToIp(int i) {

return (i & 0xFF ) + "." +
((i >> 8 ) & 0xFF) + "." +
((i >> 16 ) & 0xFF) + "." +
( i >> 24 & 0xFF) ;
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// //获取wifi服务
// WifiManager wifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE);
// //判断wifi是否开启
// if (!wifiManager.isWifiEnabled()) {
// wifiManager.setWifiEnabled(true);
// }
// WifiInfo wifiInfo = wifiManager.getConnectionInfo();
// int ipAddress = wifiInfo.getIpAddress();
// String ip = intToIp(ipAddress);
// Toast.makeText(MainActivity.this, ip,Toast.LENGTH_SHORT).show();
// new Thread(new Runnable() {
// @Override
// public void run() {
//
// try {
// Looper.prepare();
// InetAddress.getLocalHost().getHostAddress();
// Toast.makeText(MainActivity.this, InetAddress.getLocalHost().getHostAddress(), Toast.LENGTH_SHORT).show();
Toast.makeText(MainActivity.this, getLocalIpAddress(), Toast.LENGTH_SHORT).show();
// Looper.loop();
// } catch (UnknownHostException e) {
// e.printStackTrace();
// }
// }
// }).start();
try {
startFtpServer(getLocalIpAddress());
} catch (IOException e) {
e.printStackTrace();
}
}
public String getLocalIpAddress(){
try {
List<NetworkInterface> interfaces = Collections.list(NetworkInterface.getNetworkInterfaces());
for(NetworkInterface networkInterface : interfaces) {
List<InetAddress> addresses = Collections.list(networkInterface.getInetAddresses());

for (InetAddress address : addresses){
if(!address.isLoopbackAddress()){
String strAddress = address.getHostAddress().toUpperCase();
boolean isIPv4 = Isipv4(strAddress);
if(isIPv4) return strAddress;
}
}
}
}catch (Exception e){
e.printStackTrace();
}
return null;
}

private boolean Isipv4(String ipv4){
//字符串为空或为空串
if(ipv4 == null || ipv4.length() == 0) return false;
//正则分割
String[] parts = ipv4.split("\\.");
if(parts.length != 4) return false;
for(String part : parts){
try{
int n = Integer.parseInt(part);
//不在正确范围内
if(n < 0||n > 255) return false;
}catch(NumberFormatException e) {
return false;
}
}
return true;
}

private void startFtpServer(String hostip) throws IOException {
//判断SD卡是否可用
if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
//判断用户授权
if (ActivityCompat.checkSelfPermission(MainActivity.this, Manifest.permission.WRITE_EXTERNAL_STORAGE)
!= PackageManager.PERMISSION_GRANTED) {
//请求授权
ActivityCompat.requestPermissions(MainActivity.this,
new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},
1);
}
if (ActivityCompat.checkSelfPermission(MainActivity.this, Manifest.permission.WRITE_EXTERNAL_STORAGE)
== PackageManager.PERMISSION_GRANTED) {
FtpServerFactory serverFactory = new FtpServerFactory();
ListenerFactory factory = new ListenerFactory();
// serverFactory.addListener("default", factory.createListener());
//设置ftp端口和主机IP
factory.setPort(2222);
factory.setServerAddress(hostip);

serverFactory.addListener("default", factory.createListener());
// serverFactory.addListener("default", factory.createListener());
//设置用户名和密码
BaseUser user = new BaseUser();
user.setName("admin");
user.setPassword("12345");
user.setEnabled(true);
user.setMaxIdleTime(3000);
// user.setHomeDirectory("/mnt/sdcard/");
// user.setHomeDirectory(Environment.getExternalStorageDirectory().getAbsolutePath());

user.setHomeDirectory("/sdcard\r\n");

// user.setHomeDirectory("/mnt/sdcard/");
// user.setHomeDirectory("/data/user/0/");
List<Authority> authorities = new ArrayList<>();
authorities.add(new WritePermission());

user.setAuthorities(authorities);
try {
serverFactory.getUserManager().save(user);
} catch (FtpException e) {
e.printStackTrace();
}
// serverFactory.addListener("default", factory.createListener());
if (mFtpServer != null) {
mFtpServer.stop();
}
//启动服务器
mFtpServer = serverFactory.createServer();
try {
mFtpServer.start();
TextView text = findViewById(R.id.text);
text.setText("服务器启动于:"+hostip+":2121");
} catch (FtpException e) {
e.printStackTrace();
}
} else {
Toast.makeText(MainActivity.this, "SD卡不可用!", Toast.LENGTH_SHORT).show();
}

}
}
private void stoptpServer(){
if(mFtpServer != null){
mFtpServer.stop();
mFtpServer = null;
}
}


已修改为主动模式,但还是不行,求助
...全文
161 点赞 收藏 2
写回复
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
回复
结题:少加了android:requestLegacyExternalStorage="true"
回复
相关推荐
发帖
Android
创建于2009-10-09

7.8w+

社区成员

移动平台 Android
申请成为版主
帖子事件
创建了帖子
2021-05-20 06:10
社区公告
暂无公告