Postman sends a request to upload files

Postman sends a request to upload files

Tips before viewing:

The Eclipse version used in this article is Photon Release (4.8.0), the JDK version is 1.6.0_45, the Tomcat version is 7.0.92, and the Postman version is v7.26.0.

When I wrote the interface recently, there was a demand for the parameters received by the interface to be a file stream. The interface was written, but I didn't know how to test it. At this time, the powerful postman came into play.

1.Postman settings

First, I am a POST request, so configure POST. After filling in the url, select Body, select form-data, select file at the end of Key, select the uploaded file in Value, and click Send to send the request.
Insert picture description here
Insert picture description here

2. Background test code

Test 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;
	}
}

Test upload file 1.properties

id=1
name=zhangsan

The test results are as follows

Insert picture description here

Insert picture description here

Guess you like

Origin blog.csdn.net/weixin_43611145/article/details/106815703