我想上传位于内部存储的蓝牙文件夹中的文件。我不会出现什么问题,但我的代码不起作用。这里我没有添加NetworkAvailability类,因为class只是在用户连接到Internet时发送广播,这里不需要它。其余的代码如下,请查看并帮我找到这里出了什么问题....
import android.app.Service;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.os.IBinder;
import android.widget.Toast;
import java.io.File;
import it.sauronsoftware.ftp4j.FTPClient;
public class uploadData extends Service {
private NetworkAvailability networkAvailability;
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
super.onStartCommand(intent, flags, startId);
networkAvailability = NetworkAvailability.getInstance();
networkAvailability.registerNetworkAvailability(this, receiver);
return START_STICKY;
}
private BroadcastReceiver receiver = new BroadcastReceiver() {
File file;
@Override
public void onReceive(final Context context, Intent intent) {
if (intent.getBooleanExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY, false)) {
Toast.makeText(getApplicationContext(), "network is not available", Toast.LENGTH_LONG).show();
} else {
Toast.makeText(getApplicationContext(), "network is available", Toast.LENGTH_LONG).show();
try {
String path = "/storage/emulated/0/bluetooth/f.txt";
file = new File(path);
if (!file.exists()) { Toast.makeText(context, "File not
found", Toast.LENGTH_LONG).show();
}
new uploadFTP().execute(file);
} catch (Exception ex) {
Toast.makeText(getApplicationContext(), ex.toString(),
Toast.LENGTH_LONG).show();
}
}
}
};
@Override
public void onDestroy() {
networkAvailability.unregisterNetworkAvailability(this, receiver);
super.onDestroy();
}
@Override
public IBinder onBind(Intent intent) {
return null;
}
}
每次我连接到互联网时,它都可以使用网络,但文件没有上传。 这是FTP类:
import android.os.AsyncTask;
import it.sauronsoftware.ftp4j.FTPClient;
import java.io.File;
public class uploadFTP extends AsyncTask<File, Void, Void> {
@Override
protected Void doInBackground(File... uploadfile) {
String FTP_HOST = "31.170.167.199";
String FTP_USER = "u2821*****"; // Username Hidden
String FTP_PASS = "**********"; //Password Hidden
FTPClient client = new FTPClient();
try {
client.connect(FTP_HOST,21);
client.login(FTP_USER, FTP_PASS);
client.setType(FTPClient.TYPE_BINARY);
client.upload(uploadfile[0]);
} catch (Exception e) {
e.printStackTrace();
try {
client.disconnect(true);
} catch (Exception e2) {
e2.printStackTrace();
}
}
return null;
}
}
我还想问一下,有没有任何方法可以直接访问内部存储,就像有外部存储一样 存储(Environment.getExternalStorageDirectory()。getAbsolutePath())。