java读取局域网共享文件夹,在java中访问网络共享文件夹(位于Windows或Linux中)的推荐方法是什么?...

All, Forgive me I am not familiar with the Linux.

I am trying to read all the files of a network share folder which is located in either Windows or Linux system.

Currently I just made it work for the case of Windows by below code.

networkShareFolder="\\\\10.50.90.18\\ITS Tool\\xml\\";//It is a windows Network share path.

File[] files = new File(networkShareFolder).listFiles();

But When I deploy my application to the Linux system and run it. It just told me can not get any files from the specified networkShareFolder;

So I tried to type the path \\10.50.90.18 in the File explorer of Linux like what I did in the windows. To see if the path can be reached from the Linux system. But it just told me Can't locate the \\10.50.90.18. But I am sure the IP can be ping from the Linux.

So my questions are

Why \\10.50.90.18 can't be accessed in Linux .But can be accessed in Windows. (I am sure their IP are all 10.50.90.*)

What is the best way to access the network share folder from windows or linux ?

Thanks.

解决方案

Remote Mount with FUSE

It's possible to mount a remote filesystem (generally including SMB/CIFS) with FUSE and samba. That might look something like (assuming you have a mountpoint /windows)

# export USER=efrisch

# export WORKGRP=mygrp

# smbmount //10.50.90.18/ /windows –o username=$USER,workgroup=$WORKGRP

Then you could access your directory (transparently) with

new File("/windows/ITS Tool/xml")

Pure Java Solution (with JCIFS)

JCIFS provides SmbFile and that provides listFiles() allowing something like

SmbFile[] files = new SmbFile("smb://10.50.90.18/ITS Tool/xml/").listFiles();

The linked documentation for SmbFile does give the full format as

smb://[[[domain;]username[:password]@]server[:port]/[[share/[dir/]file]]][?param=value[param2=value2[...]]]

and it also notes that all SMB URLs that represent workgroups, servers, shares, or directories require a trailing slash '/'.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值