简化下载文件 io 操作

pull/22/head
kl 2021-01-28 15:55:42 +08:00
parent 212526d989
commit d95fbe02bd
1 changed files with 3 additions and 10 deletions

View File

@ -4,6 +4,7 @@ import cn.keking.config.ConfigConstants;
import cn.keking.model.FileAttribute;
import cn.keking.model.ReturnResponse;
import io.mola.galimatias.GalimatiasParseException;
import org.apache.commons.io.FileUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@ -37,16 +38,8 @@ public class DownloadUtils {
try {
URL url = WebUtils.normalizedURL(urlStr);
if (isHttpUrl(url)) {
URLConnection connection = url.openConnection();
InputStream is = connection.getInputStream();
FileOutputStream os = new FileOutputStream(realPath);
byte[] buffer = new byte[4 * 1024];
int read;
while ((read = is.read(buffer)) > 0) {
os.write(buffer, 0, read);
}
os.close();
is.close();
File realFile = new File(realPath);
FileUtils.copyURLToFile(url,realFile);
} else if (isFtpUrl(url)) {
String ftpUsername = WebUtils.getUrlParameterReg(fileAttribute.getUrl(), URL_PARAM_FTP_USERNAME);
String ftpPassword = WebUtils.getUrlParameterReg(fileAttribute.getUrl(), URL_PARAM_FTP_PASSWORD);