Image recreated even after deletion by java.

A

Anonymous

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());
}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,013
Latest member
KatriceSwa

Latest Threads

Top