我在使用Toolkit.getDefaultToolkit()。getImage(filename)方法按文件名加载图像时遇到问题; MediaTracker返回false,这表示没有加载图片吗?如何解决这个问题呢?提前致谢。下面是我的代码:
在doPost方法中:
//process only if it is multipart content
if(ServletFileUpload.isMultipartContent(request)){
try {
List<FileItem> multiparts = new ServletFileUpload(new DiskFileItemFactory()).parseRequest(request);
for(FileItem item : multiparts){
if(!item.isFormField()){
String name = new File(item.getName()).getName();
item.write( new File(UPLOAD_DIRECTORY + File.separator + name));
BufferedImage thumbImage = getThumbnail(name, 200, 120, 0, name);
File outputfile = new File(UPLOAD_DIRECTORY + File.separator + "2"+name);
ImageIO.write(thumbImage, "jpg", outputfile);
}
}
getThumbnail方法:
private static BufferedImage getThumbnail(String filename, int thumbWidth, int thumbHeight, int quality, String outFileName)
throws InterruptedException, FileNotFoundException, IOException {
//load image from filename
Image image = Toolkit.getDefaultToolkit().getImage(filename);
MediaTracker mediaTracker = new MediaTracker(new Container());
mediaTracker.addImage(image, 0);
mediaTracker.waitForID(0);
// use this to test for errors at this point:
System.out.println(mediaTracker.isErrorAny());
//determine thumbnail size from WIDTH and HEIGHT
double thumbRatio = (double)thumbWidth / (double)thumbHeight;
int imageWidth = image.getWidth(null);
int imageHeight = image.getHeight(null);
double imageRatio = (double)imageWidth / (double) imageHeight;
if (thumbRatio < imageRatio){
thumbHeight = (int)(thumbWidth / imageRatio);
} else{
thumbWidth = (int)(thumbHeight * imageRatio);
}
//draw original image to thumbnail image object and
//scale it to the new size
BufferedImage thumbImage = new BufferedImage(thumbWidth, thumbHeight, BufferedImage.TYPE_INT_RGB);
Graphics2D graphics2D = thumbImage.createGraphics();
graphics2D.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
graphics2D.drawImage(image, 0, 0, thumbWidth, thumbHeight, null);
//save thumbnail image to outFileName
BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(outFileName));
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
JPEGEncodeParam param = encoder.getDefaultJPEGEncodeParam(thumbImage);
quality = Math.max(0, Math.min(quality, 100));
param.setQuality((float)quality / 100.0f, false);
encoder.setJPEGEncodeParam(param);
encoder.encode(thumbImage);
out.close();
return thumbImage;
}
不应
BufferedImage thumbImage = getThumbnail(name, 200, 120, 0, name);
是
BufferedImage thumbImage = getThumbnail(UPLOAD_DIRECTORY + File.separator + name, 200, 120, 0, name);
?
Image img = Toolkit.getDefaultToolkit().getImage(URL or file path);
Toolkit是java.awt包中的类,它为显示系统提供各种资源和工具。 Toolkit方法之一是getImage()
,其功能与Applet类中的getImage()
方法非常相似。可以重载以指定图像文件位置的String文件名参数或标识图像文件的URL参数。
在调用getImage()之前,必须先对正在使用的Toolkit实例进行引用。静态方法Toolkit.getDefaultToolkit()
返回对该工具箱的引用。
我有相同的问题,正在使用Toolkit.getDefaultToolkit()。getImage(String fiele)。我在文件夹图片中有我的照片,因此字符串文件为“ Pictures / Foto.png”。奇怪的是,有些图片可以显示,而另一些则不能。