package com.jsdc.zhly.common.upload; import net.coobird.thumbnailator.Thumbnails; import org.apache.commons.io.FilenameUtils; import org.apache.commons.lang3.StringUtils; import org.springframework.stereotype.Service; import org.springframework.web.context.ServletContextAware; import org.springframework.web.multipart.MultipartFile; import javax.servlet.ServletContext; import java.io.File; import java.io.IOException; import java.util.ArrayList; import java.util.List; import java.util.Locale; import java.util.UUID; @Service public class FileRepository implements ServletContextAware { private ServletContext ctx; @Override public void setServletContext(ServletContext servletContext) { this.ctx = servletContext; } /** * 存储文件 * * @param path * @param ext * @param file * @return * @throws IOException */ public String storeByExt(String path, String ext, MultipartFile file) throws IOException { String filename = UploadUtils.generateFilename(path, ext); File dest = new File(getRealPath(filename)); store(file, dest); return filename; } /** * 存储文件 * * @param file 文件 * @param path 文件名 * @return * @throws IOException */ public void storePhotoByExt(MultipartFile file, String path) throws IOException { File dest = new File(getRealPath(path)); store(file, dest); //压缩图片 String origName1 = file.getOriginalFilename(); String fileF = FilenameUtils.getExtension(origName1).toLowerCase(Locale.ENGLISH);//获取文件后缀名称 String filePath= ctx.getRealPath("/"); //新图路径 String newFilePath = path.replace("."+fileF,"slt."+fileF); Thumbnails.of(filePath+path).scale(0.6f).toFile(filePath+newFilePath); } /** * 保存文件 * * @param file * @param dest * @throws IOException */ private void store(MultipartFile file, File dest) throws IOException { UploadUtils.checkDirAndCreate(dest.getParentFile()); file.transferTo(dest); } /** * 获取真实路径 * * @param name * @return */ public String getRealPath(String name) { String realpath = ctx.getRealPath(name); if (realpath == null) { realpath = ctx.getRealPath("/") + name; } return realpath; } public List<String> saveImgs(MultipartFile[] files) { List<String> imgs = new ArrayList<>(); if (files != null && files.length > 0) { for (MultipartFile file : files) { if ("".equals(file.getName()) || file.isEmpty()) { continue; } String origName1 = file.getOriginalFilename(); String fileF = FilenameUtils.getExtension(origName1).toLowerCase(Locale.ENGLISH);//获取文件后缀名称 String file_name = UUID.randomUUID().toString().replaceAll("-", ""); String file_path = "/images/portrait/" + file_name; try { storePhotoByExt(file, file_path+"."+fileF); //保存之后生成压缩之后的缩略图 /*String filePath= ctx.getRealPath("/"); Thumbnails.of(filePath+file_path+"."+fileF).scale(0.6f).toFile(filePath+file_path + "slt." + fileF);*/ } catch (IOException e) { e.printStackTrace(); } imgs.add(file_path+"."+fileF); } } return imgs; } }
其中getrealPath 获取的是tomcat的路径 如 getrealPath("/")获取到的是:
这个路径是本地IDEA用tomcat运行时,存放编译文件的路径,实际tomcat运行时,相当于ROOT,或者你指定的项目路径的根目录
对照本地真实的路径:
以上是编译后的文件存在的路径,zhly-web-0.0.1-SNAPSHOT相当于tomcat的ROOT,因为编译后的文件放在tomcat的ROOT可以直接运行,这个WEB-INF和META-INF和你getrealpah创建的upload,在放入tomcat时,应该是:
这样获取上传图片路径的方式,可以保证本地开发和tomcat实际部署时,图片的路径不会发生变化(都是tomcat的编译路径下,所以基本上不会找不到图片)