|
@@ -1,5 +1,6 @@
|
|
package org.springblade.core.tool.utils;
|
|
package org.springblade.core.tool.utils;
|
|
|
|
|
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
import org.apache.commons.lang3.StringUtils;
|
|
import org.apache.commons.lang3.StringUtils;
|
|
import org.springframework.lang.Nullable;
|
|
import org.springframework.lang.Nullable;
|
|
import org.springframework.util.Assert;
|
|
import org.springframework.util.Assert;
|
|
@@ -10,18 +11,7 @@ import org.springframework.web.context.request.ServletRequestAttributes;
|
|
import org.springframework.web.multipart.MultipartFile;
|
|
import org.springframework.web.multipart.MultipartFile;
|
|
|
|
|
|
import javax.servlet.http.HttpServletRequest;
|
|
import javax.servlet.http.HttpServletRequest;
|
|
-import java.io.BufferedInputStream;
|
|
|
|
-import java.io.BufferedOutputStream;
|
|
|
|
-import java.io.File;
|
|
|
|
-import java.io.FileFilter;
|
|
|
|
-import java.io.FileInputStream;
|
|
|
|
-import java.io.FileNotFoundException;
|
|
|
|
-import java.io.FileOutputStream;
|
|
|
|
-import java.io.IOException;
|
|
|
|
-import java.io.InputStream;
|
|
|
|
-import java.io.OutputStream;
|
|
|
|
-import java.io.Serializable;
|
|
|
|
-import java.io.UnsupportedEncodingException;
|
|
|
|
|
|
+import java.io.*;
|
|
import java.net.URLEncoder;
|
|
import java.net.URLEncoder;
|
|
import java.nio.charset.Charset;
|
|
import java.nio.charset.Charset;
|
|
import java.nio.file.Files;
|
|
import java.nio.file.Files;
|
|
@@ -35,6 +25,7 @@ import java.util.zip.ZipOutputStream;
|
|
*
|
|
*
|
|
* @author L.cm
|
|
* @author L.cm
|
|
*/
|
|
*/
|
|
|
|
+@Slf4j
|
|
public class FileUtil extends org.springframework.util.FileCopyUtils {
|
|
public class FileUtil extends org.springframework.util.FileCopyUtils {
|
|
|
|
|
|
/**
|
|
/**
|
|
@@ -311,7 +302,7 @@ public class FileUtil extends org.springframework.util.FileCopyUtils {
|
|
* @param file File
|
|
* @param file File
|
|
*/
|
|
*/
|
|
public static void toFile(InputStream in, final File file) {
|
|
public static void toFile(InputStream in, final File file) {
|
|
- try (OutputStream out = new FileOutputStream(file)) {
|
|
|
|
|
|
+ try (OutputStream out = Files.newOutputStream(file.toPath())) {
|
|
FileUtil.copy(in, out);
|
|
FileUtil.copy(in, out);
|
|
} catch (IOException e) {
|
|
} catch (IOException e) {
|
|
throw Exceptions.unchecked(e);
|
|
throw Exceptions.unchecked(e);
|
|
@@ -372,7 +363,14 @@ public class FileUtil extends org.springframework.util.FileCopyUtils {
|
|
|
|
|
|
}
|
|
}
|
|
try {
|
|
try {
|
|
- return file.delete();
|
|
|
|
|
|
+ // 删除临时文件
|
|
|
|
+ boolean delete = file.delete();
|
|
|
|
+
|
|
|
|
+ if (!delete) {
|
|
|
|
+ log.error("文件删除失败!!!");
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ return delete;
|
|
} catch (final Exception ignored) {
|
|
} catch (final Exception ignored) {
|
|
return false;
|
|
return false;
|
|
}
|
|
}
|
|
@@ -459,7 +457,7 @@ public class FileUtil extends org.springframework.util.FileCopyUtils {
|
|
BufferedInputStream bis = null;
|
|
BufferedInputStream bis = null;
|
|
FileOutputStream fos = null;
|
|
FileOutputStream fos = null;
|
|
ZipOutputStream zos = null;
|
|
ZipOutputStream zos = null;
|
|
- if (sourceFile.exists() == false) {
|
|
|
|
|
|
+ if (!sourceFile.exists()) {
|
|
System.out.println("待压缩的文件目录:" + sourceFilePath + "不存在.");
|
|
System.out.println("待压缩的文件目录:" + sourceFilePath + "不存在.");
|
|
} else {
|
|
} else {
|
|
try {
|
|
try {
|
|
@@ -474,12 +472,12 @@ public class FileUtil extends org.springframework.util.FileCopyUtils {
|
|
fos = new FileOutputStream(zipFile);
|
|
fos = new FileOutputStream(zipFile);
|
|
zos = new ZipOutputStream(new BufferedOutputStream(fos));
|
|
zos = new ZipOutputStream(new BufferedOutputStream(fos));
|
|
byte[] bufs = new byte[1024 * 10];
|
|
byte[] bufs = new byte[1024 * 10];
|
|
- for (int i = 0; i < sourceFiles.length; i++) {
|
|
|
|
- //创建ZIP实体,并添加进压缩包
|
|
|
|
- ZipEntry zipEntry = new ZipEntry(sourceFiles[i].getName());
|
|
|
|
|
|
+ for (File file : sourceFiles) {
|
|
|
|
+ // 创建ZIP实体,并添加进压缩包
|
|
|
|
+ ZipEntry zipEntry = new ZipEntry(file.getName());
|
|
zos.putNextEntry(zipEntry);
|
|
zos.putNextEntry(zipEntry);
|
|
- //读取待压缩的文件并写进压缩包里
|
|
|
|
- fis = new FileInputStream(sourceFiles[i]);
|
|
|
|
|
|
+ // 读取待压缩的文件并写进压缩包里
|
|
|
|
+ fis = new FileInputStream(file);
|
|
bis = new BufferedInputStream(fis, 1024 * 10);
|
|
bis = new BufferedInputStream(fis, 1024 * 10);
|
|
int read = 0;
|
|
int read = 0;
|
|
while ((read = bis.read(bufs, 0, 1024 * 10)) != -1) {
|
|
while ((read = bis.read(bufs, 0, 1024 * 10)) != -1) {
|