From bcfe945b1f3b7f94857ce9798af56ac5d2ead6ec Mon Sep 17 00:00:00 2001 From: pan <1029559041@qq.com> Date: Sat, 30 May 2020 21:57:41 +0800 Subject: [PATCH] =?UTF-8?q?=E4=B8=8A=E4=BC=A0=E5=9B=BE=E7=89=87=E8=B7=AF?= =?UTF-8?q?=E5=BE=84bug=E4=BF=AE=E5=A4=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/com/share/help/service/ImageService.java | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/src/main/java/com/share/help/service/ImageService.java b/src/main/java/com/share/help/service/ImageService.java index 6dbf16e..a704bd6 100644 --- a/src/main/java/com/share/help/service/ImageService.java +++ b/src/main/java/com/share/help/service/ImageService.java @@ -4,6 +4,7 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Service; +import org.springframework.util.ResourceUtils; import org.springframework.web.multipart.MultipartFile; import java.io.File; @@ -44,10 +45,16 @@ public class ImageService { return null; } - File loadFile = new File(getClass().getResource("/").getFile() + imgPath.replace("classpath:/", ""), fileName); + File imgDir; + File imgFile; try { - file.transferTo(loadFile); - logger.debug("图片保存到"+loadFile.getAbsolutePath()); + imgDir = new File(ResourceUtils.getURL("classpath:").getPath() + imgPath.replace("classpath:/", "")); + if(!imgDir.exists()&&imgDir.mkdirs()){ + logger.info("成功初始化上传图片目录:"+imgDir.getAbsolutePath()); + } + imgFile=new File(imgDir,fileName); + logger.debug("图片保存到"+imgFile.getAbsolutePath()); + file.transferTo(imgFile); return fileName; } catch (IOException e) { e.printStackTrace();