相信多数人都知道用ES文件管理器可以打开网络邻居上的其它机器的共享资源,还可以直接调用相关应用打开文件,看其属性,是类似:smb://192.168.1.123/pub/file.txt这样的路径。现在遇到一个问题:在自己编的程序中,怎么打开这样的共享文件呢?试了FileInputStream和getContentResolver().openInputStream(Uri.parse(url))都不识别上述文件路径。

解决方案 »

  1.   

    使用jcifs 开源库,贴段代码
    try {
    //指定服务器共享文件的路径,访问账号密码
    SmbFile smbFile = new SmbFile("smb://jlansrv:[email protected]:1445/JLAN/test.lrc");
    int length = smbFile.getContentLength();
    System.out.print("contentLength = " + length);
    byte buffer[] = new byte[length];
    try {
    SmbFileInputStream in = new SmbFileInputStream(smbFile);//读取文件数据流,具体操作跟普通的File类似
    while(in.read(buffer) != -1) {
    System.out.println(new String(buffer));
    System.out.println("buffer.length = " + buffer.length);
    }
    } catch (IOException e) {
    e.printStackTrace();
    }
    } catch (MalformedURLException e) {
    e.printStackTrace();
    }
      

  2.   

    建议研究一下smb和jcifs。