zoukankan      html  css  js  c++  java
  • springboot文件上传案例

    问题描述:

    文件从前端是怎么上传到后端的呢?这里我们可以们利⽤spring提供的MultipartFile类来接收和处理上传的⽂件,MultipartFile封装了⽂件上传的相关操作。

     

    解决方法:

    文件上传有两种情况,一种是单个文件和数据一起提交,一种是多个文件和数据一起提交。

    一、单个文件和数据一起提交

    1、在application.properties文件中配置文件上传大小限制

    # 单文件上传的最大值
    spring.servlet.multipart.maxFileSize=10MB
        
    # 多文件上传的最大值
    spring.servlet.multipart.maxRequestSize=10MB
        
    # 文件存储路径
    file.path=H:/Business/image/

    2、编写前端页面,需要注意的是,form标签在上传文件是必须要加enctype="multipart/form-data"

    <form method="POST" action="/upload" enctype="multipart/form-data">
        文件:<input type="file" name="file" /><br/><br/>
        描述:<input type="input" name="description"/><br/><br/>
        姓名:<input type="input" name="name"/><br/><br/>
        <input type="submit" value="提交" />
    </form>

    3、编写接收表单数据的VO实体

    @Data
    public class UploadReqVO {
        private String name;
        private String description;
        private MultipartFile file;
    }

    4、编写控制层

    import org.springframework.beans.factory.annotation.Value;
    import org.springframework.stereotype.Service;
    import org.springframework.ui.Model;
    import java.io.IOException;
    import java.nio.file.Files;
    import java.nio.file.Path;
    import java.nio.file.Paths;
    
    @Controller
    public class UploadController {
        @Value("${file.path}")
        private String FILE_PATh;
    
         @PostMapping("/upload")
        public void upload(UploadReqVO vo) {
            if (vo.getFile().isEmpty()) 
                throw new Exception("上传失败,请选择要上传的文件");
            try {
                byte[] bytes = vo.getFile().getBytes();
                String fileName = vo.getFile().getOriginalFilename();
                Path path = Paths.get(FILE_PATh+fileName);
                Files.write(path, bytes);
           } catch (IOException e) {
                e.printStackTrace();
                throw new Exception("上传失败");
           }
       }   
    }

    二、多个文件和数据一起提交

    1、在application.properties文件中配置文件上传大小限制

    # 单文件最大支持文件大小
    spring.servlet.multipart.maxFileSize=10MB
        
    # 文件总大小最大支持文件大小
    spring.servlet.multipart.maxRequestSize=10MB
        
    # 文件存储路径
    file.path=H:/Business/image/

    2、编写前端页面,需要注意的是,file类型的name必须相同

    <form method="POST" action="/uploads" enctype="multipart/form-data">
        文件一:<input type="file" name="files" /><br/><br/>
        文件二:<input type="file" name="files" /><br/><br/>
        描述:<input type="input" name="description"/><br/><br/>
        姓名:<input type="input" name="name"/><br/><br/>
        <input type="submit" value="提交" />
    </form>

    3、编写接收表单数据的VO实体

    @Data
    public class UploadReqVO {
        private String name;
        private String description;
        private MultipartFile[] files;
    }

    4、编写控制层

    import org.springframework.beans.factory.annotation.Value;
    import org.springframework.stereotype.Service;
    import org.springframework.ui.Model;
    import java.io.IOException;
    import java.nio.file.Files;
    import java.nio.file.Path;
    import java.nio.file.Paths;
    
    @Controller
    public class UploadController {
        @Value("${file.path}")
        private String FILE_PATh;
    
         @PostMapping("/uploads")
        public void uploads(UploadReqVO vo) {
            if (vo.getFiles().length==0) 
                throw new Exception("上传失败,请选择要上传的文件");
            for (MultipartFile file:vo.getFiles()){
            try {
                byte[] bytes = file.getBytes();
                String fileName = file.getOriginalFilename();
                Path path = Paths.get(FILE_PATh+fileName);
                Files.write(path, bytes);
           } catch (IOException e) {
                e.printStackTrace();
                throw new Exception("上传失败");
           }
       }   
    }
  • 相关阅读:
    获取 iPhone 上联系人姓名、电话、邮件的代码
    NSDate常用代码范例
    iphone开发之多线程NSThread和NSInvocationOperation
    iphone 定时提醒
    iphone 程序自动登陆
    搞定大厂算法面试之leetcode精讲11剪枝&回溯
    大厂算法面试之leetcode精讲7.双指针
    大厂算法面试之leetcode精讲8.滑动窗口
    大厂算法面试之leetcode精讲15.链表
    大厂算法面试之leetcode精讲10.递归&分治
  • 原文地址:https://www.cnblogs.com/XueTing/p/13693908.html
Copyright © 2011-2022 走看看