Im building an app that allows photos to be uploaded to a server. I had it going perfect with jpeg and gif images only but I wanted to support tiffs and if they uploaded a tiff, convert it to a jpeg.
Anyways I have it all working fine .... not 100% thrilled with the logic yet but its getting there. Im having a problem though deleting the tiffs once Ive created the new jpeg. I know its a file lock someplace because I cant delete them thru Windows explorer either but MAN I can find the stinking lock!
Here's the code that calls the convertor (its done with a try catch as they ARENT supposed to be uploading TIFFs:
java.io.FileInputStream in = new java.io.FileInputStream(sc.getRealPath("/images/galleries/"+galleryID+"/")+images.elementAt(x));
com.sun.image.codec.jpeg.JPEGImageDecoder decoder = null;
java.awt.image.BufferedImage image = null;
try{
decoder = com.sun.image.codec.jpeg.JPEGCodec.createJPEGDecoder(in);
image = decoder.decodeAsBufferedImage();
}
catch (com.sun.image.codec.jpeg.ImageFormatException ife) {
in.close();
String jpeg = new ConvertTiff().convertTiff((String)images.elementAt(x), sc, galleryID);
in = new java.io.FileInputStream(sc.getRealPath("/images/galleries/"+galleryID+"/")+jpeg);
decoder = com.sun.image.codec.jpeg.JPEGCodec.createJPEGDecoder(in);
image = decoder.decodeAsBufferedImage();
images.remove(x);
images.add(x, jpeg);
}
in.close();
AND here is the ConvertTiff class:
class ConvertTiff{
public String convertTiff(String tiffFile, javax.servlet.ServletContext sc, String galleryID) throws java.io.IOException{
String fileName = tiffFile.substring(0, tiffFile.indexOf("."));
String outputFile = fileName+".jpg";
String fullOut = sc.getRealPath("/images/galleries/"+galleryID+"/")+fileName+".jpg";
// Load the input image.
javax.media.jai.RenderedOp src = javax.media.jai.JAI.create("fileload", sc.getRealPath("/images/galleries/"+galleryID+"/")+tiffFile);
// Encode the file as a JPEG image.
java.io.FileOutputStream stream = new java.io.FileOutputStream(fullOut);
javax.media.jai.JAI.create("encode", src, stream, "JPEG", null);
// Store the image in the BMP format.
javax.media.jai.JAI.create("filestore", src, fullOut, "JPEG", null);
stream.close();
java.io.File oldFile = new java.io.File(sc.getRealPath("/images/galleries/"+galleryID+"/")+tiffFile);
if (oldFile.exists()){
oldFile.delete();
}
return outputFile;
}
};
Now I AM getting into the if oldFile.exists() with no problem ... but as I said its not deleting either in my app OR in Windows explorer so Ive got a lock on the file someplace ... Im just not seeing it..
Thanks in advance!