首先要下载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

评论

发表评论

关于我

8年以上程序开发,熟用php、python、js(jquery、angular)、C等语言,&数据库等。熟悉国内部份开源软件。应用过php socket、vb socket等物联网应用开发设计。对系统设计、分析、团队带领有多年经验,有大型移动项目开发管理经验,对App接入设计管理有较熟悉。多数据量数据库设计使用经验,对高并发负荷系统有深入了解与研究。不喜平淡无挑战性的工作,期待能高迁业务稳定的公司,同时也欢迎各位boss、技术大牛有项目或者技术上的合作!

IM与我联系