Postman 上传 multipartfile

Postman 上传 multipartfile

1.需求描述

通过postman上传一张png图片(其他文件也可),服务端保存到指定目录

简单定义前端入参

​ 文件使用 file 字段存储

​ 文件别称 name 存储

2.Postman端

  1. 切换到body
  2. 选择form-data
  3. 修改file类型为file
  4. 选择待上传文件
3.后端代码
  1. 后端model使用MultipartFile

    @Data
    @AllArgsConstructor
    @NoArgsConstructor
    class BaseFile implements Serializable {
        private String name;
        private MultipartFile file;
    }
    
  2. 后端controller (为了代码演示,这里直接在controller保存文件)

    @PostMapping("/upload")
    public void uploadFile(BaseFile baseFile) throws IOException {
        MultipartFile file = baseFile.getFile();
        String name = baseFile.getName();
    
        String originalFilename = file.getOriginalFilename();
        long size = file.getSize();
        byte[] bytes = file.getBytes();
        String contentType = file.getContentType();
        Resource resource = file.getResource();
    
        System.out.println(originalFilename);
        System.out.println(size);
        System.out.println(contentType);
    
        InputStream inputStream = file.getInputStream();
        FileOutputStream fileOutputStream = new FileOutputStream(UploadConfig.path + originalFilename);
        byte[] buffer = new byte[1024];
        int len;
        while (-1 != (len = inputStream.read(buffer))) {
            fileOutputStream.write(buffer, 0, len);
        }
        fileOutputStream.flush();
        fileOutputStream.close();
    }
    
  • 5
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值