java 实施最新版本的JCIFS

eoxn13cs  于 2023-06-28  发布在  Java
关注(0)|答案(2)|浏览(179)

尝试将我的应用程序从jcifs-1.3.19更新到最新的jcifs-2.1.29时,我更改了相应的代码,添加了“必要的”jar到build.gradle

<see answer below>

但仍出现相同的错误,原因如下:java.lang.NoClassDefFoundError:解析失败:Lorg/bouncycastle/asn1/ASN1ObjectIdentifier;
找到了一些参考资料,实施了他们建议的解决方案,但无济于事。缺少什么??
java -version java version“18.0.1”2022-04-19 Java(TM)SE Runtime Environment(build 18.0.1+10-24)Java HotSpot(TM)64位服务器VM(build 18.0.1+10-24,混合模式,共享)
JAVA_HOME=C:\Program Files\Common Files\Oracle\Java\javapath
Android Studio Gradle:

//  implementation files('libs/jcifs-1.3.19.jar') moving from V1 to V2
    implementation files('libs/jcifs-2.1.29.jar')  
    implementation files('libs/bcprov-jdk18on-171')
    implementation files('libs/bcutil-jdk18on-171')
    implementation files('libs/slf4j-api-1.7.30.jar')



public CIFSContext getcredentials(String user, String password){
    NtlmPasswordAuthenticator sourceAuth = new NtlmPasswordAuthenticator("",
            user, password);
    Properties properties = new Properties();
    properties.setProperty("jcifs.smb.client.responseTimeout", "5000");
    PropertyConfiguration configuration = null;
    try {
        configuration = new PropertyConfiguration(properties);
    } catch (CIFSException e) {
        e.printStackTrace();
    }
    CIFSContext cifsContext = new BaseContext(configuration).withCredentials(sourceAuth);
    return cifsContext;
}

连接到PC的代码:

String username = getusername();
        String password= getpassword();

        CIFSContext cifsContext = general.getcredentials(username,password );
        String[] shares;
        sharelist.clear();
        try {
            SmbFile server = new SmbFile("smb://" + ip, cifsContext);  // value for server: "smb://192.168.0.38"
           if( server.exists()) {           // yes as it continues
               try {
                   shares = server.list();  // this is where it crashes      Caused by: java.lang.NoClassDefFoundError: Failed resolution of: Lorg/bouncycastle/asn1/ASN1ObjectIdentifier;
                   for (String s : shares) {
                       if (!s.contains("$")) {
                           sharelist.add(s);
                           op.success = true;
                       }
                       //  System.out.println(s);
                   }
               } catch (SmbException e) {
                   e.printStackTrace();
               }
           }
        } catch (MalformedURLException e) {
            e.printStackTrace();
            op.an_error=e.getMessage().toString();

        } catch (SmbException e) {
            e.printStackTrace();
            op.an_error=e.getMessage().toString();
        }

附加:https://www.baeldung.com/java-bouncy-castle

noj0wjuj

noj0wjuj1#

现在,我们将以下内容添加到build.gradle中:

implementation files('libs/jcifs-2.1.29.jar')
implementation files('libs/slf4j-api-1.7.30.jar') 
implementation 'org.bouncycastle:bcprov-jdk15on:1.68'
3pmvbmvn

3pmvbmvn2#

这就是我如何使用NtlmPasswordAuthenticator解决的问题,例如,将文件复制到桑巴舞共享:

String user="your_samba_username";
String pass="your_samba_password";
String path="smb://samba_ip_address/outputdir/outputfile.txt";
try {
    InputStream  winfis = new FileInputStream(new File("c:/inputdir/inputfile.txt"));
    try {
        SingletonContext baseContext = SingletonContext.getInstance(); 
        Credentials credentials=new NtlmPasswordAuthenticator(null,user,pass); 
        CIFSContext testCtx = baseContext.withCredentials(credentials);
        try {             
            SmbFile smbFile = new SmbFile(path, testCtx);
            SmbFileOutputStream smbfos = new SmbFileOutputStream(smbFile);
            try {
                IOUtils.copy(winfis, smbfos);
            } catch (IOException ex) {
                ex.printStackTrace();
            } finally {    
                try {
                    smbfos.close();
                } catch (IOException ex) {
                     ex.printStackTrace();
                }
            }
        } catch (MalformedURLException ex) {
            ex.printStackTrace();
        }
    } catch (CIFSException ex) {
        ex.printStackTrace();
    } finally {
        try {
            winfis.close();
        } catch (IOException ex) {
             ex.printStackTrace();
        }
    }
} catch (FileNotFoundException ex) {
    ex.printStackTrace();
}

所需的库(依赖项):

相关问题