赞
踩
废话不多说,直接上代码
@PostMapping("/upload") public ResponseResult upload(@RequestParam MultipartFile file){ String path=System.getProperty("user.dir")+"/file/"; String newFileName= UUID.randomUUID().toString().replaceAll("-","")+file.getOriginalFilename(); File newFile=new File(path,newFileName); if (!newFile.getParentFile().exists()){ newFile.getParentFile().mkdirs(); } try { file.transferTo(newFile); } catch (IOException e) { e.printStackTrace(); return new ResponseResult(201,"上传失败"); } String filePath="/upload/"+newFileName; return new ResponseResult(200,"保存成功",filePath); }
@SpringBootApplication public class WebApplication { public static void main(String[] args) { SpringApplication.run(WebApplication.class,args); } @Bean public WebMvcConfigurer webMvcConfigurerUpload(){ String uploadPath=System.getProperty("user.dir")+"/file/"; return new WebMvcConfigurer() { @Override public void addResourceHandlers(ResourceHandlerRegistry registry) { registry.addResourceHandler("/upload/**") .addResourceLocations("file:"+uploadPath+"/"); } }; } }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。