利用ImageIO.write(image,extension,file);
请参阅下面的代码片段:
JFileChooser FC=new JFileChooser("C:/");
FC.addChoosableFileFilter(new jpgSaveFilter());
FC.addChoosableFileFilter(new jpegSaveFilter());
FC.addChoosableFileFilter(new PngSaveFilter());
FC.addChoosableFileFilter(new gifSaveFilter());
FC.addChoosableFileFilter(new BMPSaveFilter());
FC.addChoosableFileFilter(new wbmpSaveFilter());
int retrival=m_fileChooser_save.showSaveDialog(null);
if (retrival == m_fileChooser_save.APPROVE_OPTION)
{
String ext="";
String extension=m_fileChooser_save.getFileFilter().getDescription();
if(extension.equals("*.jpg,*.JPG"))
{
ext=".jpg";
}
if(extension.equals("*.png,*.PNG"))
{
ext=".png";
}
if(Extension.equals("*.gif,*.GIF"))
{
ext=".gif";
}
if(extension.equals("*.wbmp,*.WBMP"))
{
ext=".wbmp";
}
if(Extension.equals("*.jpeg,*.JPEG"))
{
EXT=".jpeg";
}
if(extension.equals("*.bmp,*.BMP"))
{
ext=".bmp";
}
示例过滤器:
import java.io.*;
import java.io.File;
import java.util.*;
import javax.swing.filechooser.FileFilter;
class jpgSaveFilter extends FileFilter
{
public boolean accept(File f)
{
if (f.isDirectory())
{
return false;
}
String s = f.getName();
return s.endsWith(".jpg")||s.endsWith(".JPG");
}
public String getDescription()
{
return "*.jpg,*.JPG";
}
}