做旅游海报的软件或是网站,中淼建设工程有限公司网站,最新app推广,wordpress火车头插件最开始项目是放在eclipse之中的、springboot项目默认把静态的文件加载到classpath的目录下的。而此时我们上传的图片并没有传入启动了的项目当中去、所以明明路径是对的、却访问不了、在项目重新启动之后项目会打成新的jar包、这个时候上一次上传的图片才会正常显示。 解决方法… 最开始项目是放在eclipse之中的、springboot项目默认把静态的文件加载到classpath的目录下的。而此时我们上传的图片并没有传入启动了的项目当中去、所以明明路径是对的、却访问不了、在项目重新启动之后项目会打成新的jar包、这个时候上一次上传的图片才会正常显示。 解决方法配置静态资源路径访问。配置虚拟路径。
后端上传文件代码 RequestMapping(/upload)public R upload(RequestParam(file) MultipartFile file,String type) throws Exception {if (file.isEmpty()) {throw new EIException(上传文件不能为空);}String fileExt file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf(.)1);File upload new File(D:/work/);if(!upload.exists()) {upload.mkdirs();}String fileName new Date().getTime().fileExt;File dest new File(upload/fileName);file.transferTo(dest);if(StringUtils.isNotBlank(type) type.equals(1)) {ConfigEntity configEntity configService.selectOne(new EntityWrapperConfigEntity().eq(name, faceFile));if(configEntitynull) {configEntity new ConfigEntity();configEntity.setName(faceFile);configEntity.setValue(fileName);} else {configEntity.setValue(fileName);}configService.insertOrUpdate(configEntity);}return R.ok().put(file, fileName);} File upload new File(D:/work/);这里路径建议在yml里面配置、然后读取、因为我这是简单的毕业设计项目、所以就直接写死了。
配置WebMvcConfigurationSupport重写addResourceHandlers即可实现。
/*** springboot 2.0配置WebMvcConfigurationSupport之后会导致默认配置被覆盖要访问静态资源需要重写addResourceHandlers方法*/Overridepublic void addResourceHandlers(ResourceHandlerRegistry registry) {registry.addResourceHandler(/**).addResourceLocations(classpath:/resources/).addResourceLocations(classpath:/static/).addResourceLocations(classpath:/upload/).addResourceLocations(classpath:/admin/).addResourceLocations(classpath:/front/).addResourceLocations(classpath:/public/);registry.addResourceHandler(/upload/**).addResourceLocations(file:D:/work/);super.addResourceHandlers(registry);} 问题解决。简单记录一下。