2010-03-08 34 views
1

我developp使用Windows桌面搜索的Java应用程序从中我能找回我的计算机上的文件的一些信息,比如URL(System.ItemUrl)。这种URL的一个例子是打开邮件使用协议“MAPI://”

file://c:/users/ausername/documents/aninterestingfile.txt 

为 “正常” 的文件。该栏位还提供从Outlook或Thunderbird索引的邮件项目的网址。 Thunderbird的项目(仅适用于Vista和7)也是文件(.wdseml)。但前景的项目网址开头为“MAPI://”,如:

mapi://{S-1-5-21-1626573300-1364474481-487586288-1001}/[email protected]($b423dcd5)/0/Inbox/가가가가곕갘객겒갨겑곓걌게겻겨곹곒갓곅갩갤가갠가 

我已经使用这个网址从Java是开放Outlook中的实际项目的问题。如果我将它复制/粘贴到Windows的运行对话框中,它就会起作用;如果我使用“start”,然后在命令行中复制/粘贴的url,它也可以工作。

该网址似乎以UTF-16编码。我希望能够写出这样的代码:

String url = "mapi://{S-1-5-21-1626573300-1364474481-487586288-1001}/[email protected]($b423dcd5)/0/Inbox/가가가가곕갘객겒갨겑곓걌게겻겨곹곒갓곅갩갤가갠가"; 

Runtime.getRuntime().exec("cmd.exe /C start " + url); 

我不工作,我已经尝试过其他解决方案,如:

String start = "start"; 
String url = "mapi://{S-1-5-21-1626573300-1364474481-487586288-1001}/[email protected]($b423dcd5)/0/Inbox/가가가가곕갘객겒갨겑곓걌게겻겨곹곒갓곅갩갤가갠가"; 

FileOutputStream fos = new FileOutputStream(new File("test.bat"); 
fos.write(start.getBytes("UTF16"); 
fos.write(url.getBytes("UTF16")); 
fos.close(); 

Runtime.getRuntime().exec("cmd.exe /C test.bat"); 

没有任何成功。使用上面的解决方案,文件“下的test.bat”包含了正确的网址上的“开始”命令,但在众所周知的错误信息“下的test.bat”的结果来看:

'■' is not recognized as an internal or external command, operable program or batch file. 

有没有人的能够打开来自Java的“mapi://”项目的想法?

回答

1

嗯,我的问题有点棘手。但我终于找到了答案,并将在此分享。

我怀疑什么是真实的:Windows使用UTF-16(小端)的URL。这是没有差异的UTF-8工作时,我们只用如图像,文字等的文件的路径,但要能够访问Outlook项目,我们必须使用UTF-16LE。如果我使用C#编码,则不会有任何问题。但在Java中,你必须更具创造性。

从Windows桌面搜索,我检索此:

mapi://{S-1-5-21-1626573300-1364474481-487586288-1001}/[email protected]($b423dcd5)/0/Inbox/가가가가곕갘객겒갨겑곓걌게겻겨곹곒갓곅갩갤가갠가 

而我所做的是创建一个临时VB脚本,并像这样运行:

/** 
* Opens a set of items using the given set of paths. 
*/ 
public static void openItems(List<String> urls) { 
    try { 

    // Create VB script 
    String script = 
     "Sub Run(ByVal sFile)\n" + 
     "Dim shell\n" + 
     "Set shell = CreateObject(\"WScript.Shell\")\n" + 
     "shell.Run Chr(34) & sFile & Chr(34), 1, False\n" + 
     "Set shell = Nothing\n" + 
     "End Sub\n"; 

    File file = new File("openitems.vbs"); 

    // Format all urls before writing and add a line for each given url 
    String urlsString = ""; 
    for (String url : urls) { 
     if (url.startsWith("file:")) { 
     url = url.substring(5); 
     } 
     urlsString += "Run \"" + url + "\"\n"; 
    } 

    // Write UTF-16LE bytes in openitems.vbs 
    FileOutputStream fos = new FileOutputStream(file); 
    fos.write(script.getBytes("UTF-16LE")); 
    fos.write(urlsString.getBytes("UTF-16LE")); 
    fos.close(); 

    // Run vbs file 
    Runtime.getRuntime().exec("cmd.exe /C openitems.vbs"); 

    } catch(Exception e){} 
} 
+0

什么绕道而行!为什么exec(cmd)? Java有没有办法调用COM对象?那么你应该能够导入WScript.Shell'的'类型库(该推移的全名'Windows脚本宿主对象Model')和直接调用运行。 – 2015-09-23 06:07:26

相关问题