当前位置: 代码网 > it编程>编程语言>Java > Postman 调用 Spring Boot 文件上传接口

Postman 调用 Spring Boot 文件上传接口

2024年08月01日 Java 我要评论
Postman 调用 Spring Boot 封装的文件上传接口。

postman 调用文件上传接口

postman 简介

postman 是一个用于构建和使用 api 的 api 平台。
postman 简化了 api 生命周期的每一步,并优化了协作,因此您可以更快地创建更好的api。

简单点说:postman 发送请求给服务器,然后从服务器接受响应,最后在postman中展示出来。

spring boot 定义文件上传的接口

package com.joe.file.controller;

import org.springframework.util.stringutils;
import org.springframework.web.bind.annotation.postmapping;
import org.springframework.web.bind.annotation.requestmapping;
import org.springframework.web.bind.annotation.restcontroller;
import org.springframework.web.multipart.multipartfile;
import org.springframework.web.multipart.multiparthttpservletrequest;

import javax.servlet.http.httpservletrequest;

/**
 * @author: 高建伟-joe
 * @date: 2022-12-05
 * @description: 文件上传 控制器
 */
@requestmapping("/file")
@restcontroller
public class filecontroller {

    @postmapping("/upload")
    public string upload(httpservletrequest request){
        multiparthttpservletrequest multipartrequest = (multiparthttpservletrequest) request;
        multipartfile file = multipartrequest.getfile("file");
        string businesscode = request.getparameter("businesscode");
        if (null == file){
            return "获取文件失败";
        }
        if (!stringutils.haslength(businesscode)){
            return "业务类型为空";
        }

        // todo 保存文件

        // todo 保存文件上传记录

        return "文件上传成功";
    }
}

postman 调用文件上传接口

请求参数

地址请求方式content-type参数
http://localhost:8080/file/uploadpostmultipart/form-data{ “file”: fileobject, “businesstype”: “test” }

postman 调用截图

  1. 请求头设置 content-type
    fileupload_postman_1

  2. 请求体设置
    fileupload_postman_2

  3. 接口断点调试
    fileupload_postman_4

  4. 响应
    fileupload_postman_3

文件上传接口源码

点击下载

参考文献

postman 官方文档
postman 中文文档

(0)

相关文章:

版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。

发表评论

验证码:
Copyright © 2017-2025  代码网 保留所有权利. 粤ICP备2024248653号
站长QQ:2386932994 | 联系邮箱:2386932994@qq.com