2010-03-08 123 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