postman上传文件

1、postman配置

1.1 报错配置

postman上传文件_第1张图片

1.2 正确配置

postman上传文件_第2张图片

Headers中什么都不用配置

1.3 file参数

postman上传文件_第3张图片

2、java代码上传示例

pom.xml配置


	
	
		org.springframework.boot
		spring-boot-starter-web
	
	
		org.apache.httpcomponents
		httpclient
	
	
	
		org.apache.httpcomponents
		httpcore
	
	
	
		org.apache.httpcomponents
		httpmime
	
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import com.spring.pro.model.User;

/**
 * @ClassName: UploadController
 * @Description:
 * @author weiyb
 * @date 2018年4月25日 下午6:03:00
 */
@RestController
@RequestMapping("/upload")
public class UploadController {
	private Logger logger = LoggerFactory.getLogger(getClass());

	private String filePath = "D:/1/";

	@RequestMapping("/upLoadFile")
	public User uploadFiles(@RequestParam("file") MultipartFile[] files,@RequestParam("em") String em) throws IOException {
		logger.info("em:{}",em);
		for (int i = 0; i < files.length; i++) {
			try {
				uploadFile(files[i].getBytes(), files[i].getOriginalFilename());
			} catch (Exception e) {
				e.printStackTrace();
				logger.info("上传文件发生错误");
			}
		}
		return new User("1", "张三", 12);
	}

	private void uploadFile(byte[] file, String fileName) throws Exception {
		File targetFile = new File(filePath);
		if (!targetFile.exists()) {
			targetFile.mkdirs();
		}
		FileOutputStream out = new FileOutputStream(filePath + fileName);
		out.write(file);
		out.flush();
		out.close();
	}
}

 

你可能感兴趣的:(工具)