What is the recommended way of accessing a network share folder (located in Windows or Linux) in java

16,006

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 '/'.

Share:
16,006

Related videos on Youtube

Joe.wang
Author by

Joe.wang

An Asp.net developer. Sometimes in Java

Updated on September 14, 2022

Comments

  • Joe.wang
    Joe.wang over 1 year

    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

    1. 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.*)
    2. What is the best way to access the network share folder from windows or linux ?

    Thanks.

    • Adrian Shum
      Adrian Shum almost 9 years
      I believe Linux doesn't allow you to access UNC directly like that. Either you mount that path and access just like a local directory, or you may make use of smbclient if you do not like to mount it. Or a more portable way, use a plain Java SMB client (e.g. JCIFS?) to access so that you do not need to rely on OS-specific features
    • Adrian Shum
      Adrian Shum almost 9 years
      SMB is, in short, the protocol name. Samba is the product on Linux that implements SMB with different utilties
    • Joe.wang
      Joe.wang almost 9 years
      @AdrianShum aha . Good . Well explain. :)
  • Joe.wang
    Joe.wang almost 9 years
    Nice . Sounds I have two options to make it . Well . I think I need to do more research based on your answer .em.. That is good point to get start. Thank you .
  • Joe.wang
    Joe.wang almost 9 years
    I am sorry . I don't why , I can't access the path smb://10.50.90.18 in my Linux(alt+f2 run windows). Nothing to do with the Java. Thanks.