当前位置: 技术问答>java相关
如何进行MIME编码解码,Java的标准库有吗?
来源: 互联网 发布时间:2014-12-25
本文导语: 如何进行MIME编码解码,Java的标准库有吗? | /** * @version 1.10 1999-09-20 * @author Cay Horstmann */ import java.io.*; import java.awt.*; import java.awt.image.*; import java.awt.event.*; import java.awt.datatransfer.*...
如何进行MIME编码解码,Java的标准库有吗?
|
/**
* @version 1.10 1999-09-20
* @author Cay Horstmann
*/
import java.io.*;
import java.awt.*;
import java.awt.image.*;
import java.awt.event.*;
import java.awt.datatransfer.*;
import javax.swing.*;
public class MimeClipboardTest
{ public static void main(String [] args)
{ JFrame frame = new MimeClipboardFrame();
frame.show();
}
}
class MimeClipboardFrame extends JFrame
implements ActionListener
{ public MimeClipboardFrame()
{ setSize(300, 300);
setTitle("MimeClipboardTest");
addWindowListener(new WindowAdapter()
{ public void windowClosing(WindowEvent e)
{ System.exit(0);
}
} );
Container contentPane = getContentPane();
label = new JLabel();
contentPane.add(label, "Center");
JMenu fileMenu = new JMenu("File");
openItem = new JMenuItem("Open");
openItem.addActionListener(this);
fileMenu.add(openItem);
exitItem = new JMenuItem("Exit");
exitItem.addActionListener(this);
fileMenu.add(exitItem);
JMenu editMenu = new JMenu("Edit");
copyItem = new JMenuItem("Copy");
copyItem.addActionListener(this);
editMenu.add(copyItem);
pasteItem = new JMenuItem("Paste");
pasteItem.addActionListener(this);
editMenu.add(pasteItem);
JMenuBar menuBar = new JMenuBar();
menuBar.add(fileMenu);
menuBar.add(editMenu);
setJMenuBar(menuBar);
}
public void actionPerformed(ActionEvent evt)
{ Object source = evt.getSource();
if (source == openItem)
{ JFileChooser chooser = new JFileChooser();
chooser.setCurrentDirectory(new File("."));
chooser.setFileFilter(new
javax.swing.filechooser.FileFilter()
{ public boolean accept(File f)
{ String name = f.getName().toLowerCase();
return name.endsWith(".gif")
|| name.endsWith(".jpg")
|| name.endsWith(".jpeg")
|| f.isDirectory();
}
public String getDescription()
{ return "Image files";
}
});
int r = chooser.showOpenDialog(this);
if(r == JFileChooser.APPROVE_OPTION)
{ String name
= chooser.getSelectedFile().getAbsolutePath();
setImage(Toolkit.getDefaultToolkit().getImage(name));
}
}
else if (source == exitItem) System.exit(0);
else if (source == copyItem) copy();
else if (source == pasteItem) paste();
}
private void copy()
{ MediaTracker tracker = new MediaTracker(this);
tracker.addImage(theImage, 0);
try { tracker.waitForID(0); }
catch (InterruptedException e) {}
BufferedImage image
= new BufferedImage(theImage.getWidth(null),
theImage.getHeight(null),
BufferedImage.TYPE_INT_RGB);
Graphics2D g2 = image.createGraphics();
g2.drawImage(theImage, 0, 0, null);
Bitmap bitmap = new Bitmap(image);
SerializableSelection selection
= new SerializableSelection(bitmap);
mimeClipboard.setContents(selection, null);
}
private void paste()
{ Transferable selection
= mimeClipboard.getContents(this);
try
{ Bitmap bitmap = (Bitmap)selection.getTransferData
(SerializableSelection.serializableFlavor);
setImage(bitmap.getImage());
}
catch(Exception e) {}
}
public void setImage(Image image)
{ theImage = image;
label.setIcon(new ImageIcon(image));
}
private static Clipboard mimeClipboard
= new MimeClipboard
(Toolkit.getDefaultToolkit().getSystemClipboard());
private Image theImage;
private JLabel label;
private JMenuItem openItem;
private JMenuItem exitItem;
private JMenuItem copyItem;
private JMenuItem pasteItem;
}
class Bitmap implements Serializable
{ public Bitmap(BufferedImage image)
{ type = image.getType();
width = image.getWidth();
height = image.getHeight();
WritableRaster raster = image.getRaster();
data = raster.getDataElements(0, 0, width, height, null);
}
public BufferedImage getImage()
{ BufferedImage image
= new BufferedImage(width, height, type);
WritableRaster raster = image.getRaster();
raster.setDataElements(0, 0, width, height, data);
return image;
}
private int type;
private int width;
private int height;
private Object data;
}
class SerializableSelection implements Transferable
{ public SerializableSelection(Serializable object)
{ theObject = object;
}
public boolean isDataFlavorSupported(DataFlavor flavor)
{ return flavor.equals(serializableFlavor);
}
public synchronized Object getTransferData
(DataFlavor flavor)
throws UnsupportedFlavorException
{ if(flavor.equals(serializableFlavor))
{ return theObject;
}
else
{ throw new UnsupportedFlavorException(flavor);
}
}
public DataFlavor[] getTransferDataFlavors()
{ return flavors;
}
public static final DataFlavor serializableFlavor
= new DataFlavor(java.io.Serializable.class,
"Serializable Object");
private static DataFlavor[] flavors
= { serializableFlavor };
private Serializable theObject;
}
class MimeClipboard extends Clipboard
{ public MimeClipboard(Clipboard cb)
{ super("MIME/" + cb.getName());
clip = cb;
}
public synchronized void setContents(Transferable contents,
ClipboardOwner owner)
{ if (contents instanceof SerializableSelection)
{ try
{ DataFlavor flavor
= SerializableSelection.serializableFlavor;
Serializable obj = (Serializable)
contents.getTransferData(flavor);
String enc = encode(obj);
String header = "Content-type: "
+ flavor.getMimeType()
+ "nContent-length: "
+ enc.length() + "nn";
StringSelection selection
= new StringSelection(header + enc);
clip.setContents(selection, owner);
}
catch(UnsupportedFlavorException e)
{}
catch(IOException e)
{}
}
else clip.setContents(contents, owner);
}
public synchronized Transferable getContents
(Object requestor)
{ Transferable contents = clip.getContents(requestor);
if (contents instanceof StringSelection)
{ String data = null;
try
{ data = (String)contents.getTransferData
(DataFlavor.stringFlavor);
}
catch(UnsupportedFlavorException e)
{ return contents; }
catch(IOException e)
{ return contents; }
if (!data.startsWith("Content-type: "))
return contents;
int start = -1;
// skip three newlines
for (int i = 0; i 2]);
super.write(toBase64[((inbuf[0] & 0x03) 4)]);
super.write(toBase64[((inbuf[1] & 0x0F) 6)]);
super.write(toBase64[inbuf[2] & 0x3F]);
col += 4;
i = 0;
if (col >= 76)
{ super.write('n');
col = 0;
}
}
}
public void flush() throws IOException
{ if (i == 1)
{ super.write(toBase64[(inbuf[0] & 0xFC) >> 2]);
super.write(toBase64[(inbuf[0] & 0x03) 2]);
super.write(toBase64[((inbuf[0] & 0x03) 4)]);
super.write(toBase64[(inbuf[1] & 0x0F)
* @version 1.10 1999-09-20
* @author Cay Horstmann
*/
import java.io.*;
import java.awt.*;
import java.awt.image.*;
import java.awt.event.*;
import java.awt.datatransfer.*;
import javax.swing.*;
public class MimeClipboardTest
{ public static void main(String [] args)
{ JFrame frame = new MimeClipboardFrame();
frame.show();
}
}
class MimeClipboardFrame extends JFrame
implements ActionListener
{ public MimeClipboardFrame()
{ setSize(300, 300);
setTitle("MimeClipboardTest");
addWindowListener(new WindowAdapter()
{ public void windowClosing(WindowEvent e)
{ System.exit(0);
}
} );
Container contentPane = getContentPane();
label = new JLabel();
contentPane.add(label, "Center");
JMenu fileMenu = new JMenu("File");
openItem = new JMenuItem("Open");
openItem.addActionListener(this);
fileMenu.add(openItem);
exitItem = new JMenuItem("Exit");
exitItem.addActionListener(this);
fileMenu.add(exitItem);
JMenu editMenu = new JMenu("Edit");
copyItem = new JMenuItem("Copy");
copyItem.addActionListener(this);
editMenu.add(copyItem);
pasteItem = new JMenuItem("Paste");
pasteItem.addActionListener(this);
editMenu.add(pasteItem);
JMenuBar menuBar = new JMenuBar();
menuBar.add(fileMenu);
menuBar.add(editMenu);
setJMenuBar(menuBar);
}
public void actionPerformed(ActionEvent evt)
{ Object source = evt.getSource();
if (source == openItem)
{ JFileChooser chooser = new JFileChooser();
chooser.setCurrentDirectory(new File("."));
chooser.setFileFilter(new
javax.swing.filechooser.FileFilter()
{ public boolean accept(File f)
{ String name = f.getName().toLowerCase();
return name.endsWith(".gif")
|| name.endsWith(".jpg")
|| name.endsWith(".jpeg")
|| f.isDirectory();
}
public String getDescription()
{ return "Image files";
}
});
int r = chooser.showOpenDialog(this);
if(r == JFileChooser.APPROVE_OPTION)
{ String name
= chooser.getSelectedFile().getAbsolutePath();
setImage(Toolkit.getDefaultToolkit().getImage(name));
}
}
else if (source == exitItem) System.exit(0);
else if (source == copyItem) copy();
else if (source == pasteItem) paste();
}
private void copy()
{ MediaTracker tracker = new MediaTracker(this);
tracker.addImage(theImage, 0);
try { tracker.waitForID(0); }
catch (InterruptedException e) {}
BufferedImage image
= new BufferedImage(theImage.getWidth(null),
theImage.getHeight(null),
BufferedImage.TYPE_INT_RGB);
Graphics2D g2 = image.createGraphics();
g2.drawImage(theImage, 0, 0, null);
Bitmap bitmap = new Bitmap(image);
SerializableSelection selection
= new SerializableSelection(bitmap);
mimeClipboard.setContents(selection, null);
}
private void paste()
{ Transferable selection
= mimeClipboard.getContents(this);
try
{ Bitmap bitmap = (Bitmap)selection.getTransferData
(SerializableSelection.serializableFlavor);
setImage(bitmap.getImage());
}
catch(Exception e) {}
}
public void setImage(Image image)
{ theImage = image;
label.setIcon(new ImageIcon(image));
}
private static Clipboard mimeClipboard
= new MimeClipboard
(Toolkit.getDefaultToolkit().getSystemClipboard());
private Image theImage;
private JLabel label;
private JMenuItem openItem;
private JMenuItem exitItem;
private JMenuItem copyItem;
private JMenuItem pasteItem;
}
class Bitmap implements Serializable
{ public Bitmap(BufferedImage image)
{ type = image.getType();
width = image.getWidth();
height = image.getHeight();
WritableRaster raster = image.getRaster();
data = raster.getDataElements(0, 0, width, height, null);
}
public BufferedImage getImage()
{ BufferedImage image
= new BufferedImage(width, height, type);
WritableRaster raster = image.getRaster();
raster.setDataElements(0, 0, width, height, data);
return image;
}
private int type;
private int width;
private int height;
private Object data;
}
class SerializableSelection implements Transferable
{ public SerializableSelection(Serializable object)
{ theObject = object;
}
public boolean isDataFlavorSupported(DataFlavor flavor)
{ return flavor.equals(serializableFlavor);
}
public synchronized Object getTransferData
(DataFlavor flavor)
throws UnsupportedFlavorException
{ if(flavor.equals(serializableFlavor))
{ return theObject;
}
else
{ throw new UnsupportedFlavorException(flavor);
}
}
public DataFlavor[] getTransferDataFlavors()
{ return flavors;
}
public static final DataFlavor serializableFlavor
= new DataFlavor(java.io.Serializable.class,
"Serializable Object");
private static DataFlavor[] flavors
= { serializableFlavor };
private Serializable theObject;
}
class MimeClipboard extends Clipboard
{ public MimeClipboard(Clipboard cb)
{ super("MIME/" + cb.getName());
clip = cb;
}
public synchronized void setContents(Transferable contents,
ClipboardOwner owner)
{ if (contents instanceof SerializableSelection)
{ try
{ DataFlavor flavor
= SerializableSelection.serializableFlavor;
Serializable obj = (Serializable)
contents.getTransferData(flavor);
String enc = encode(obj);
String header = "Content-type: "
+ flavor.getMimeType()
+ "nContent-length: "
+ enc.length() + "nn";
StringSelection selection
= new StringSelection(header + enc);
clip.setContents(selection, owner);
}
catch(UnsupportedFlavorException e)
{}
catch(IOException e)
{}
}
else clip.setContents(contents, owner);
}
public synchronized Transferable getContents
(Object requestor)
{ Transferable contents = clip.getContents(requestor);
if (contents instanceof StringSelection)
{ String data = null;
try
{ data = (String)contents.getTransferData
(DataFlavor.stringFlavor);
}
catch(UnsupportedFlavorException e)
{ return contents; }
catch(IOException e)
{ return contents; }
if (!data.startsWith("Content-type: "))
return contents;
int start = -1;
// skip three newlines
for (int i = 0; i 2]);
super.write(toBase64[((inbuf[0] & 0x03) 4)]);
super.write(toBase64[((inbuf[1] & 0x0F) 6)]);
super.write(toBase64[inbuf[2] & 0x3F]);
col += 4;
i = 0;
if (col >= 76)
{ super.write('n');
col = 0;
}
}
}
public void flush() throws IOException
{ if (i == 1)
{ super.write(toBase64[(inbuf[0] & 0xFC) >> 2]);
super.write(toBase64[(inbuf[0] & 0x03) 2]);
super.write(toBase64[((inbuf[0] & 0x03) 4)]);
super.write(toBase64[(inbuf[1] & 0x0F)