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();
}
}