使用Spring RestTemplate将文件上传到LogicalDOC

时间:2019-02-06 19:20:01

标签: java spring jax-rs resttemplate multipart

我正在尝试将文件上传到LogicalDOC DMS,但出现500错误。他们的日志中有一个例外(我认为他们正在使用apache jax rs

Caused by: java.io.IOException: Couldn't determine the boundary from the message!

我已经这样配置RestTemplate

@Bean
public ObjectMapper objectMapper() {
    ObjectMapper mapper = new ObjectMapper();
    mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
    mapper.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false);
    return mapper;
}

@Bean
public RestTemplate restTemplate() {
    RestTemplate restTemplate = new RestTemplate();

    MappingJackson2HttpMessageConverter mappingJackson2HttpMessageConverter = new MappingJackson2HttpMessageConverter();
    mappingJackson2HttpMessageConverter.setObjectMapper(objectMapper());
    mappingJackson2HttpMessageConverter.setSupportedMediaTypes(Collections.singletonList(MediaType.ALL));

    ResourceHttpMessageConverter resourceHttpMessageConverter = new ResourceHttpMessageConverter();

    ByteArrayHttpMessageConverter byteArrayHttpMessageConverter = new ByteArrayHttpMessageConverter();
    byteArrayHttpMessageConverter.setSupportedMediaTypes(Arrays.asList(
            MediaType.IMAGE_JPEG,
            MediaType.IMAGE_GIF,
            MediaType.IMAGE_PNG,
            MediaType.APPLICATION_OCTET_STREAM,
            MediaType.TEXT_XML,
            MediaType.TEXT_PLAIN,
            MediaType.TEXT_HTML
    ));

    FormHttpMessageConverter formHttpMessageConverter = new FormHttpMessageConverter();
    formHttpMessageConverter.setPartConverters(Arrays.asList(
            resourceHttpMessageConverter,
            byteArrayHttpMessageConverter
    ));

    restTemplate.setMessageConverters(Arrays.asList(
            mappingJackson2HttpMessageConverter,
            formHttpMessageConverter,
            resourceHttpMessageConverter,
            byteArrayHttpMessageConverter
    ));
    restTemplate.setErrorHandler(responseErrorHandler());

    return restTemplate;
}

并创建了service

public DocumentDto upload(Long folderId, String fileName, byte[] file, Long documentId) {
    MultiValueMap<String, Object> params = new LinkedMultiValueMap<>();

    params.add("folderId", folderId);
    params.add("filename", fileName);
    params.add("filedata", file);

    if (Objects.nonNull(documentId)) {
        params.add("docId", documentId);
    }

    return client.upload("/document/upload", params, DocumentDto.class);
}

休息client

public <T> T upload(String path, MultiValueMap<String, Object> body, Class<T> clazz) throws RestClientException {
    HttpHeaders headers = new HttpHeaders();
    headers.setContentType(MediaType.MULTIPART_FORM_DATA);
    headers.setAccept(Collections.singletonList(MediaType.APPLICATION_JSON));
    headers.setBasicAuth(username, password);

    HttpEntity<MultiValueMap<String, Object>> entity = new HttpEntity<>(body, headers);

    return restTemplate.exchange(getUrl(path), HttpMethod.POST, entity, clazz).getBody();
}

最后test ...服务呼叫

service.upload(FOLDER_ID, "application.yml", getDocument(), null);

private byte[] getDocument() {
    byte[] file = new byte[]{};
    Resource resource = resourceLoader.getResource("classpath:application.yml");
    try {
        file = StreamUtils.copyToByteArray(resource.getInputStream());
    } catch (IOException ex) {
        ex.printStackTrace();
    }
    return file;
}

那么你能帮我吗?我怀疑我没有发送附件的正确表示信息。是否有人在使用Spring RestTemplate发送文件时遇到类似的问题。我也尝试使用Resource类而不是byte[]类,但是遇到相同的错误。

0 个答案:

没有答案