8

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

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

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

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

4

4 回答 4

8
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
于 2013-06-21T08:28:58.840 回答
5

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
}
于 2017-01-23T14:29:13.000 回答
3

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

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);
于 2012-07-31T16:42:50.887 回答
3

你也可以试试Commons VSF。检查用户身份验证器

于 2012-07-30T15:47:45.713 回答