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

评论

Jenna

Can I take your number? https://oic.gov.pg/pharmacy/index.php/stmap_15vkkger.html?viagra.potassium.advair azithromycin 500mg uses in tamil The biggest danger female troops face is in fact sexual assault from their peers. 3,192 cases were recorded in 2011

On 25 March,2022

Horace

I've just started at https://santi.pp.ua/pharmacy/stmap_42qktlqx.html?mofetil.allegra.levitra clarithromycin biaxin pregnancy We already have 50 million of our own people in poverty, dependent on our entitlement systems. Why in the world are we thinking

On 25 March,2022

Branden

Where do you study? https://www.twlcat.org/pharmacy/stmap_15nrpill.html?biaxin.levitra.finax can you overdose on tylenol or advil The number of market-rate rental apartments available to those of low income is extremely small. A metaphor one often hears

On 25 March,2022

Willian

I'll put him on http://msmcf.co.kr/pharmacy/stmap_81aaschp.html?wellbutrin.augmentin.cialis.linezolid azithromycin over the counter walgreens They have questioned Guerra's credibility, saying Chevron ispaying him for his testimony. In January, Chevron sa

On 25 March,2022

Stephan

I enjoy travelling https://www.asmp.mpi.gov.lk/pharmacy/stmap_81dcevos.html?manxxx.prozac.prevacid.viagra zofran zydis Selectors are the NSA's term for what it is searching for - such as an email address or phone number. Detasking means the agency stops

On 25 March,2022

Carmen

Withdraw cash http://cnpcl.com/stmap_15nrpill.html?shatavari.imiquimod.levitra kan man ta voltaren och alvedon samtidigt “Mighty, Larry Budd, president/Irish Pub Mighty Quinn Fan Club here. On Saturday, The Ancient Order of Hibernians, Div. 14 RVC/Lynb

On 25 March,2022

Odell

I'm a member of a gym http://thevikingmethod.com.b100.opex.is/pharmacy/stmap_15nrpill.html?tri-cyclen.droxia.thorazine.viagra amazon uk slimfast keto Conde Nast is the first magazine publisher to collaborate with Amazon on this type of service, a move th

On 25 March,2022

Lifestile

What part of do you come from? https://www.asmp.mpi.gov.lk/pharmacy/stmap_15nrpill.html?cialis.periactin.ethionamide.aciphex meloxicam y metocarbamol The malware showed up Sunday morning on multiple websites hosted by the anonymous hosting company Freed

On 25 March,2022

发表评论

关于我

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

IM与我联系