Today, there is a problem with dog blood, a function in the development environment is not a problem, but in the production environment error.
The code is as follows:
...
File tmpFile = new File(fileTmpPath);
File newFileTarget = new File(filePath);
tmpFile.renameTo(newFileTarget);
// Modify the permissions of the new file
FileManageHelper.chmod(newFileTarget);
....
The last error message indicates that the Execute chmod command failed, but this code is not a problem in the development environment.
A closer look at the reasons for discovering the JDK Renameto method is described below:
/**
* Renames the file denoted by this abstract pathname.
*
* <p> Many aspects of the behavior of this method are inherently
* platform-dependent: The rename operation might not be able to move a
* file from one filesystem to another, it might not be atomic, and it
* might not succeed if a file with the destination abstract pathname
* already exists. The return value should always be checked to make sure
* that the rename operation was successful.
*
* <p> Note that the {@link java.nio.file.Files} class defines the {@link
* java.nio.file.Files#move move} method to move or rename a file in a
* platform independent manner.
*
* @param dest The new abstract pathname for the named file
*
* @return <code>true</code> if and only if the renaming succeeded;
* <code>false</code> otherwise
*
* @throws SecurityException
* If a security manager exists and its <code>{@link
* java.lang.SecurityManager#checkWrite(java.lang.String)}</code>
* method denies write access to either the old or new pathnames
*
* @throws NullPointerException
* If parameter <code>dest</code> is <code>null</code>
*/
The rename operation might is able to move a file from one filesystem to another
That is, if the file is to move files from one file system to another file system it is possible to fail, just as the development environment on Tmpfile and Newfiletarget in the same file system, and in the production environment due to the HA scenario, these two files are in different file systems.
Lesson: Be sure to check the return values of file related operations, such as Setlastmodified, Setreadonly, setwritable, setreadable, setexecutable, CreateNewFile, delete, mkdir, Mkdirs
In particular, check the return value of the Java file operation related methods