通过resteamplate将multipartfile的实现发布到webapi

rqenqsqc  于 2021-07-24  发布在  Java
关注(0)|答案(0)|浏览(137)

我有这样一个实体:

public class Chunk implements MultipartFile
{
        private String name;

        private String originalFileName;

        private String contentType;

        private final byte[] content;

        public Chunk(String name, 
                                        String originalFileName, 
                                        String contentType, 
                                        byte[] vidContent)
        {
           this.name = name;
           this.originalFileName = originalFileName;
           this.contentType = contentType;
           this.content = vidContent;
            }

        @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 content == null || content.length == 0;
        }

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

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

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

        @Override
        public void transferTo(File dest) throws IOException, IllegalStateException
        {
            FileOutputStream fos = new FileOutputStream(dest);
            fos.write(content);
            fos.close();
        }
    }

我必须从我的spring boot restapi将其发布到restapi。我试着通过:

private void sendChunk(Chunk chunk) {

        final String uri = "http://localhost:8080/backapifinal/v1/upload-multipartfile";

        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);

        MultiValueMap<String, Object> body = new LinkedMultiValueMap<>();
        body.add("file", chunk);

        HttpEntity<MultiValueMap<String, Object>> requestEntity = new HttpEntity<>(body, headers);
        ResponseEntity<String> response = restTemplate.postForEntity(uri, requestEntity, String.class);
        LOG.info("Response code: {}", response.getStatusCode());
}

我得到了:
org.springframework.web.client.restclientexception:没有用于org.springframework.util.linkedmultivaluemap和内容类型“application/octet stream”的httpmessageconverter
也尝试过:

LinkedMultiValueMap<String, Object> map = new LinkedMultiValueMap<>();
map.add("ByteArrayInputStream", chunk);
String response;
HttpStatus httpStatus = HttpStatus.CREATED;

    HttpHeaders headers = new HttpHeaders();
    headers.setContentType(MediaType.MULTIPART_FORM_DATA);

    HttpEntity<LinkedMultiValueMap<String, Object>> requestEntity = new HttpEntity<>(map, headers);
    response = restTemplate.postForObject(uri, requestEntity, String.class);
    LOG.info("Response code: {}", response);

明白了:
com.fasterxml.jackson.databind.exc.invaliddefinitionexception:找不到类java.io.bytearrayinputstream的序列化程序,也找不到创建beanserializer的属性(为了避免异常,在空的\u bean上禁用serializationfeature.fail(通过引用链:org.springframework.util.linkedmultivaluemap[“file”]->java.util.arraylist[0]->com.backapiflter.v1.model.chunk[“inputstream”])
我试着避免在空豆子上失败,但没有成功。
我不知道如何序列化那个实体来成功地发布它。该实体将包含一个字节[],该字节[]是通过分割一个大的多部分文件(这是一个video-mp4)获得的,我已经将一个视频分成字节[]部分,并且我必须分别发送这些部分,以便在客户机restapi中再次重建整个文件。

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题