【发布时间】:2015-06-04 09:47:12
【问题描述】:
我创建了一个 Java 应用程序,用户可以在其中拖放文件以将其保存到指定的文件夹中。我正在使用FileDrop,不幸的是它不适用于直接从 Outlook 拖放的电子邮件。当电子邮件首先被放到桌面(创建一个 .eml 文件)然后放到应用程序中时,它可以工作,但我真的想绕过这一步。
你可以在下面看到我的代码:
new FileDrop(panel, new FileDrop.Listener() {
public void filesDropped(java.io.File[] files) {
for (int i=0; i<files.length; i++) {
File newFile = files[i];
byte[] myByteArray = null;
try { //get the data of the file into a byte array
myByteArray = org.apache.commons.io.FileUtils.readFileToByteArray(newFile);
} catch (IOException e1) {
e1.printStackTrace();
}
String newFileName = newFile.getName();
try { //create the file
FileOutputStream file = new FileOutputStream("projects/"+ newFileName);
file.write(myByteArray);
file.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
});
我很想听听这个问题的任何可能解决方案。
顺便说一句,这是我收到的错误消息的一部分,指出问题出现在public void filesDropped(java.io.File[] files) { 行中,因为放入应用程序的电子邮件还没有被识别为文件(我猜)。
...
2015-06-04 12:10:50.860 java[718:71442] Couldn't get a copy of an HFS Promise from the pasteboard
2015-06-04 12:10:50.860 java[718:71442] Looked for HFSPromises on the pasteboard, but found none.
Exception in thread "AWT-EventQueue-0" java.lang.IllegalArgumentException: URI is not hierarchical
at java.io.File.<init>(File.java:363)
at net.iharder.dnd.FileDrop.createFileArray(FileDrop.java:453)
...
提前致谢。
【问题讨论】:
-
从应用程序中拖出的东西不是“文件”(总是),它是某种其他数据,您需要确定应用程序用于导出对象的数据类型和根据数据风格通过适当的方式“提取”数据
-
@MadProgrammer 感谢您的快速回复!有没有办法使用 FileDrop 来解决我的问题?或者您会提出类似的解决方案:stackoverflow.com/questions/1204580/…
-
在不了解 Outlook 支持的数据风格的情况下,我会说知道,您不能使用 FileDrop,您将不得不使用核心 D'n'D api或可转让的api
-
从我使用 Outlook、Swing 和拖放的经验来看,Outlook 仅传输所拖动电子邮件的一些数据(主题、收件人……)。不会传输有关实际内容或附件的数据。因此,您需要做的是,您的代码会通知 Outlook 发生了放置事件,然后使用一些 COM 框架通过 COM 访问 Outlook。然后您可以自己获取所需的数据...顺便说一句,Thunderbird 与我的代码完美配合,无需任何更改。我从未实现过这种 COM 通信,但请看这里:link
标签: java swing file email drag-and-drop