postman上传文件

1、postman配置

1.1 报错配置

1.2 正确配置

Headers中什么都不用配置

1.3 file参数

2、java代码上传示例

pom.xml配置

<dependencies>
	<!-- spring boot -->
	<dependency>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-web</artifactId>
	</dependency>
	<dependency>
		<groupId>org.apache.httpcomponents</groupId>
		<artifactId>httpclient</artifactId>
	</dependency>
	<!-- https://mvnrepository.com/artifact/org.apache.httpcomponents/httpcore -->
	<dependency>
		<groupId>org.apache.httpcomponents</groupId>
		<artifactId>httpcore</artifactId>
	</dependency>
	<!-- https://mvnrepository.com/artifact/org.apache.httpcomponents/httpmime -->
	<dependency>
		<groupId>org.apache.httpcomponents</groupId>
		<artifactId>httpmime</artifactId>
	</dependency>
</dependencies>
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();
	}
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值