除非重新启动TomCat,否则图像文件不会被覆盖

时间:2014-03-12 07:11:55

标签: java image tomcat

我创建了一个java Web应用程序。我在其中创建了调整大小和上传照片的程序。这存储在特定文件夹中。当我更改照片时,它不会改变并保留旧照片。但重新启动TomCat后,我可以更改照片。为什么会这样?以下是调整大小和存储图像的代码

  public static int createThumbnailNew(String original,
            String resized, int maxSize) {
        try
        {
            File originalFile = new File(original);
            ImageIcon ii = new ImageIcon(originalFile.getCanonicalPath());
            Image i = ii.getImage();
            int iWidth = i.getWidth(null);
            int iHeight = i.getHeight(null);
            BufferedImage originalImage = new BufferedImage(
                    i.getWidth(null), i.getHeight(null),
                    BufferedImage.TYPE_INT_RGB);
            // Copy image to buffered image.
            Graphics g = originalImage.createGraphics();

            // Clear background and paint the image.
            g.setColor(Color.white);
            g.fillRect(0, 0, i.getWidth(null), i.getHeight(null));
            g.drawImage(i, 0, 0, null);
            g.dispose();
            BufferedImage bufferedImage = null;
            if (iWidth > iHeight) {
                bufferedImage = resizeImage(originalImage, BufferedImage.TYPE_INT_RGB,(maxSize * iHeight)/iWidth,maxSize);
            } else {
                bufferedImage = resizeImage(originalImage, BufferedImage.TYPE_INT_RGB,maxSize,(maxSize * iWidth) / iHeight);
            }

            //BufferedImage croppedImage=cropImage(bufferedImage,crX,crY,crH,crW);


            File file = new File(resized);

            FileOutputStream out = new FileOutputStream(file);

            /* encodes image as a JPEG data stream */
            JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);

            com.sun.image.codec.jpeg.JPEGEncodeParam param = encoder
                    .getDefaultJPEGEncodeParam(bufferedImage);

            // writeParam = new JPEGImageWriteParam(null);
            // writeParam.setCompressionMode(JPEGImageWriteParam.MODE_EXPLICIT);
            // writeParam.setProgressiveMode(JPEGImageWriteParam.MODE_DEFAULT);
            param.setQuality(1.0f, true);
            encoder.setJPEGEncodeParam(param);
            encoder.encode(bufferedImage);


        }
        catch(Exception e)
        {
            return -1;
        }
        return 0;
    }
    private  static BufferedImage resizeImage(BufferedImage originalImage, int type,int h,int w){
    BufferedImage resizedImage = new BufferedImage(w, h, type);
    Graphics2D g = resizedImage.createGraphics();
    g.drawImage(originalImage, 0, 0, w, h, null);
    g.dispose();

    return resizedImage;
    }

请帮助..谢谢..

1 个答案:

答案 0 :(得分:1)

您尚未关闭已打开的文件。这是一个可能的问题。