postman json参数_用PostMan学习上传图片与json对象

22065a5657a696bd55cfd3c6c377edb1.png

一、上传时同时带上文件与其它参数

public JsonResult<void> submitBook(HttpServletRequest req,@RequestParam("picFile") MultipartFile file,Long bookBaseId,String isbn,String name,String author,String publishCompany){

这里的MultipartFile是spring mvc里的类

那么PostMan怎么配置请求参数?

6b5c037ce489f33e544985d760a9936e.png


header不用设置。二、只上传文件,没有参数名

controller取文件代码:

DataInputStream dataStream = new DataInputStream(request.getInputStream());
ByteArrayOutputStream outStream = new ByteArrayOutputStream();
byte[] buff = new byte[100]; //buff用于存放循环读取的临时数据
int rc = 0;
while ((rc = dataStream.read(buff, 0, 100)) > 0) {
    outStream.write(buff, 0, rc);
}
dataStream.close();
byte[] byteData = outStream.toByteArray();
outStream.flush();

这里用的是request.getInputStream来获取文件,来看看postman的配置:

add6629a577cd590d66a55bb47691d1b.png

这次要设置一下header,如图:

5c7d2fadf6ef5135c4b9f9e4ea7f1592.png


要注意的是Content-Length参数必须有,而且值要准确,否则request.getInputStream方法就会报错或获取不了正确的文件。

那这个值是怎么来的,可以new File(xx.jpg).length()获取的值填到这里。

从这些参数的配置,是不是能学到一些http协议的参数的用法?

三、复杂对象json参数的传递

2019-01-29补充。json数据作为参数,怎么传:

粘下spring mvc的代码:

@ApiOperation(value = "保存(编辑)一本书")
	@RequestMapping(value="/updateBookInfo",method = {RequestMethod.POST})
	public JsonResult<ugcbookvo> updateBookInfo(HttpServletRequest req,@RequestBody  UGCBookVo book){

看下postman的配置:

cd26dc0dc0b4bcd76d8f47a3314fe242.png

看下header的设置:

8a9d1127df19d51a1cf9b343af6509df.png

原文:用PostMan学习上传图片与json对象

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值