pom文件上传头像依赖
<!-- thymeleaf渲染模版 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency>controller层上传代码
@RequestMapping(value = "add",method = RequestMethod.POST) public String add(HttpServletRequest request, Stock stock, MultipartFile source) throws Exception { System.out.println(stock); System.out.println(source); //获取上传文件名称 String filename = source.getOriginalFilename(); //获取新的文件名 long millis = System.currentTimeMillis(); String newName = millis+filename; //获取文件流 InputStream inputStream1 = null; try { inputStream1 = source.getInputStream(); } catch (IOException e) { e.printStackTrace(); } //获取物理路径 String wlPath = "D:\\Program Files\\JetBrains\\workspace\\stock\\src\\main\\resources\\static\\img/"+newName; //逻辑路径 String ljPath = "../img/"+newName; //临时路径 String pathRoot = request.getSession().getServletContext().getRealPath("/");//自动寻找target根目录 String lsPath = pathRoot+"\\img\\"+newName; //判断是否需要创建文件夹 //物理路径文件夹 File wlFile = new File("D:\\Program Files\\JetBrains\\workspace\\stock\\src\\main\\resources\\