Access to file using Java with Samba JCIFS

前端 未结 2 476
情深已故
情深已故 2020-11-27 15:44

I have a question about accessing file with Samba JCIFS.

So there is a server I want to access, let\'s call it server.unv.edu and the workgroup is WKGRP.

There

相关标签:
2条回答
  • 2020-11-27 16:03

    You are making this harder than it should be. Please follow the below steps and make sure the shared folder you are creating has write access for this user you are using.

    1. download the jar file http://jcifs.samba.org/ (there is only one jar file)
    2. copy and paste the below code with your information for user name, password and shared folder and that's all you need

    I was running this on Linux and wanted to write to a Windows box so you want to create a shared folder and put the shared folder name in the below variable if you don't know how to create shared folder on windows ...use google as always

        String user = "your_user_name";
        String pass ="your_pass_word";
    
        String sharedFolder="shared";
        String path="smb://ip_address/"+sharedFolder+"/test.txt";
        NtlmPasswordAuthentication auth = new NtlmPasswordAuthentication("",user, pass);
        SmbFile smbFile = new SmbFile(path,auth);
        SmbFileOutputStream smbfos = new SmbFileOutputStream(smbFile);
        smbfos.write("testing....and writing to a file".getBytes());
        System.out.println("completed ...nice !");
    
    0 讨论(0)
  • 2020-11-27 16:09
    /** You can simply use this util class for read write operations. **/
    
    import java.io.IOException;
    import java.io.OutputStream;
    import java.net.MalformedURLException;
    import java.net.UnknownHostException;
    import java.nio.charset.StandardCharsets;
    
    import jcifs.smb.NtlmPasswordAuthentication;
    import jcifs.smb.SmbException;
    import jcifs.smb.SmbFile;
    import jcifs.smb.SmbFileInputStream;
    import jcifs.smb.SmbFileOutputStream;
    
    public class NDMUtil {
    
    public static void writeToNDM(String userName, String password, String domain, String sharedPath,
            String textToWrite) throws MalformedURLException, SmbException, UnknownHostException, IOException {
        NtlmPasswordAuthentication authentication = new NtlmPasswordAuthentication(domain, userName, password);
        try (OutputStream out = new SmbFileOutputStream(new SmbFile(sharedPath, authentication))) {
            byte[] bytesToWrite = textToWrite.getBytes();
            if (out != null && bytesToWrite != null && bytesToWrite.length > 0) {
                out.write(bytesToWrite);
            }
        }
        ;
    }
    
    public static String readFromNDM(String userName, String password, String domain, String sharedPath)
            throws MalformedURLException, SmbException, IOException {
        NtlmPasswordAuthentication authentication = new NtlmPasswordAuthentication(domain, userName, password);
        String fileContent = IOUtil.toString(new SmbFileInputStream(new SmbFile(sharedPath, authentication)),
                StandardCharsets.UTF_8.name());
        System.out.println(fileContent);
        return fileContent;
    }
    

    }

    0 讨论(0)
提交回复
热议问题