2014-03-12 22 views
0

我創建了一個java web應用程序。我在其中創建了調整大小和上傳照片的程序。這存儲在特定的文件夾中。當我更改照片時,它不會更改,並保留舊照片。但重新啓動TomCat後,我可以更改照片。爲什麼會發生?這裏是調整代碼和店面形象圖像文件不覆蓋,除非重新啓動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

嘗試關閉您的文件和輸出流 –

+0

@ Deepak213我將圖像存儲到特定的文件夾中。 – bijin

回答

1

您還沒有關閉了打開的文件。這是一個可能的問題。