“This is the first day of my participation in the First Challenge 2022. For details: First Challenge 2022”

I recently worked with a Vue project that is placed on top of Eclipse. The original project is placed in Eclipse. The SpringBoot project loads static files into the classpath directory by default when files are uploaded. At this time, the picture we uploaded was not passed into the started project, so it is clear that the path is correct, but it cannot be accessed. After the project is restarted, the project will be put into a new JAR package, and then the picture uploaded last time will be displayed normally.

The main process is: click the front-end Vue file upload button, select file upload in the computer, and replace the file with the selected file

Click Upload to upload the picture to the background Controller server through the file stream. The server saves the image to the specified location, I configure the drive path, and generates the specified file name. If the upload succeeds, the newly generated file name is returned.

After the image is uploaded successfully, the uploaded image will be displayed in the successful and loaded to the page according to the path

Solution: Configure static resource file path access in application.ymlz. Configure a virtual path.

virtuel:
  filePath: E:/jiaodaVideo/
Copy the code

Back-end controller upload file code:

@RequestMapping("/upload")
	public R upload(@RequestParam("file") MultipartFile file,String type) throws Exception {
		if (file.isEmpty()) {
			throw new EIException("Upload file cannot be empty");
		}
		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 EntityWrapper<ConfigEntity>().eq("name"."faceFile"));
			if(configEntity==null) {
				configEntity = new ConfigEntity();
				configEntity.setName("faceFile");
				configEntity.setValue(fileName);
			} else {
				configEntity.setValue(fileName);
			}
			configService.insertOrUpdate(configEntity);
		}
		return R.ok().put("file", fileName);
	}
Copy the code

File upload = new File(“D:/work/”); Here the path is recommended to configure in YML, and then read, because this is a simple graduation project, so directly write dead.

Configuration WebMvcConfigurationSupport rewrite addResourceHandlers can be realized.

/ * * * 2.0 configuration springboot WebMvcConfigurationSupport, leads to the default configuration is covered, static resource needs to be rewritten to allow access to * / addResourceHandlers method
	@Override
    public 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);
    }
Copy the code

Finally, the object data is uploaded and saved successfully

Problem solved. Let me just write this down.