JAVA实现上传附件到服务器

Source

1 问题

java 实现 前端上传一个附件,保存到服务器一个路径下

2 实现


   @ApiOperation(value = "上传附件", httpMethod = "POST")
    @PostMapping("/upload")
    public Result upload(@RequestParam(value = "file") MultipartFile multipartFile) {
    
      
        return ResultUtil.success(attachmentsService.upload(multipartFile));
    }



  @Override
    public String upload(MultipartFile multipartFile) {
    
      
        if (multipartFile != null) {
    
      
            String originalFilename = multipartFile.getOriginalFilename();
            File file = new File(attachmentPath + File.separator + originalFilename);
            try {
    
      
                if (file.exists()) file.delete();
                multipartFile.transferTo(file);
            } catch (IOException e) {
    
      
                return "";
            }
            return file.getAbsolutePath();
        }
        return "";
    }