Explorar el Código

简化下载文件 io 操作

kl hace 4 años
padre
commit
d95fbe02bd
Se han modificado 1 ficheros con 3 adiciones y 10 borrados
  1. 3 10
      server/src/main/java/cn/keking/utils/DownloadUtils.java

+ 3 - 10
server/src/main/java/cn/keking/utils/DownloadUtils.java

@@ -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);