Postman 发送请求上传文件

Postman 发送请求上传文件

观前提示:

本文所使用的Eclipse版本为Photon Release (4.8.0),JDK版本为1.6.0_45,Tomcat版本为7.0.92,Postman版本为v7.26.0。

在最近写接口的时候,有需求要接口接收的参数为文件流,接口写完了,但是却不知道如何测试,这个时候,强大的postman便发挥了作用。

1.Postman设置

首先,我是POST请求,所以配置POST,填写好url后,选择Body,选择form-data,在Key最后的地方选择file,在Value选择上传的文件,点击Send发送请求。
在这里插入图片描述
在这里插入图片描述

2.后台测试代码

测试Controller TestController.java

package cn.com.infosec.ra.system.web;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

/**
 * @ClassName TestController
 * @Description 测试Controller
 * @author jjy
 * @date 2020年6月17日 下午6:18:56
 */
@Controller
@RequestMapping("test")
public class TestController {
    
    
	
	/**
	 * @Title: upload
	 * @Description 测试获取上传文件
	 * @param request
	 * @return
	 * @throws IOException
	 * @Create 2020年6月17日 下午6:29:02 by jjy
	 */
	@RequestMapping("upload")
	@ResponseBody
	public Map<String, String> upload(MultipartHttpServletRequest request) throws IOException {
    
    
		
		Map<String, String> result = new HashMap<String, String>();
		
		// 获取文件
		MultipartFile file = request.getFile("file");
		
		Properties p = new Properties();
		p.load(file.getInputStream());
		
		System.out.println("id : " + p.get("id"));
		System.out.println("name : " + p.get("name"));
		
		result.put("msg", "success");
		return result;
	}
}

测试上传文件 1.properties

id=1
name=zhangsan

测试结果如下图

在这里插入图片描述

在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/weixin_43611145/article/details/106815703