使用Spring RestTemplate发布多部分(PDF)文件

时间:2018-02-19 19:13:49

标签: java spring

我尝试向现有端点发出POST请求以上传pdf文档。我尝试了以下代码和许多其他解决方案,但总是得到400 status code请帮助...

public String upload(String token, byte[] doc) throws Exception {

        String url = "http://localhost:8080/api/document";
        RestTemplate rest = new RestTemplate();
        MappingJackson2HttpMessageConverter jsonHttpMessageConverter = new MappingJackson2HttpMessageConverter();
        jsonHttpMessageConverter.getObjectMapper().configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false);
        rest.getMessageConverters().add(new MappingJackson2HttpMessageConverter());
        rest.getMessageConverters().add(new ByteArrayHttpMessageConverter());

        HttpHeaders header = new HttpHeaders();
        header.setContentType(MediaType.MULTIPART_FORM_DATA);
        header.add("X-Auth-Token", token);

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

        MultiValueMap<String, Object> multipartRequest = new LinkedMultiValueMap<>();

        HttpHeaders jsonHeader = new HttpHeaders();
        HttpEntity<String> jsonHttpEntity = new HttpEntity<>("doc", jsonHeader);

        HttpHeaders fileHeader = new HttpHeaders();
        fileHeader.setContentType(MediaType.APPLICATION_PDF);


        HttpEntity<byte[]> fileEntity = new HttpEntity<>(doc, fileHeader);

        multipartRequest.add("", bodyHeader);
        multipartRequest.add("reference", jsonHttpEntity);
        multipartRequest.add("file", fileEntity);
        System.out.println("body:    " + multipartRequest.toString());
        HttpEntity<MultiValueMap<String, Object>> requestEntity = new HttpEntity<>(multipartRequest, header);

        ResponseEntity<String> result = rest.exchange(url, HttpMethod.POST, requestEntity, String.class);

        return result.getBody();
    }

1 个答案:

答案 0 :(得分:1)

您可以尝试传递文件本身而不是字节数组byte[],即

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

import org.springframework.core.io.FileSystemResource;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.MediaType;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.web.client.RestTemplate;

public class Application {
    public static void main(String[] args) throws IOException {
        LinkedMultiValueMap<String, Object> map = new LinkedMultiValueMap<>();
        FileSystemResource value = new FileSystemResource(new File("D://test.pdf"));
        map.add("file", value);
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.MULTIPART_FORM_DATA);
        HttpEntity<LinkedMultiValueMap<String, Object>> requestEntity = new HttpEntity<>(map, headers);
        RestTemplate restTemplate = new RestTemplate();
        restTemplate.exchange("http://localhost:8080/api/document", HttpMethod.POST, requestEntity, String.class);
    }
}

Here是此代码段的参考