【问题标题】:Read remote file in java which needs username and password在java中读取需要用户名和密码的远程文件
【发布时间】:2012-07-28 07:15:45
【问题描述】:

我正在尝试在 java 中读取远程文件

File f = new File("//192.168.1.120/home/hustler/file.txt");

远程机器需要用户名和密码才能访问文件。

有没有办法通过java代码传递参数并读取文件?

【问题讨论】:

标签: java network-programming


【解决方案1】:
package com.eiq;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;

import org.apache.commons.vfs.FileObject;
import org.apache.commons.vfs.FileSystemOptions;
import org.apache.commons.vfs.Selectors;
import org.apache.commons.vfs.UserAuthenticator;
import org.apache.commons.vfs.VFS;
import org.apache.commons.vfs.auth.StaticUserAuthenticator;
import org.apache.commons.vfs.impl.DefaultFileSystemConfigBuilder;

public class RemoteFileDemo {
    public static void main(String[] args) throws IOException {

        String domain = "hyd\\all";
        String userName = "chiranjeevir";
        String password = "Acvsl@jun2013";
        String remoteFilePath = "\\\\10.0.15.74\\D$\\Suman\\host.txt";


        File f = new File("E:/Suman.txt"); //Takes the default path, else, you can specify the required path
        if (f.exists()) {
            f.delete();
        }
        f.createNewFile();
        FileObject destn = VFS.getManager().resolveFile(f.getAbsolutePath());

        //domain, username, password
        UserAuthenticator auth = new StaticUserAuthenticator(domain, userName, password);
        FileSystemOptions opts = new FileSystemOptions();
        DefaultFileSystemConfigBuilder.getInstance().setUserAuthenticator(opts, auth);


        FileObject fo = VFS.getManager().resolveFile(remoteFilePath, opts);

        System.out.println(fo.exists());

        //fo.createFile();

        destn.copyFrom(fo, Selectors.SELECT_SELF);
        destn.close();

        //InputStream is = new FileInputStream(f);

    }
}

这是一个从远程机器读取文件并将其作为文件E:/Suman.txt存储在我们本地机器中的程序。

写文件路径时要小心,而不是:,我们必须用$符号替换它,例如: D:\Suman\Boorla\kpl.txt 错了, D$\\Suman\\Boorla\\kpl.txt 是对的。

在上面的程序中,你必须更改远程机器的域名、用户名、密码和文件路径。 要使用上述程序,我们需要在类路径中添加以下 jar 文件。

commons-vfs.jar
commons-logging.jar

【讨论】:

  • 如何添加这个类路径??
【解决方案2】:

jCIFS 的另一种选择是您可以轻松指定身份验证参数:

NtlmPasswordAuthentication auth = new NtlmPasswordAuthentication("domain", "user", "password"); // Authentication info here, domain can be null
try (InputStream is = new SmbFile("smb://192.168.1.120/home/hustler/file.txt", auth).getInputStream()) {
    // Read from 'is' ...
} catch (IOException e) {
    // Handle IOException
}

【讨论】:

    【解决方案3】:

    你也可以试试Commons VSF。检查UserAuthenticator

    【讨论】:

    • 谢谢,它就像一个魅力,我已经发布了代码作为答案。再次感谢
    【解决方案4】:

    这是我编写的代码,它运行良好。

    File f=new File("abc.txt"); //Takes the default path, else, you can specify the required path
    if(f.exists())
    {
        f.delete();
    }
    f.createNewFile(); 
    FileObject destn = VFS.getManager().resolveFile(f.getAbsolutePath());
    UserAuthenticator auth = new StaticUserAuthenticator("", "myusername", "secret_password");
    FileSystemOptions opts = new FileSystemOptions();
    
    DefaultFileSystemConfigBuilder.getInstance().setUserAuthenticator(opts, auth);
    FileObject fo = VFS.getManager().resolveFile("\\\\192.168.0.1\\direcory\\to\\GetData\\sourceFile.txt",opts);
    destn.copyFrom(fo,Selectors.SELECT_SELF);
    destn.close();
    

    现在您可以使用该文件执行所需的操作。有点像...

    InputStream is = new FileInputStream(f);
    

    【讨论】:

    • 在从本地副本读取之前,您是否在本地复制文件?那是相当低效的......
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-22
    • 1970-01-01
    • 1970-01-01
    • 2010-12-08
    • 1970-01-01
    • 2018-01-10
    相关资源
    最近更新 更多