Required request part 'file' is not present

问题描述:

	@RequestMapping(value = "upload", method = RequestMethod.POST,consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    public void upload(@RequestPart MultipartFile upfile) throws Exception {
        return fileStorageService.upload(upfile);
    }

上面的代码是上传一个文件到upload方法中,然后在该方法中调用fileStorageService(FeignClient)去上传

fileStorageService.upload方法如下:

@FeignClient(name = "STORAGE")
@RequestMapping("/file")
public interface FileStorageService {
	@PostMapping(value = "/upload",consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    String upload(@RequestPart MultipartFile file);
}

在实际运行过程中,报错:

org.springframework.web.multipart.support.MissingServletRequestPartException: Required request part 'file' is not present

问题原因

因为upfile的属性是upfile,而不是file,传递到fileStorageService.upload方法中的时候,找不到file标记的文件内容

解决方案

方案一:让两个方法的参数名一致

方案二:处理upfile对象,让它生成一个新的MutipartFile对象

MultipartFile multipartFile = new MultipartFileDto(PlatformStorage.MinioMutipartFileFieldName.FIELD_NAME,  bytes);
//自定义一个MultipartFile
public class MultipartFileDto implements MultipartFile {
    private final String name;

    private String originalFilename;

    private String contentType;

    private final byte[] content;

    /**
     * Create a new MultipartFileDto with the given content.
     * @param name the name of the file
     * @param content the content of the file
     */
    public MultipartFileDto(String name, byte[] content) {
        this(name, "", null, content);
    }

    /**
     * Create a new MultipartFileDto with the given content.
     * @param name the name of the file
     * @param contentStream the content of the file as stream
     * @throws IOException if reading from the stream failed
     */
    public MultipartFileDto(String name, InputStream contentStream) throws IOException {
        this(name, "", null, FileCopyUtils.copyToByteArray(contentStream));
    }

    /**
     * Create a new MultipartFileDto with the given content.
     * @param name the name of the file
     * @param originalFilename the original filename (as on the client's machine)
     * @param contentType the content type (if known)
     * @param content the content of the file
     */
    public MultipartFileDto(String name, String originalFilename, String contentType, byte[] content) {
        this.name = name;
        this.originalFilename = (originalFilename != null ? originalFilename : "");
        this.contentType = contentType;
        this.content = (content != null ? content : new byte[0]);
    }

    /**
     * Create a new MultipartFileDto with the given content.
     * @param name the name of the file
     * @param originalFilename the original filename (as on the client's machine)
     * @param contentType the content type (if known)
     * @param contentStream the content of the file as stream
     * @throws IOException if reading from the stream failed
     */
    public MultipartFileDto(String name, String originalFilename, String contentType, InputStream contentStream)
            throws IOException {

        this(name, originalFilename, contentType, FileCopyUtils.copyToByteArray(contentStream));
    }

    @Override
    public String getName() {
        return this.name;
    }

    @Override
    public String getOriginalFilename() {
        return this.originalFilename;
    }

    @Override
    public String getContentType() {
        return this.contentType;
    }

    @Override
    public boolean isEmpty() {
        return (this.content.length == 0);
    }

    @Override
    public long getSize() {
        return this.content.length;
    }

    @Override
    public byte[] getBytes() throws IOException {
        return this.content;
    }

    @Override
    public InputStream getInputStream() throws IOException {
        return new ByteArrayInputStream(this.content);
    }

    @Override
    public void transferTo(File dest) throws IOException, IllegalStateException {
        FileCopyUtils.copy(this.content, dest);
    }

}

方案三:升级springboot和springcloud

我发现这个问题在2.1.7.RELEASE和Greenwich.SR2下会出现,但是升级到2.4.0和2020.0.4后就不会出现,直接这样使用没问题

posted @ 2022-08-24 10:43  镇魂帆-张  阅读(3490)  评论(0编辑  收藏  举报