android 使用smb读写局域网共享文件 20 January,2021——Mesong
首先要下载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