尝试从亚马逊复制并粘贴图像时报告此错误,虽然我们检查可转移对象是否支持特定的数据风格,但在尝试使用该数据风格获取网址时失败。我以前没见过这个错误,最近转移到了Java 1.8.0_20所以我假设问题出在Java 8上?
java.net.MalformedURLException: no protocol: <?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<array>
<string>http://ecx.images-amazon.com/images/I/41NTPXC8EHL.jpg</string>
<string></string>
</array>
</plist>
at java.net.URL.<init>(URL.java:586)
at java.net.URL.<init>(URL.java:483)
at java.net.URL.<init>(URL.java:432)
at sun.lwawt.macosx.CDataTransferer.translateBytes(CDataTransferer.java:135)
at sun.awt.datatransfer.ClipboardTransferable$DataFactory.getTransferData(ClipboardTransferable.java:71)
at sun.awt.datatransfer.ClipboardTransferable.getTransferData(ClipboardTransferable.java:168)
代码提取
public static DataFlavor imageUrlFlavor = new DataFlavor("application/x-java-url;class=java.net.URL");
public void getImage(Transferable trans)
if(trans.isDataFlavorSupported(FileDropTarget.imageUrlFlavor))
{
imageUrl = (URL) trans.getTransferData(FileDropTarget.imageUrlFlavor);
}
答案 0 :(得分:0)
不是一个令人满意的答案
您可以列出放置在剪贴板上的所有DataFlavor
trans.getDataFlavors();通常,副本会放置几种风格,如纯文本和样式文本。
替代
public static DataFlavor imageUrlFlavor =
new DataFlavor(URL.class, "application/x-java-url");
可能也失败了。
答案 1 :(得分:0)
似乎OS X将复制的数据包装到plist / xml中,因此它会破坏您的解析。您是直接从浏览器还是从其他应用程序复制图像?
我试图通过我的Mac(OS X 10.9.5,Java 8u20)上的简单摇摆应用程序从Chrome和Safari复制图像和网址,我总是得到text / plain或image / x-java- image mimetypes,never application / x-java-url或xml-wrapped content。
Transferable clipData = clipboard.getContents(clipboard);
if (clipData != null) {
try {
for (DataFlavor f : clipData.getTransferDataFlavors()) {
System.out.println("f = " + f);
}
if (clipData.isDataFlavorSupported(imageUrlFlavor)) {
URL url = (URL) clipData.getTransferData(imageUrlFlavor);
System.out.println("Got url:");
System.out.println(url);
}
} catch (UnsupportedFlavorException ufe) {
System.err.println("Flavor unsupported: " + ufe);
} catch (IOException ioe) {
System.err.println("Data not available: " + ioe);
}
}
我也试图通过拖放来做到这一点并得到相同的结果:
public void drop(DropTargetDropEvent dtde) {
System.out.println("drop");
dtde.acceptDrop(DnDConstants.ACTION_COPY_OR_MOVE);
Transferable trans = dtde.getTransferable();
boolean gotData = false;
// try for application/x-java-url flavor
try {
if (trans.isDataFlavorSupported(urlFlavor)) {
URL url = (URL) trans.getTransferData(urlFlavor);
System.out.println ("got an url: " + url);
statusField.setText(url.toString());
statusField.setCaretPosition(0);
gotData = true;
} else if (trans.isDataFlavorSupported(DataFlavor.stringFlavor)) {
String s =
(String) trans.getTransferData(DataFlavor.stringFlavor);
System.out.println("Got text: " + s);
statusField.setText(s);
gotData = true;
} else {
dumpDataFlavors(trans);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
System.out.println("gotData is " + gotData);
dtde.dropComplete(gotData);
}
}