In my Java Spring web app I am creating an image file. This file gets a temporary name and later on I try to rename it using:
public void rename(String productFilename){
String newProductFilename = "newfile.jpg";
File input = new File(imageDir + "/products/" + productFilename);
File output = new File(imageDir + "/products/" + newProductFilename);
Boolean checkRename = input.renameTo(output);
}
For creating the temp file, I’m using:
public String generate(){
String productFilename = "filename.jpg";
ImageIO.write(out, imageFileType, new File(imageDir + "/products/" + productFilename));
return productFilename;
}
the value of imageDir is: /var/images
Throughout the class, the imageDir variable is set to an absolute path. The strange thing is that this all works great on Windows, but when running on Linux, I get a FileNotFoundException.
I’m 100% sure that the file exists. Any clue on what I’m doing wrong?
I found the solution. The filenames needed to be trimmed to be recognised in Linux. This, however, worked without trimming in Windows.