• 5.0 SpringBoot普通上传功能 > 我的程序猿之路:第四十章


    SpringBoot 2.0

    1.upLoad.html

     1 <!DOCTYPE html>
     2 <html lang="en" xmlns:th="http://www.thymeleaf.org">
     3 <head>
     4     <meta charset="UTF-8">
     5     <title>Upload Page</title>
     6 </head>
     7 
     8 <body>
     9 <form action="/index.do/upload" method="post" enctype="multipart/form-data">
    10     <table>
    11         <tr>
    12             <td><input type="file" id="fileNameEmg" name="fileNameEmg"></td><h2 th:text="${emg}"/>
    13             <td th:if="${emg=='上传成功!'}"><img th:src="@{'/img/'+${image}}" alt="picture" width="100px" height="100px" /></td>
    14         </tr>
    15         <tr>
    16             <td><input type="button" value="submit" onclick="aa(this)"></td>
    17         </tr>
    18     </table>
    19 
    20 </form>
    21 <script>
    22 
    23     function aa(obj){
    24         var fiel = document.getElementById("fileNameEmg");
    25         var a = fiel.value;
    26         if(a.indexOf(".png")!=-1||a.indexOf(".jpg")!=-1){
    27 
    28             obj.form.submit()
    29         }else{
    30             alert("请选择图片文件")
    31         }
    32     }
    33     </script>
    34 </body>
    35 </html>

    2.uploadController.java

     1 @Controller
     2 @RequestMapping("index.do")
     3 public class userController {
     4 
     5 @RequestMapping("/uploadEng")
     6 public String up(@RequestParam("fileNameEmg") MultipartFile file,Model model) throws Exception{
     7 
     8        if (file.isEmpty()){
     9            System.out.println("--=========");
    10            model.addAttribute("emg","请选择要上传的文件!");
    11 
    12         }
    13         if(!file.isEmpty()){
    14            15            
    16             System.out.println("--");
    17            byte[] bytes = file.getBytes();
    18            Path path = Paths.get("C://tool//Project//demo3//src//main//resources//static//img//" + file.getOriginalFilename());
    19          Files.write(path, bytes);
    20          String str = "上传成功!";
    21           model.addAttribute("emg",str);
    22           model.addAttribute("image",file.getOriginalFilename());
    23           System.out.println(path);
    24       }
    25        return "uploadPage";
    26     }
    27 }

     3.application.properties

    #spring.servlet.multipart.enabled=false 
    spring.servlet.multipart.enabled=true

    注意:Spring Boot默认实例化了一个MultipartResolver(StandardServletMultipartResolver);

              application.properties中的“spring.servlet.multipart.enabled”默认(不写)为true。设置false在这里会报错

     

  • 相关阅读:
    jquery ajax 向后台传递数组
    定时任务
    C# 好用的插件
    C# 跳出循环
    存储区更新、插入或删除语句影响到了意外的行数(0)。实体在加载后可能被修改或删除。刷新 ObjectS
    Opencv2.4.9源码分析——HoughLinesP
    OpenCV+C++ 视频图片相互转换
    opencv2.2版本不稳定
    OpenCV 透视变换【图像归一化矫正】
    opencv配置注意事项【总结】
  • 原文地址:https://www.cnblogs.com/fanyuyi-boke/p/qiao_duo_shao_nian_dai_ma_neng_ba_shou_zhi_mo_ping40.html
Copyright © 2020-2023  润新知