# 最基本的 HTML 文件表单上传模板

📆 2021-12-16 13:08

# HTML

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <title>Multipart Form Data File Upload</title>
  </head>
  <body>
    <form action="http://192.168.1.1:8080/file/upload" method="post" enctype="multipart/form-data">
      <input type="file" name="filename">
      <button type="submit" name="button">提交</button>
    </form>
  </body>
</html>

# JAVA

@RequestMapping("file/upload")
@PostMapping
public String upload(@RequestParam("filename") MultipartFile file){
  try {
    String suffix=file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf("."));
    file.transferTo(new File("/test"+suffix));

    return "success";
  } catch (Exception e) {
    log.error("上传失败!",e);
  }
  return "error";
}
最后更新于: 1/12/2022, 2:19:06 PM