springboot单文件上传,使用postman测试文件上传接口

1、创建测试类(FileController.java)

package com.jeff.controller;

import java.io.File;
import java.io.IOException;

import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

@RestController
public class FileController {

	@PostMapping("/upload")
	public String upload(MultipartFile file) {
		if (file == null) {
			return "上传失败,未选择文件";
		}
		String fileName = file.getOriginalFilename();
		System.out.println("文件名:" + fileName);
		String filePath = "F:\\Jeff\\project\\workspace\\mavenDemo\\src\\main\\resources\\static\\";
		File dest = new File(filePath + fileName);
		try {
			file.transferTo(dest);
			System.out.println("上传成功!");
		} catch (IOException e) {
			System.out.println("上传异常!" + e);
			return "error";
		}
		return "success";
	}

}

2、使用postman测试接口(未选择文件)
springboot单文件上传,使用postman测试文件上传接口_第1张图片
3、使用postman测试接口(选择文件)
springboot单文件上传,使用postman测试文件上传接口_第2张图片
springboot单文件上传,使用postman测试文件上传接口_第3张图片
4、查看项目路径
springboot单文件上传,使用postman测试文件上传接口_第4张图片
5、如果报下图错误,请查看 解决方法
在这里插入图片描述

你可能感兴趣的:(SpringBoot,Postman)