From 4ad7a7b785bc16bc6e47e7cd68f302adf6441d71 Mon Sep 17 00:00:00 2001 From: TinyAnts Date: Thu, 30 Mar 2023 11:35:52 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E5=BE=AE=E4=BF=A1=E5=85=AC?= =?UTF-8?q?=E4=BC=97=E5=8F=B7=E7=99=BB=E5=BD=95=E6=B2=A1=E6=9C=89=E6=98=B5?= =?UTF-8?q?=E7=A7=B0=E5=A4=B4=E5=83=8F=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/com/mdd/common/util/ToolUtils.java | 70 ++++++++++++++----- .../front/service/impl/LoginServiceImpl.java | 3 +- 2 files changed, 53 insertions(+), 20 deletions(-) diff --git a/server/like-common/src/main/java/com/mdd/common/util/ToolUtils.java b/server/like-common/src/main/java/com/mdd/common/util/ToolUtils.java index b01c4c21..3ab0423a 100644 --- a/server/like-common/src/main/java/com/mdd/common/util/ToolUtils.java +++ b/server/like-common/src/main/java/com/mdd/common/util/ToolUtils.java @@ -3,6 +3,7 @@ package com.mdd.common.util; import com.mdd.common.config.GlobalConfig; import java.io.*; +import java.net.HttpURLConnection; import java.net.URL; import java.net.URLConnection; import java.nio.charset.StandardCharsets; @@ -130,30 +131,61 @@ public class ToolUtils { * 下载文件 * * @author fzr - * @param urlString (文件网址) - * @param savePath (保存路径,如: /www/uploads) - * @param filename (保存名称,如: aa.png) + * @param urlStr (文件网址) + * @param savePath (保存路径,如: /www/uploads/aa.png) * @throws IOException IO异常 */ - public static void download(String urlString, String savePath, String filename) throws IOException { - URL url = new URL(urlString); - URLConnection con = url.openConnection(); - con.setConnectTimeout(20 * 1000); - File sf = new File(savePath); - if (!sf.exists()) { - if (sf.mkdirs()) { - throw new IOException("创建目录失败"); + public static void download(String urlStr, String savePath) throws IOException { + ByteArrayOutputStream bos = null; + FileOutputStream fos = null; + InputStream inputStream = null; + try { + URL url = new URL(urlStr); + HttpURLConnection conn = (HttpURLConnection) url.openConnection(); + conn.setConnectTimeout(5*1000); + conn.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 5.0; Windows NT; DigExt)"); + inputStream = conn.getInputStream(); + + // 获取数组数据 + byte[] buffer = new byte[4*1024]; + int len; + bos = new ByteArrayOutputStream(); + while((len = inputStream.read(buffer)) != -1) { + bos.write(buffer, 0, len); } - } - try (InputStream in = con.getInputStream(); - OutputStream out = new FileOutputStream(sf.getPath() + "\\" + filename)) { - byte[] buff = new byte[1024]; - int n; - while ((n = in.read(buff)) >= 0) { - out.write(buff, 0, n); + byte[] getData = bos.toByteArray(); + + // 新创建文件夹 + String fileName = StringUtils.substringAfterLast(savePath, "/"); + String path = savePath.replace("/"+fileName, ""); + File saveDir = new File(path); + if(!saveDir.exists()) { + if (!saveDir.mkdirs()) { + throw new IOException("创建存储文件夹失败"); + } } + // 保存文件数据 + File file = new File(savePath); + fos = new FileOutputStream(file); + fos.write(getData); } catch (Exception e) { - e.printStackTrace(); + throw new IOException(e.getMessage()); + } finally { + if (bos != null) { + try { + bos.close(); + } catch (IOException ignored) {} + } + if (fos != null) { + try { + fos.close(); + } catch (IOException ignored) {} + } + if (inputStream != null) { + try { + inputStream.close(); + } catch (IOException ignored) {} + } } } diff --git a/server/like-front/src/main/java/com/mdd/front/service/impl/LoginServiceImpl.java b/server/like-front/src/main/java/com/mdd/front/service/impl/LoginServiceImpl.java index 9f7d0dee..53bc7337 100644 --- a/server/like-front/src/main/java/com/mdd/front/service/impl/LoginServiceImpl.java +++ b/server/like-front/src/main/java/com/mdd/front/service/impl/LoginServiceImpl.java @@ -341,7 +341,8 @@ public class LoginServiceImpl implements ILoginService { String date = TimeUtils.millisecondToDate(time, "yyyyMMdd"); String name = ToolUtils.makeMd5(ToolUtils.makeUUID()+time) + ".jpg"; String path = "avatar" + date + "/" + name; - ToolUtils.download(avatar, YmlUtils.get("like.upload-directory"), path); + String savePath = YmlUtils.get("like.upload-directory") + path; + ToolUtils.download(avatar, savePath); defaultAvatar = path; } catch (IOException ignored) {} }