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