springmvc 文件上传

来源:互联网 发布:淘宝一颗钻在哪看 编辑:程序博客网 时间:2024/06/08 04:16
  @PostMapping(value = "/uploadFile")  public String uploadFile(@RequestParam(value = "md5") String md5,                           @RequestParam(value = "file") MultipartFile file) {    String fileurl = fileService.uploadFile(md5,file);    return fileurl;  }
  public String uploadFile(String md5, MultipartFile file) {    String fileName = file.getOriginalFilename();    String suffix = fileName.substring(fileName.length() - 3);    String filePath = "D:/upload";    if (!file.isEmpty()) {      if (!new File(filePath + "/" + suffix + "/").exists()) {        new File(filePath).mkdirs();      }      filePath = filePath + "/" + suffix + "/" + md5 + "." + suffix;      try {        FileUtils.copyInputStreamToFile(file.getInputStream(), new File(filePath));      } catch (IOException e) {        logger.error("文件上传失败", e);        throw new StoredProcedureException();      }    }    return filePath;  }

yml配置下

    http:        multipart:            enabled: true            max-file-size: 50MB            max-request-size: 50MB
原创粉丝点击