上传图片路径bug修复

master
pan 5 years ago
parent 5481e49ec3
commit bcfe945b1f
  1. 13
      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();

Loading…
Cancel
Save