|
@@ -15,6 +15,7 @@ import org.springframework.web.multipart.commons.CommonsMultipartFile;
|
|
|
|
|
|
import java.io.*;
|
|
|
import java.net.URL;
|
|
|
+import java.nio.file.Files;
|
|
|
|
|
|
@Service
|
|
|
public class FileUtils {
|
|
@@ -316,6 +317,97 @@ public class FileUtils {
|
|
|
return DateUtils.getNowTimeFormat("yyyyMMdd");
|
|
|
}
|
|
|
|
|
|
+ public static boolean deleteQuietly(final File file) {
|
|
|
+ if (file == null) {
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ try {
|
|
|
+ if (file.isDirectory()) {
|
|
|
+ cleanDirectory(file);
|
|
|
+ }
|
|
|
+ } catch (final Exception ignored) {
|
|
|
+ }
|
|
|
+
|
|
|
+ try {
|
|
|
+ return file.delete();
|
|
|
+ } catch (final Exception ignored) {
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public static void cleanDirectory(final File directory) throws IOException {
|
|
|
+ final File[] files = verifiedListFiles(directory);
|
|
|
+
|
|
|
+ IOException exception = null;
|
|
|
+ for (final File file : files) {
|
|
|
+ try {
|
|
|
+ forceDelete(file);
|
|
|
+ } catch (final IOException ioe) {
|
|
|
+ exception = ioe;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ if (null != exception) {
|
|
|
+ throw exception;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private static File[] verifiedListFiles(final File directory) throws IOException {
|
|
|
+ if (!directory.exists()) {
|
|
|
+ final String message = directory + " does not exist";
|
|
|
+ throw new IllegalArgumentException(message);
|
|
|
+ }
|
|
|
+
|
|
|
+ if (!directory.isDirectory()) {
|
|
|
+ final String message = directory + " is not a directory";
|
|
|
+ throw new IllegalArgumentException(message);
|
|
|
+ }
|
|
|
+
|
|
|
+ final File[] files = directory.listFiles();
|
|
|
+ if (files == null) { // null if security restricted
|
|
|
+ throw new IOException("Failed to list contents of " + directory);
|
|
|
+ }
|
|
|
+ return files;
|
|
|
+ }
|
|
|
+
|
|
|
+ public static void forceDelete(final File file) throws IOException {
|
|
|
+ if (file.isDirectory()) {
|
|
|
+ deleteDirectory(file);
|
|
|
+ } else {
|
|
|
+ final boolean filePresent = file.exists();
|
|
|
+ if (!file.delete()) {
|
|
|
+ if (!filePresent) {
|
|
|
+ throw new FileNotFoundException("File does not exist: " + file);
|
|
|
+ }
|
|
|
+ final String message =
|
|
|
+ "Unable to delete file: " + file;
|
|
|
+ throw new IOException(message);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public static void deleteDirectory(final File directory) throws IOException {
|
|
|
+ if (!directory.exists()) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ if (!isSymlink(directory)) {
|
|
|
+ cleanDirectory(directory);
|
|
|
+ }
|
|
|
+
|
|
|
+ if (!directory.delete()) {
|
|
|
+ final String message =
|
|
|
+ "Unable to delete directory " + directory + ".";
|
|
|
+ throw new IOException(message);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public static boolean isSymlink(final File file) throws IOException {
|
|
|
+ if (file == null) {
|
|
|
+ throw new NullPointerException("File must not be null");
|
|
|
+ }
|
|
|
+ return Files.isSymbolicLink(file.toPath());
|
|
|
+ }
|
|
|
|
|
|
}
|
|
|
|