|
|
|
@ -2,6 +2,7 @@ package com.java3y.austin.support.utils;
|
|
|
|
|
|
|
|
|
|
import cn.hutool.core.io.IoUtil;
|
|
|
|
|
import com.google.common.base.Throwables;
|
|
|
|
|
import com.java3y.austin.common.constant.CommonConstant;
|
|
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
|
|
|
|
|
|
|
import java.io.File;
|
|
|
|
@ -33,15 +34,27 @@ public class AustinFileUtils {
|
|
|
|
|
* @return
|
|
|
|
|
*/
|
|
|
|
|
public static File getRemoteUrl2File(String path, String remoteUrl) {
|
|
|
|
|
|
|
|
|
|
InputStream inputStream = null;
|
|
|
|
|
FileOutputStream fileOutputStream = null;
|
|
|
|
|
try {
|
|
|
|
|
URL url = new URL(remoteUrl);
|
|
|
|
|
String protocol = url.getProtocol();
|
|
|
|
|
// 防止SSRF攻击
|
|
|
|
|
if (!CommonConstant.HTTP.equalsIgnoreCase(protocol)
|
|
|
|
|
&& !CommonConstant.HTTPS.equalsIgnoreCase(protocol)
|
|
|
|
|
&& !CommonConstant.OSS.equalsIgnoreCase(protocol)) {
|
|
|
|
|
log.error("AustinFileUtils#getRemoteUrl2File fail:{}, remoteUrl:{}",
|
|
|
|
|
"The remoteUrl is invalid, it can only be of the types http, https, and oss.", remoteUrl);
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
File file = new File(path, url.getPath());
|
|
|
|
|
inputStream = url.openStream();
|
|
|
|
|
if (!file.exists()) {
|
|
|
|
|
file.getParentFile().mkdirs();
|
|
|
|
|
boolean res = file.getParentFile().mkdirs();
|
|
|
|
|
if (!res) {
|
|
|
|
|
log.error("AustinFileUtils#getRemoteUrl2File Failed to create folder, path:{}, remoteUrl:{}", path, remoteUrl);
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
fileOutputStream = new FileOutputStream(file);
|
|
|
|
|
IoUtil.copy(inputStream, fileOutputStream);
|
|
|
|
|
}
|
|
|
|
@ -49,20 +62,8 @@ public class AustinFileUtils {
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
log.error("AustinFileUtils#getRemoteUrl2File fail:{},remoteUrl:{}", Throwables.getStackTraceAsString(e), remoteUrl);
|
|
|
|
|
} finally {
|
|
|
|
|
if (Objects.nonNull(inputStream)) {
|
|
|
|
|
try {
|
|
|
|
|
inputStream.close();
|
|
|
|
|
} catch (IOException e) {
|
|
|
|
|
log.error("close#inputStream fail:{}", Throwables.getStackTraceAsString(e));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (Objects.nonNull(fileOutputStream)) {
|
|
|
|
|
try {
|
|
|
|
|
fileOutputStream.close();
|
|
|
|
|
} catch (IOException e) {
|
|
|
|
|
log.error("close#fileOutputStream fail:{}", Throwables.getStackTraceAsString(e));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
closeQuietly(inputStream);
|
|
|
|
|
closeQuietly(fileOutputStream);
|
|
|
|
|
}
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
@ -85,4 +86,33 @@ public class AustinFileUtils {
|
|
|
|
|
return files;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 关闭InputStream流
|
|
|
|
|
*
|
|
|
|
|
* @param inputStream
|
|
|
|
|
*/
|
|
|
|
|
private static void closeQuietly(InputStream inputStream) {
|
|
|
|
|
if (Objects.nonNull(inputStream)) {
|
|
|
|
|
try {
|
|
|
|
|
inputStream.close();
|
|
|
|
|
} catch (IOException e) {
|
|
|
|
|
log.error("close#inputStream fail:{}", Throwables.getStackTraceAsString(e));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 关闭FileOutputStream流
|
|
|
|
|
*
|
|
|
|
|
* @param fileOutputStream
|
|
|
|
|
*/
|
|
|
|
|
private static void closeQuietly(FileOutputStream fileOutputStream) {
|
|
|
|
|
if (Objects.nonNull(fileOutputStream)) {
|
|
|
|
|
try {
|
|
|
|
|
fileOutputStream.close();
|
|
|
|
|
} catch (IOException e) {
|
|
|
|
|
log.error("close#fileOutputStream fail:{}", Throwables.getStackTraceAsString(e));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|