无法使用方法Toolkit.getDefaultToolkit()从文件名加载图像.getImage(filename);

时间:2014-03-27 06:51:57

标签: java servlets

我有使用方法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;

}

2 个答案:

答案 0 :(得分:0)

Image img = Toolkit.getDefaultToolkit().getImage(URL or file path);

Toolkit是java.awt包中的一个类,它为显示系统提供各种资源和工具。其中一个Toolkit方法是getImage(),其功能与Applet类中的getImage()方法非常相似。它被重载以获取指定图像文件位置的String filename参数或标识图像文件的URL参数。

在调用getImage()之前,必须引用正在使用的Toolkit实例。静态方法Toolkit.getDefaultToolkit()返回对该Toolkit的引用。

答案 1 :(得分:0)

不应该

BufferedImage thumbImage = getThumbnail(name, 200, 120, 0, name);

BufferedImage thumbImage = getThumbnail(UPLOAD_DIRECTORY + File.separator + name, 200, 120, 0, name);