zoukankan      html  css  js  c++  java
  • SpringBoot的文件上传

    先在src/main/resources下新建一个static目录用以存放html页面,简单的html页面如下

     1 <!DOCTYPE html>
     2 <html>
     3 <head>
     4 <meta charset="UTF-8">
     5 <title>SpringBoot文件上传</title>
     6 </head>
     7 <body>
     8     <form action="FileUploadController" method="post" enctype="multipart/form-data">
     9         上传文件:
    10         <input type="file" name="filename" />
    11         <br />
    12         <input type="submit" />
    13     </form>
    14 </body>
    15 </html>

    注意一点时,form表单的提交方法为 method = "post"

    然后就是Controller类的编写,如下:

     1 package com.example.uploadfile.springbootupload.cont;
     2 
     3 import java.io.File;
     4 import java.util.HashMap;
     5 import java.util.Map;
     6 
     7 import org.springframework.web.bind.annotation.RequestMapping;
     8 import org.springframework.web.bind.annotation.RestController;
     9 import org.springframework.web.multipart.MultipartFile;
    10 
    11 /**
    12  * 
    13  * @author SpringBoot文件上传
    14  */
    15 @RestController // 这个类下所有的方法的返回值都会自动进行json格式的转换
    16 public class FileUploadController {
    17     /*
    18      * 处理文件上传
    19      */
    20 
    21     // 路由映射
    22     @RequestMapping("/FileUploadController")
    23     public Map<String, Object> fileUpload(MultipartFile filename) throws Exception { // 这里的MultipartFile参数的命名最好和html中的一样(也就是filename)免去转换操作
    24         System.out.print(filename.getOriginalFilename()); // 打印文件的原始名称
    25         // 将文件进行保存
    26         filename.transferTo(new File("e:/" + filename.getOriginalFilename()));
    27         Map<String, Object> map = new HashMap<>(); // 处理异常
    28         map.put("msg", "ok");
    29         return map;
    30 
    31     }
    32 
    33 }

    关于这个Controller类,上传文件成功后在控制台打印文件名,然后再将该文件保存至E盘

    接着就是启动类的编写,如下:

    package com.example.uploadfile.springbootupload;
    
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    
    /**
     *
     * @author  SpringBoot文件上传的启动类
     */
    @SpringBootApplication
    public class AppFile {
    
        public static void main(String[] args) {
            // TODO Auto-generated method stub
            SpringApplication.run(AppFile.class, args);
        }
    
    }

    需要注意一点的是,“Make sure that your main class is in a root package above other classes.”

    然后构建运行就可以执行上传文件的操作了;

    但是当你上传的文件大小超过10MB时,就会出现以下的错误。

    由控制台的输出可知,上传的文件超过了默认大小,所以要在application.properties中修改一下配置信息;

    一开始参照其他博客的修改是这样的

    单个文件最大的大小
    spring.http.multipart.maxFileSize=200MB
    上传的总文件的大小 spring.http.multipart.maxRequestSize
    =200MB

    但是还是出错,后来在spring官方指导上看到这个:

    就试着修改成以下的这样

    spring.servlet.multipart.maxFileSize=200MB
    spring.servlet.multipart.maxRequestSize=200MB

    再次运行时就阔以了!

    而且Spring官网上也提供了很详细的指导!

  • 相关阅读:
    梯度下降进阶
    梯度下降基础
    python---matplotlib
    python---numpy
    浅析Jupyter Notebook
    anaconda安装
    机器学习---导学
    python---线程与进程
    mapping values are not allowed in this context at line 115 column 10
    laravel进行单元测试的时候如何模拟数据库以及mockery的调用
  • 原文地址:https://www.cnblogs.com/Guhongying/p/10521205.html
Copyright © 2011-2022 走看看