如何在Java中为pdf文件设置Mimetype?

时间:2019-05-24 10:32:19

标签: java resttemplate

我需要用pdf上传功能调用第三方api。当我遇到邮递员的请求时,我看到mimeType是“ application / pdf”,但是当我从代码中调用相同的api时,mimeType会变成“ application / octet-stream”。

有人可以帮助我设置正确的mimeType吗?

public Boolean uploadApplicantDocument(UUID customerId, UUID applicantId) {
    String url = "https://hookb.in/OeEomZeKRjcyVylWxWZW";

    HttpHeaders headers = new HttpHeaders();
    headers.add("Authorization", API_TOKEN);
    List<DocumentsResponse> list = documentsClient.getDocuments(customerId);
    for(DocumentsResponse d : list){
        try {

            S3Object s3Object = s3FileManagementService.getS3Document((d.filePathKey);
            ByteArrayResource contentsAsResource = new ByteArrayResource(IOUtils.toByteArray(s3Object.getObjectContent())) {
                @Override
                public String getFilename()
                {
                    return filename;
                }
            };
            MultiValueMap<String, Object> request = new LinkedMultiValueMap<>();

            request.add("file", contentsAsResource);
            restTemplateExecutor.executeMultipartFormDataPostRequest(url, request, headers, Object.class);

        } catch (Exception e) {
            log.info("Failed to submit document for applicantId: " + applicantId);
            return false;
        }
    }
    return true;
}


public <T> T executeMultipartFormDataPostRequest(String url, MultiValueMap<String, Object> request, HttpHeaders httpHeaders, Class<T> responseClass){
    log.info("invoking the postMultipartFormDataRequest with url = " + url + " responseClass = " + responseClass);
    HttpEntity<MultiValueMap<String, Object>> requestEntity
            = new HttpEntity(request, httpHeaders);
    RestTemplate restTemplate = new RestTemplate();
    ResponseEntity<String> result = restTemplate
            .postForEntity(url, requestEntity, String.class);
    log.debug("Response from Server: " + result.getBody() + " with Status : " + result.getStatusCode().toString());
    return gson.fromJson(result.getBody(), responseClass);
}

0 个答案:

没有答案