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/upload | post | multipart/form-data | { “file”: fileobject, “businesstype”: “test” } |
postman 调用截图
-
请求头设置 content-type
-
请求体设置
-
接口断点调试
-
响应
发表评论