求教一个android传文件的问题
java和android新手作业求教。。代码太长了也不方便全贴出来都不知道怎么问了= =算是一个通信的软件,发文件的截取部分:
/** 构建List */
private List<Map<String, Object>> constructList(String path) {
File nowFile = new File(path);
adapterList = new ArrayList<Map<String, Object>>();
// 根目录
Map<String, Object> root = new HashMap<String, Object>();
root.put("name", "根目录");
root.put("img", R.drawable.file_root);
root.put("path", "回根目录");
adapterList.add(root);
// 上级目录
Map<String, Object> pMap = new HashMap<String, Object>();
pMap.put("name", "上一级");
pMap.put("img", R.drawable.file_parent);
pMap.put("path", "上一级");
adapterList.add(pMap);
// 若是当前路径对应的是文件,则返回
if (!nowFile.isDirectory()) {
makeTextLong("已选择"+path+",请点击发送按钮发送该文件");
// 发送按钮可用
sendButton.setEnabled(true);
return adapterList;
} else {
// 得到path下所有文件
File[] files = nowFile.listFiles();
if (files != null) {
for (File file : files) {
Map<String, Object> map = new TreeMap<String, Object>();
map.put("name", file.getName());
map.put("path", file.getPath());
String fileName = file.getName();
if (file.isDirectory()) {
map.put("img", R.drawable.folder);
} else if (checkEndName(fileName, this.getResources()
.getStringArray(R.array.fileEndingMid))) {
map.put("img", R.drawable.file_icon_mid);
} else if (checkEndName(fileName, this.getResources()
.getStringArray(R.array.fileEndingMp3))) {……后面省略,就是各种格式后缀文件都来一遍
接收部分:
private static Handler handler = new Handler() {
@Override
public void handleMessage(Message msg) {
switch (msg.what) {
case CommunicationConst.COM_SENDMSG | CommunicationConst.FILE_ATTACHFILE:{
//收到发送文件请求
//得到附加文件信息,字符串数组,分别放了 IP,附加文件信息,发送者名称,包ID
final String[] extraMsg = (String[]) msg.obj;
byte[] bt = {0x07}; //用于分隔多个发送文件的字符
String splitStr = new String(bt);
final String[] fileInfos = extraMsg[1].split(splitStr); //使用分隔字符进行分割
String infoStr = "发送者IP:\t" + extraMsg[0] + "\n" +
"发送者名称:\t" + extraMsg[2] + "\n" +
"文件总数:\t" + fileInfos.length +"个";
new AlertDialog.Builder(queue.getLast())
.setIcon(android.R.drawable.ic_dialog_info)
.setTitle("收到文件传输请求")
.setMessage(infoStr)
.setPositiveButton("接收", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Thread fileReceiveThread = new Thread(new TcpFileReceiveThread(extraMsg[3], extraMsg[0],fileInfos)); //新建一个接受文件线程
fileReceiveThread.start(); //启动线程
queue.getLast().showNotification(); //显示notification
}
})
.setNegativeButton("取消",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
//发送拒绝报文
//构造拒绝报文
CommunicationProtocol ipmsgSend = new CommunicationProtocol();
ipmsgSend.setVersion("" +CommunicationConst.COM_VERSION); //拒绝命令字
ipmsgSend.setCommandNo(CommunicationConst.FILE_RELEASEFILE);
ipmsgSend.setSenderName("android");
ipmsgSend.setSenderHost("android");
ipmsgSend.setAdditionalSection(extraMsg[3] + "\0"); //附加信息里是确认收到的包的编号
InetAddress sendAddress = null;
try {
sendAddress = InetAddress.getByName(extraMsg[0]);
} catch (UnknownHostException e) {
e.printStackTrace();
}
netThreadHelper.sendUdpData(ipmsgSend.getProtocolString(), sendAddress, CommunicationConst.COM_PORT);
}
}).show();
别人的代码,也不是很看得懂。。。主要就是这边发送以后,对方是如何检测到有文件发送过来了,然后跳出那个收到文件传输请求的对话框的呢?不知道代码怎么写。。
感觉我也说的不清不楚的>_< Android
补充:Java , Eclipse