方法1:加个配置类
@Configuration public class WebMvcConfig implements WebMvcConfigurer { /** * 创建时间: 2022/3/25 * 函数名称: addResourceHandlers * 函数功能: 解决图片上传后 必须 重启项目才能 显示图片的问题 * 函数参数: * * @param registry: * @return: void * @author: Snow ******************************************************* * 修改记录(时间--修改人--修改说明): */ @Override public void addResourceHandlers(ResourceHandlerRegistry registry) { // 告知系统static 当成 静态资源访问 String path = System.getProperty("user.dir") + "\\src\\main\\resources\\static\\images\\"; registry.addResourceHandler("/static/**").addResourceLocations("classpath:/static/"); registry.addResourceHandler("/images/**").addResourceLocations("file:"+path); } }
解决了我的问题。
下面是我同事提供的解决方法,我没使用,不知道行不行:
public BaseResult<String> getLogo(HttpServletResponse response) throws IOException { ServletOutputStream out = null; FileInputStream ips = null; try { //获取图片存放路径 String imgPath = ""; File file = ResourceUtils.getFile(basePath); File[] files = file.listFiles(); if(files.length>0){ for (int i = 0; i < files.length; i++) { if(files[i].getName().indexOf("logo")!=-1){ /*System.out.println(files[i].getCanonicalPath()); System.out.println(files[i].getAbsolutePath());*/ imgPath = files[i].getAbsolutePath(); } } } ips = new FileInputStream(new File(imgPath)); response.setContentType("multipart/form-data"); out = response.getOutputStream(); //读取文件流 int len = 0; byte[] buffer = new byte[1024 * 10]; while ((len = ips.read(buffer)) != -1){ out.write(buffer,0,len); } out.flush(); }catch (Exception e){ e.printStackTrace(); }finally { out.close(); ips.close(); } return null; }