android 使用smb读写局域网共享文件

首先要下载jcifs.jar哦

这里以文本的读写为例子:

这是写入文本:

{
                OutputStream out = null;
                PrintStream ps = null;
                try {
                    File localFile = new File("iputto.txt");//远程服务器共享文件名称
                    String text = "来来来,我们来试一试";//要写入的文本内容
                    String host = "192.168.1.100";//远程服务器的地址
                    //            String username = "";//远程服务器的用户名
                    //            String password = "";//远程服务器的密码
                    String path = "/share/";//远程服务器共享文件夹名称
//                    String remoteUrl = "smb://" + username + ":" + password + "@" + host + path + (path.endsWith("/") ? "" : "/");//带密码的url
                    String remoteUrl = "smb://"+ host + path + (path.endsWith("/") ? "" : "/");//不需要输入用户名密码的url
                    SmbFile remoteFile = new SmbFile(remoteUrl + localFile.getPath());//创建远程对象
                    remoteFile.connect();//建立连接
                    out = new BufferedOutputStream(new SmbFileOutputStream(remoteFile));
                    ps = new PrintStream(out);
                    ps.println(text);
                } catch (Exception e) {
                    String msg = "发生错误:" + e.getLocalizedMessage();
                    System.out.println(msg);
                } finally {
                    try {
                        if (ps != null) {
                            ps.close();
                        }
                        if (out != null) {
                            out.close();
                        }
                    } catch (Exception e) {
                    }
                }
            }

这里是读取文本内容:

  InputStream in = null;
                BufferedReader reader = null;
                try {
                    //目标文件名
                    String fileName = "iputto.txt";
                    String host = "192.168.1.100";//远程服务器的地址
//                    String username = "";//远程服务器的用户名
//                    String password = "";//远程服务器的密码
                    String path = "/share/";//远程服务器共享文件夹名称
//                    String remoteUrl = "smb://" + username + ":" + password + "@" + host + path + (path.endsWith("/") ? "" : "/");//这是需要输入密码的url
                    String remoteUrl = "smb://" + host + path + (path.endsWith("/") ? "" : "/");//这是不需要输入密码的url

                    //创建远程文件对象
                    SmbFile remoteFile = new SmbFile(remoteUrl + "/" + fileName);
                    remoteFile.connect();//建立连接
                    //创建文件流
                    in = new BufferedInputStream(new SmbFileInputStream(remoteFile));
                    reader = new BufferedReader(new InputStreamReader(in, "UTF-8"));//读取流,记得文本文档要设置格式哦,不然会出现乱码
                    StringBuffer sb = new StringBuffer();
                    String line = null;
                    while ((line = reader.readLine()) != null) {
                        sb.append(line.toString());
                    }
                    Message msg = Message.obtain();
                    msg.obj = sb.toString();
                    handler.sendMessage(msg);
                } catch (Exception e) {
                   Log.i("Info","下载远程文件出错:" + e.getLocalizedMessage());
                } finally {
                    try {
                        if (reader != null) {
                            reader.close();
                        }
                        if (in != null) {
                            in.close();
                        }
                    } catch (Exception e) {
                    }
                }

记得要放再子线程里哦~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

 

posted on 2016-11-04 11:04  程序小渣渣  阅读(2699)  评论(0编辑  收藏  举报

导航