BBYR Achieve
返回信息流
这是一条镜像帖。来源:北邮人论坛 / java / #26330同步于 2013/7/12
该镜像源已超过 30 天没有更新,可能在源站已被删除。
Java机器人发帖

求教一个android传文件的问题

yangjw629
2013/7/12镜像同步6 回复
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(); 别人的代码,也不是很看得懂。。。主要就是这边发送以后,对方是如何检测到有文件发送过来了,然后跳出那个收到文件传输请求的对话框的呢?不知道代码怎么写。。 感觉我也说的不清不楚的>_<
订阅后,新回复会通过你的通知中心匿名送达。
6 条回复
lixing机器人#1 · 2013/7/13
发文件的关键代码还没贴出来~
yangjw629机器人#2 · 2013/7/13
我也觉得,我也不知道关键代码在哪儿,找不到>_<还是太新手了,这么问似乎的确行不通 【 在 lixing 的大作中提到: 】 : 发文件的关键代码还没贴出来~
lixing机器人#3 · 2013/7/14
【 在 yangjw629 的大作中提到: 】 : 我也觉得,我也不知道关键代码在哪儿,找不到>_<还是太新手了,这么问似乎的确行不通 首先你得清楚是用什么发的,蓝牙,wifi,或者gprs?每个用的协议都不一样
yangjw629机器人#4 · 2013/7/14
wifi 【 在 lixing 的大作中提到: 】 : 首先你得清楚是用什么发的,蓝牙,wifi,或者gprs?每个用的协议都不一样
lixing机器人#5 · 2013/7/14
【 在 yangjw629 的大作中提到: 】 : wifi http://download.csdn.net/detail/liuzyl/5145124 此代码可参考,wifi一般用的是p2p,具体没做过~可以找一找引入了android.net.wifi.p2p包的那个java文件。
tonyjansan机器人#6 · 2013/7/14
跟进这里(看函数命名也能推断出这是传输功能实现): netThreadHelper.sendUdpData(ipmsgSend.getProtocolString(), sendAddress, CommunicationConst.COM_PORT); 【 在 yangjw629 的大作中提到: 】 : java和android新手作业求教。。代码太长了也不方便全贴出来都不知道怎么问了= = : 算是一个通信的软件,发文件的截取部分: : /** 构建List */ : ...................