|
@@ -1,6 +1,7 @@
|
|
|
package api.common.util;
|
|
|
|
|
|
import lombok.SneakyThrows;
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
import org.apache.commons.compress.archivers.tar.TarArchiveEntry;
|
|
|
import org.apache.commons.compress.archivers.tar.TarArchiveInputStream;
|
|
|
import org.apache.commons.compress.compressors.gzip.GzipCompressorInputStream;
|
|
@@ -15,6 +16,7 @@ import java.nio.file.Files;
|
|
|
import java.nio.file.Paths;
|
|
|
import java.util.*;
|
|
|
|
|
|
+@Slf4j
|
|
|
public class FileUtil {
|
|
|
|
|
|
/**
|
|
@@ -1016,7 +1018,7 @@ public class FileUtil {
|
|
|
* 将字符串保存为本地文件
|
|
|
*/
|
|
|
@SneakyThrows
|
|
|
- public static void writeStringToLocalFile(String string, String filePath) throws IOException {
|
|
|
+ public static void writeStringToLocalFile(String string, String filePath) {
|
|
|
writeInputStreamToLocalFile(new ByteArrayInputStream(string.getBytes(StandardCharsets.UTF_8)), filePath);
|
|
|
}
|
|
|
|
|
@@ -1024,7 +1026,7 @@ public class FileUtil {
|
|
|
* 将字符串保存为本地文件
|
|
|
*/
|
|
|
@SneakyThrows
|
|
|
- public static void writeStringToLocalFile(String string, String filePath, int bufferLength) throws IOException {
|
|
|
+ public static void writeStringToLocalFile(String string, String filePath, int bufferLength) {
|
|
|
writeInputStreamToLocalFile(new ByteArrayInputStream(string.getBytes(StandardCharsets.UTF_8)), filePath, bufferLength);
|
|
|
}
|
|
|
|
|
@@ -1032,25 +1034,34 @@ public class FileUtil {
|
|
|
/**
|
|
|
* 将输入流保存为本地文件
|
|
|
*/
|
|
|
- public static void writeInputStreamToLocalFile(InputStream inputStream, String filePath) throws IOException {
|
|
|
+ public static void writeInputStreamToLocalFile(InputStream inputStream, String filePath) {
|
|
|
writeInputStreamToLocalFile(inputStream, filePath, 4096);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 将输入流保存为本地文件
|
|
|
*/
|
|
|
- public static void writeInputStreamToLocalFile(InputStream inputStream, String filePath, int bufferLength) throws IOException {
|
|
|
- BufferedInputStream bufferedInputStream = new BufferedInputStream(inputStream);
|
|
|
- byte[] data = new byte[bufferLength];
|
|
|
- int dataLength;
|
|
|
- File file = new File(filePath);
|
|
|
- createParentDirectory(file);
|
|
|
- BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(new FileOutputStream(file, false));
|
|
|
- while ((dataLength = bufferedInputStream.read(data)) != -1) {
|
|
|
- bufferedOutputStream.write(data, 0, dataLength);
|
|
|
+ public static void writeInputStreamToLocalFile(InputStream inputStream, String filePath, int bufferLength) {
|
|
|
+ try {
|
|
|
+ final File file1 = new File(filePath);
|
|
|
+ if(file1.exists()){
|
|
|
+ file1.delete();
|
|
|
+ }
|
|
|
+ BufferedInputStream bufferedInputStream = new BufferedInputStream(inputStream);
|
|
|
+ byte[] data = new byte[bufferLength];
|
|
|
+ int dataLength;
|
|
|
+ File file = new File(filePath);
|
|
|
+ createParentDirectory(file);
|
|
|
+ BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(new FileOutputStream(file, false));
|
|
|
+ while ((dataLength = bufferedInputStream.read(data)) != -1) {
|
|
|
+ bufferedOutputStream.write(data, 0, dataLength);
|
|
|
+ }
|
|
|
+ bufferedOutputStream.close();
|
|
|
+ bufferedInputStream.close();
|
|
|
+ } catch (IOException e) {
|
|
|
+ log.error(e.getMessage());
|
|
|
+ throw new RuntimeException(e);
|
|
|
}
|
|
|
- bufferedOutputStream.close();
|
|
|
- bufferedInputStream.close();
|
|
|
}
|
|
|
|
|
|
/**
|