zoukankan      html  css  js  c++  java
  • Spring Boot 上传文件

    Spring Boot 上传文件也是使用 MultipartFile 类,和 Spring MVC 其实差不多,参考文章:https://www.cnblogs.com/jwen1994/p/11182923.html

    HTML

    <form enctype="multipart/form-data" method="post" action="/upload">
      文件:<input type="file" name="head_img"/>
      姓名:<input type="text" name="name"/>
      <input type="submit" value="上传"/>
    </form>

    Java 文件

    package com.example.demo.controller;
    
    import java.io.File;
    import java.io.IOException;
    import java.util.UUID;
    
    import javax.servlet.http.HttpServletRequest;
    
    import com.example.demo.domain.JsonData;
    
    import com.example.demo.domain.JsonData;
    import org.springframework.stereotype.Controller;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RequestParam;
    import org.springframework.web.bind.annotation.ResponseBody;
    import org.springframework.web.multipart.MultipartFile;
    
    /**
     * 功能描述:文件测试
     *
     * <p> 创建时间:Apr 22, 2018 11:22:29 PM </p> 
     * <p> 作者:小D课堂</p>
     */
    @Controller
    public class FileController {
            private static final String filePath = "F:/IdeaProjects/springbootDemo/src/main/resources/static/images/";
        
            @RequestMapping(value = "upload")
            @ResponseBody
            public JsonData upload(@RequestParam("head_img") MultipartFile file, HttpServletRequest request) {
              
                //file.isEmpty(); 判断图片是否为空
                //file.getSize(); 图片大小进行判断
                 
                String name = request.getParameter("name");
                System.out.println("用户名:"+name);
                
                 // 获取文件名
                String fileName = file.getOriginalFilename();            
                System.out.println("上传的文件名为:" + fileName);
                
                // 获取文件的后缀名,比如图片的jpeg,png
                String suffixName = fileName.substring(fileName.lastIndexOf("."));
                System.out.println("上传的后缀名为:" + suffixName);
                
                // 文件上传后的路径
                fileName = UUID.randomUUID() + suffixName;
                System.out.println("转换后的名称:"+fileName);
                
                File dest = new File(filePath + fileName);
               
                try {
                    file.transferTo(dest);
                    
                    return new JsonData(0, fileName);
                } catch (IllegalStateException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }
                return  new JsonData(-1, "fail to save ", null);
            }
    }

    文件大小配置,可以在启动类里面配置

    @Bean 
    public MultipartConfigElement multipartConfigElement() { 
      MultipartConfigFactory factory = new MultipartConfigFactory(); 
      //单个文件最大 
      factory.setMaxFileSize("10240KB"); //KB,MB 
      /// 设置总上传数据总大小 
      factory.setMaxRequestSize("1024000KB"); 
      return factory.createMultipartConfig(); 
    }
  • 相关阅读:
    Codeforces 1381B Unmerge(序列划分+背包)
    daily overview(2020.03.07update:该网站打不开惹
    矩阵相关
    颓式子
    51nod 1603 限高二叉排列树/1412 AVL树的种类
    模板合集(未完
    【luogu5651】 基础最短路练习题 [?]
    一个大Za
    【2019.11.11】
    【noip2017】
  • 原文地址:https://www.cnblogs.com/jwen1994/p/11184566.html
Copyright © 2011-2022 走看看