0

所以我想创建一个DataFlavor,它包含一个字符串并使用java 具有mimetype image/svg+xml。我不太确定我是否了解 DataFlavor 和 mimetype 是如何连接的。我不太明白 Java 教程1

我想要的是能够使用系统剪贴板将我作为 svg 对象生成的字符串移动到其他程序。

我现在使用的代码是:

import java.awt.Toolkit;
import java.awt.datatransfer.ClipboardOwner;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.Transferable;
import java.awt.datatransfer.UnsupportedFlavorException;



public class SvgClip implements Transferable, ClipboardOwner
{

    private String string;

    public SvgClip(String str)

    {
        string = str;
    }

    public DataFlavor[] getTransferDataFlavors()

    {    
        DataFlavor flavor = new DataFlavor();
        try{
            //flavor = new DataFlavor("image/svg+xml");S
            flavor = new DataFlavor("image/svg+xml;class=java.lang.String");
        }catch (Exception ex) {
            ex.printStackTrace();
        }
        return new DataFlavor[]{flavor};    
    }

    public boolean isDataFlavorSupported(DataFlavor flavor)        
    {    
        return DataFlavor.imageFlavor.equals(flavor);    
    }

    public Object getTransferData(DataFlavor flavor) throws        
            UnsupportedFlavorException        
    {    
        if (!isDataFlavorSupported(flavor))        
            throw new UnsupportedFlavorException(flavor);    
        return string;    
    }

    public void lostOwnership(java.awt.datatransfer.Clipboard clip,        
            java.awt.datatransfer.Transferable tr)        
    {    
        return;    
    }
}

但是,这不起作用,我java.io.IOException: image/svg+xml 在尝试创建它时得到:

SvgClip strSel = new SvgClip(newfileString);
clipboard.setContents(strSel, null);

有人知道吗?

4

1 回答 1

0

我现在正在使用这个类来实现一个可以放在剪贴板中的 SVG 对象,如果有用请感谢!:

import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.SystemFlavorMap;
import java.awt.datatransfer.Transferable;
import java.awt.datatransfer.UnsupportedFlavorException;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.nio.charset.StandardCharsets;
import java.util.List;




public class SvgClip implements Transferable
{

    final private String svgstring;

    //https://bugs.openjdk.java.net/browse/JDK-8140526
    //http://bugs.java.com/bugdatabase/view_bug.do;jsessionid=67bf0d656b66cc60360819906ba?bug_id=4493178

    static final private DataFlavor svgFlavor = new DataFlavor("image/svg+xml; class=java.io.InputStream","Scalable Vector Graphic");
    static final private DataFlavor inkscapeFlavor = new DataFlavor("image/x-inkscape-svg; class=java.io.InputStream","Scalable Vector Graphic");
    //static private DataFlavor svgFlavor = new DataFlavor(String.class,"image/svg+xml");


    private DataFlavor [] supportedFlavors;
    private FileTransferable file;

    public SvgClip(String str)       
    {
        /*
        try {
            this.svgFlavor = new DataFlavor("image/svg+xml");
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(SvgClip.class.getName()).log(Level.SEVERE, null, ex);
        }
*/

        this.supportedFlavors = new DataFlavor[] {
            svgFlavor, 
            DataFlavor.javaFileListFlavor,
            inkscapeFlavor
        };

        this.svgstring = str;

        SystemFlavorMap systemFlavorMap = (SystemFlavorMap) SystemFlavorMap.getDefaultFlavorMap();
        DataFlavor dataFlavor = svgFlavor;
        //systemFlavorMap.addFlavorForUnencodedNative("image/svg+xml", dataFlavor);
        systemFlavorMap.addUnencodedNativeForFlavor(dataFlavor, "image/svg+xml");
        systemFlavorMap.addUnencodedNativeForFlavor(inkscapeFlavor, "image/x-inkscape-svg");

        try{
            this.file = FileTransferable.createFileInTempDirectory("temp.svg");
            List<File> files = file.getFiles();
            if(files.isEmpty())
            {
                System.out.println("No file!");
            }
            else
            {
                //write the svgstring to a file in temp.
                PrintWriter writer = new PrintWriter(files.get(0));
                writer.println(svgstring);
                writer.close();

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

    @Override public DataFlavor[] getTransferDataFlavors()
    {    
          return this.supportedFlavors;    
    }
    static DataFlavor getSVGFlavor()
    {
        return SvgClip.svgFlavor;
    }
    @Override public boolean isDataFlavorSupported(DataFlavor flavor)        
    {   

        for( DataFlavor supported : this.supportedFlavors)
        {
            if( flavor.equals(supported)){
                System.out.println("flavor is supported " + 
                        flavor.getMimeType()
                + " " + flavor.toString()
                        );
                return true;
            }
            else{
                System.out.println("flavor is NOT supported " + flavor.getMimeType());

            }
        }
        return false;    
    }

    @Override public Object getTransferData(DataFlavor flavor)
            throws UnsupportedFlavorException, IOException 
    {
        if (isDataFlavorSupported(flavor))
        {

            if(flavor.equals(inkscapeFlavor) || flavor.equals(svgFlavor))
            {
                InputStream stream = new ByteArrayInputStream(svgstring.getBytes(StandardCharsets.UTF_8));
                return stream;
            }
            if(flavor.equals(DataFlavor.javaFileListFlavor))
            {
                System.out.println("Returning file");
                return this.file.getTransferData(flavor);
            }
        }
        throw new UnsupportedFlavorException (flavor);
    }

    public void lostOwnership(java.awt.datatransfer.Clipboard clip,        
            java.awt.datatransfer.Transferable tr)        
    {    
        return;    
    }
}
于 2015-11-21T18:30:11.527 回答