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) {
                    }
                }

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

 

转载于:https://www.cnblogs.com/ce-home/p/6029479.html

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值