Image recreated even after deletion by java.

From: Anonymous (follow_me_69_au_at_hotmail.com)
Date: 11/27/04


Date: 26 Nov 2004 22:02:11 -0800

Hi, I have a very weird problem.

I have two methods. one to get an uploded image from a web page (which
works with many different images), another to convert that uploaded
image into a thumbnail. So heres the story...
The thumbnail constantly remains the same, no matter what file I
upload. I have tryed manually deleting both the uploaded file and the
thumbnail, but the thumbnail is re-created, the exact same image as
before EVEN after their both deleted.
Its like theres a ghost in my system... maybe I need to call the
garbage collector to clean up the image in memory or something?
here is the code...

setImage gets the uploaded image and writes to file(works fine).
makeThumbNail (which is called within setImage) always makes the same
thumbnail no matter what you upload and even if you delete the old
image & thumbnail image.

    public String setImage(HttpServletRequest request,
HttpServletResponse response)
    throws ValidationException{
        //Save the image to a file in dcpc/images folder using the
file name
        //<productId>.jpg you need two \\'s as \ means go to next
character in java.
        String productID = null;
        String fileNameToSave;
        DiskFileUpload upload;
        java.util.List items;
        FileItem picture = null;
        File fileTo;
        FileOutputStream fileOutputStream;
        boolean isMultiPart = false;
        
        //Create a new upload handler
        upload = new DiskFileUpload();
        isMultiPart = upload.isMultipartContent(request);
        if( !(request== null) && isMultiPart) {
            try {
                //Get a list of the items uploaded including normal
form fields.
                items = upload.parseRequest(request);
                
                // Get the picture & product ID from the list
                Iterator iterator = items.iterator();
                while (iterator.hasNext()) {
                    FileItem item = (FileItem) iterator.next();
                    if (item.getFieldName().equals("productID")) {
                        productID = item.getString();
                        System.out.println("Product ID: " +
productID);
                    }
                    if (item.isFormField() == false) {
                        picture = item;
                    }
                }

                fileNameToSave = "C:\\Tomcat\\webapps\\dcpc\\images\\"
+ productID + ".jpg";
                System.out.println("TO:" + fileNameToSave);
                //Delete old Image & Create reference to new Image
                fileTo = new File(fileNameToSave);
                System.out.println(fileTo.length());
                while(fileTo.exists()){
                    if(fileTo.delete()){
                        System.out.println("Existing image deleted");
                        continue;
                    }
                }
                //Write to file
                long pictureSize = picture.getSize();
                System.out.println(fileTo.length());
                System.out.println("File being written.");
                picture.write(fileTo);
                System.out.println(pictureSize);
                System.out.println(fileTo.length());
                while(fileTo.length() != pictureSize){
                    System.out.print(".");
                }
                //Save ThumbNail
                String thumbNailFile =
"C:\\Tomcat\\webapps\\dcpc\\images\\" + productID + "thumbnail.jpg";
                //Give the system time to write the file if it is
large.
                makeThumbNail(150, 150, 90, fileNameToSave,
thumbNailFile, response, productID);
            }catch(IOException ioe) {
                ioe.printStackTrace();
                return "<div class='error'>Failed. System
Error</div>";
            }catch(NullPointerException npe){
                return null;
            }catch(Exception e){
                e.printStackTrace();
                return "<div class='error'>Failed. System
Error</div>";
            }
        }
        return productID;
    }
    
    public void makeThumbNail(int width, int height, int quality,
String fileIn, String fileOut, HttpServletResponse response, String
productID) {
        Image image;
        MediaTracker mediaTracker;
        BufferedImage thumbImage;
        BufferedOutputStream out;
        JPEGImageEncoder jpegEncoder;
        JPEGEncodeParam param;
        try {
            //Delete the existing thumbnail, if it exists
            File fFileOut = new File(fileOut);
            while(fFileOut.exists()) {
                if(fFileOut.delete()){
                    System.out.println("Existing thumbnail deleted");
                    continue;
                }
            }
            System.out.println(fileIn);
            //thumbnail no longer exists. this is confirmed.
            //Get the image from fileIn
            
            image = Toolkit.getDefaultToolkit().getImage(fileIn);
            mediaTracker = new MediaTracker(new Container());
            mediaTracker.addImage(image, 0);
            mediaTracker.waitForID(0);

            double thumbRatio = (double)width / (double)height;
            
            int imageWidth = image.getWidth(null);
            int imageHeight = image.getHeight(null);
            double imageRatio = (double)imageWidth /
(double)imageHeight;
            if (thumbRatio < imageRatio) {
              height = (int)(width / imageRatio);
            } else {
              width = (int)(height * imageRatio);
            }
            
            // Shrink original image to the thumbnail size
specifications
            thumbImage = new BufferedImage(width, height,
BufferedImage.TYPE_INT_RGB);
            Graphics2D graphics2D = thumbImage.createGraphics();
            graphics2D.setRenderingHint(RenderingHints.KEY_INTERPOLATION,
              RenderingHints.VALUE_INTERPOLATION_BILINEAR);
            graphics2D.drawImage(image, 0, 0, width, height, null);
            // Create output stream
            out = new BufferedOutputStream(new
FileOutputStream(fileOut));
            // Create jpeg encoder that writes to output stream
            jpegEncoder = JPEGCodec.createJPEGEncoder(out);
            // Get JPEG Encoder's default encode param so we can set
quality
            param = jpegEncoder.getDefaultJPEGEncodeParam(thumbImage);
            // Set the quality of the image to int quality
            quality = Math.max(0, Math.min(quality, 100));
            param.setQuality((float)quality / 100.0f, false);
            // Set the JPEG Encoders param to the new param.
            jpegEncoder.setJPEGEncodeParam(param);
            // Tell jpeg encoder to write to fileOut
            jpegEncoder.encode(thumbImage);
            // Close the output stream because on some operating
systems (e.g Windows)
            // The file will be locked by this program & unusable by
other programs unless
            // you close the output stream.
            out.close();
            //Redirect the browser to the page with the new picture.
            response.setHeader("Refresh", "1;
URL=products.jsp?productID="+productID+"&productName=&description=&price=&unitsInStock=&unitsSold=&pictureUploading=Yes&operation=Search+for+Product%2Fs");
        }catch(Exception e){
            System.out.println(e.getMessage());
        }
    }



Relevant Pages

  • Image recreated even after deletion by java.
    ... The thumbnail constantly remains the same, ... thumbnail no matter what you upload and even if you delete the old ... productID); ... String fileIn, String fileOut, HttpServletResponse response, String ...
    (comp.lang.java.help)
  • Re: Upload photo?
    ... Microsoft MVP-FrontPage ... | just have the user upload the larger photo and then dynamically resize ... I can manually place the thumbnail in ...
    (microsoft.public.frontpage.client)
  • Re: Upload photo?
    ... | photos, as well as writing code that would allow the photo to be stored in two sizes or you could ... | just have the user upload the larger photo and then dynamically resize when displayed in the ... I can manually place the thumbnail in the ... |> cell and also have the upload script in that same cell just below the ...
    (microsoft.public.frontpage.client)
  • Re: Photo Gallery web manager thingy.
    ... > As you can see from the subject heading I don't know what the fuck I'm ... > like nice little viewers with thumbnail and full views. ... Faffing around with 3rd party graphic packages everytime you want to upload ... the required folder, rename it and resize it, ...
    (uk.rec.motorcycles)
  • Re: Http post
    ... Now if you were for example to build a string out of these 3 things ... Two separate HTTP Post request are required to complete the upload ... The example you sent me works fine and the authentication id can be ... perhaps just simple text string with an attachment (in binary format) ...
    (microsoft.public.fox.programmer.exchange)