package donglisiwei.bingyuan.cai;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import org.apache.commons.net.ftp.FTPFile;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.os.StrictMode;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;
public class ServiceList extends Activity {
private String hostName;
private String userName;
private String password;
private TextView showServiceInfo = null;// 显示主机名
private List<FTPFile> remoteFile;// FTP服务器的文件
// private List<File> localFile;//本地的文件列表
private ListView fileList;// FTP服务器的文件列表
private FTP ftp;
private static final String LOCAL_PATH = "mnt/sdcard";
private String currentTouchFolder = "";// 当前点击的目录
private int position = 0;
private Button turnBack;
private ProgressBar progressBar;
@Override
public void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
fixVersion();// 支持4.0版本,2.3以下的不用调用这个函数
super.onCreate(savedInstanceState);
setContentView(R.layout.servicelist);
showServiceInfo = (TextView) findViewById(R.id.myTextView1);
fileList = (ListView) findViewById(R.id.list);
turnBack = (Button) findViewById(R.id.turnback);
progressBar = (ProgressBar) findViewById(R.id.progressBar2);
progressBar.setVisibility(View.INVISIBLE);
getLogInfo();// 获取主机名、用户名、登录密码
RemoteFilePath.remoteFilePath = FTP.REMOTE_PATH;// 切换目录时,RemoteFilePath.remoteFilePath存放绝对路径
loadRemoteView(RemoteFilePath.remoteFilePath);// 加载FTP服务器的文件目录到本地
fileList.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> arg0, View arg1,
int location, long arg3) {
Toast.makeText(ServiceList.this,
remoteFile.get(location).toString(), Toast.LENGTH_SHORT)
.show();
final int folder;
folder = location;
position = location;
// 选项是文件夹,弹出对话框,提示下载或者进入该文件夹
if (remoteFile.get(location).isDirectory()) {
AlertDialog.Builder builder = new Builder(ServiceList.this);
builder.setMessage("下载or进入?");
builder.setTitle("提示");
builder.setPositiveButton("取消",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int which) {
// TODO Auto-generated method stub
}
});
builder.setNegativeButton("进入",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int which) {
// TODO Auto-generated method stub
currentTouchFolder = remoteFile.get(folder)
.toString();
int str_position = currentTouchFolder
.indexOf("<DIR>") + 15;
currentTouchFolder = currentTouchFolder
.substring(str_position,
currentTouchFolder.length());
if (RemoteFilePath.remoteFilePath == "")
RemoteFilePath.remoteFilePath = RemoteFilePath.remoteFilePath
+ currentTouchFolder;
else
RemoteFilePath.remoteFilePath = RemoteFilePath.remoteFilePath
+ "/" + currentTouchFolder;
loadRemoteView(RemoteFilePath.remoteFilePath);
}
});
builder.setNeutralButton("下载",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int which) {
// TODO Auto-generated method stub
progressBar.setVisibility(View.VISIBLE);
downLoad();
}
});
builder.create().show();
} else {
// 如果是文件格式,弹出提示对话框,提示下载或者取消
AlertDialog.Builder builder = new Builder(ServiceList.this);
builder.setMessage("下载or取消?");
builder.setTitle("提示");
builder.setNegativeButton("下载",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int which) {
// TODO Auto-generated method stub
progressBar.setVisibility(View.VISIBLE);
downLoad();
}
});
builder.setNeutralButton("取消",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int which) {
// TODO Auto-generated method stub
}
});
builder.create().show();
}
}
});
// 返回上层目录
turnBack.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
String str = RemoteFilePath.remoteFilePath;
int index = 0;
while (str.indexOf("/") != -1) {
index = index + str.indexOf("/") + 1;
str = str.substring(str.indexOf("/") + 1, str.length());
System.out.println("str-->" + str);
}
if (index != 0){
RemoteFilePath.remoteFilePath = RemoteFilePath.remoteFilePath
.substring(0, index - 1);
}
else
RemoteFilePath.remoteFilePath = "";
System.out.println(RemoteFilePath.remoteFilePath);
loadRemoteView(RemoteFilePath.remoteFilePath);
}
});
}
/**
* 加载FTP服务器的文件目录到本地
*
* */
private void loadRemoteView(String remotePath) {
try {
if (ftp != null) {
ftp.closeConnect();
}
Log.e("hostName", hostName);
Log.e("userName", userName);
Log.e("passwrod", password);
ftp = new FTP(hostName, userName, password);
ftp.openConnect();
remoteFile = new ArrayList<FTPFile>();
remoteFile = ftp.listFiles(remotePath);
RemoteAdapter adapter = new RemoteAdapter(this, remoteFile);
fileList.setAdapter(adapter);
} catch (IOException e) {
System.out.println("connect exception");
e.printStackTrace();
}
}
/**
* 4.0版本进行修改
* */
public void fixVersion() {
String strVer = GetVersion.GetSystemVersion();
strVer = strVer.substring(0, 3).trim();
float fv = Float.valueOf(strVer);
if (fv > 2.3) {
StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
.detectDiskReads().detectDiskWrites().detectNetwork() // 这里可以替换为detectAll()
// 就包括了磁盘读写和网络I/O
.penaltyLog() // 打印logcat,当然也可以定位到dropbox,通过文件保存相应的log
.build());
StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder()
.detectLeakedSqlLiteObjects() // 探测SQLite数据库操作
.penaltyLog() // 打印logcat
.penaltyDeath().build());
}
}
public void getLogInfo() {
Intent intent = getIntent();
hostName = intent.getStringExtra("hostName");
userName = intent.getStringExtra("userName");
password = intent.getStringExtra("password");
showServiceInfo.setText("您登录的主机:" + hostName);
}
public void downLoad() {
Result result = null;
try {
result = ftp.download(RemoteFilePath.remoteFilePath, remoteFile
.get(position).getName(), LOCAL_PATH);
} catch (IOException e) {
e.printStackTrace();
}
if (result.isSucceed()) {
Toast.makeText(ServiceList.this, "下载成功", Toast.LENGTH_SHORT).show();
progressBar.setVisibility(View.INVISIBLE);
} else {
Toast.makeText(ServiceList.this, "下载失败", Toast.LENGTH_SHORT).show();
progressBar.setVisibility(View.INVISIBLE);
}
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论














收起资源包目录




























































































共 64 条
- 1
会跑的鱼~
- 粉丝: 4
- 资源: 5

上传资源 快速赚钱
我的内容管理 收起
我的资源 快来上传第一个资源
我的收益
登录查看自己的收益我的积分 登录查看自己的积分
我的C币 登录后查看C币余额
我的收藏
我的下载
下载帮助

会员权益专享
安全验证
文档复制为VIP权益,开通VIP直接复制

- 1
- 2
- 3
前往页