Java - 以特定用户身份连接到网络路径

Java - Connect to Network Path as a specific User

目前我有一个 java 应用程序,它加载了一个 xml 文件,并为用户提供了一个简单的图形用户界面来编辑 xml 文件的特定字段。但是,此 xml 将存在于网络路径上。只要用户已经登录到网络位置,应用程序就可以正常加载和执行。但是,如果它们没有专门连接到该位置,并且不是 "logged in",则应用程序将不会执行。

有什么方法可以让我在加载文件时以特定用户身份连接到网络路径吗?

感谢阅读。下面的代码片段。

示例位置(已删除 IP 地址)

private static final String wg01x = "\\0.0.0.0\d$\Variable_Namedropper_XMLs\WG01_Variable_NameDropper_XML\VancInsertionLabelDatelineTC.xml";

解析器构造函数片段

public Parser(String fp){

    // File Path Of XMl 
    try{
        this.setFilePath(fp);
    }catch(Exception e ){
        e.printStackTrace();
    }
    // Main DOM Object, Uses implicit FilePath 
    this.setDom(this.normalizeDocument());

normalizeDocument()

public Document normalizeDocument(){
     File xmlFile = new File(this.filePath);
        DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
        DocumentBuilder dBuilder;

        try {
            dBuilder = dbFactory.newDocumentBuilder();
            Document doc = dBuilder.parse(xmlFile);
            doc.getDocumentElement().normalize();
            return doc;

        }catch(Exception e){
            e.printStackTrace();
        }

清单

  <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
  <assembly xmlns="urn:schemas-microsoft-com:asm.v1" manifestVersion="1.0">
   <assemblyIdentity version="1.1.1.1"
    processorArchitecture="X86"
    name="NameDropper_wg01Y.exe"
    type="win32"/>
   <description>NameDropper</description>
   <trustInfo xmlns="urn:schemas-microsoft-com:asm.v2">
     <security>
      <requestedPrivileges>
        <requestedExecutionLevel level="requireAdministrator" uiAccess="false"/>
       </requestedPrivileges>
      </security>
    </trustInfo>
   </assembly>  

编辑:: 也欢迎其他方法。我应该能够得到 access/ownership 的网络位置(可能)

总的来说这是个坏主意...我只是把它放在那里因为它可能会弄乱元数据或者你有什么。

但我仍然会提供帮助,因为这很有趣。我相信它与 normalizeDocument() 上的 File 构造函数有关。我自己从来没有做过,但我做了一些搜索,我认为这可能会对你有所帮助。

Read remote file in java which needs username and password

虽然@Syrrus 发布的线程从概念层面上很有用且有趣,但我无法让它为我的目的工作。

不过,我想出了一个可行的解决方法! 更新后的代码如下。该方法不是使用 Java 对象,而是与运行时环境交互并执行连接到该位置的 cmd。示例中的 exec 字符串格式不正确,因为我隐藏了当前硬编码到其中的用户信息

public Document normalizeDocument(){ File xmlFile = new File(this.filePath); connectToNetworkLocation();

     public void connectToNetworkLocation(){
       try{
        Process p = Runtime.getRuntime().exec("net use \\0.0.0.0\c$ pword /USER:usr");
        p.waitFor();
        BufferedReader reader = new BufferedReader(new InputStreamReader(p.getInputStream()));

        String line;
        while((line = reader.readLine()) != null){
            System.out.println(line);
        }

    }catch(Exception e ){
        e.printStackTrace();
    }
}`