REST API:InputStream复制(dovetailing)失败的大文件

时间:2016-11-03 05:12:16

标签: java rest inputstream

使用Jersey 1.8,我正在开发一个REST API来上传文件。反过来,我使用服务提供商REST API将文件上传到他们的网站。流程如下:

浏览器 - >我的REST使用Jersey服务器端;然后使用Apache HTTP Client进行后续REST调用 - >服务提供商的REST。

当我上传小文件(<1MB)时,一切正常。但是当文件大小增加时,它会出现以下错误。

A JSONObject text must begin with '{' at character 0

我怀疑错误是由于流在中途被“切断”。以下代码段有什么问题(我删除了不相关的代码)。

@POST
@Path("/upload")
@Consumes(MediaType.MULTIPART_FORM_DATA)
@Produces(MediaType.APPLICATION_JSON)
public Response uploadDocument(@FormDataParam("document") InputStream inputStream) {
        FilenetAdapter filenet = new FilenetAdapter();
        String documentId = filenet.uploadDocument(inputStream);
        // return the Jersey call - code removed for bravity
}

//FilenetAdapert.uploadDocument function
public String uploadDocument(InputStream inputStream) {

    HttpPost request = new HttpPost(serviceUrl);
    request.setEntity(buildMultipartEntity(metadata, inputStream));

    CloseableHttpClient client = HttpClientBuilder.create().build();
    CloseableHttpResponse response = client.execute(request);
    //consume the response - code removed for bravity
}

private HttpEntity buildMultipartEntity(JSONObject metadata, InputStream inputStream)
        throws IOException, URISyntaxException, JSONException {

    // Build a multipart HTTP request according to RFC1341
    MultipartEntityBuilder builder = MultipartEntityBuilder.create();

    // Set request metadata
    StringBody stringBody = new StringBody(metadata.toString(), ContentType.TEXT_PLAIN);
    FormBodyPartBuilder bodyPartBuilder = FormBodyPartBuilder.create();
    bodyPartBuilder.setName("metadata");
    bodyPartBuilder.setBody(stringBody);
    FormBodyPart mdata = bodyPartBuilder.build();
    builder.addPart(mdata);

    // Set request content
    ContentBody cbFile = new InputStreamBody(inputStream, ContentType.APPLICATION_OCTET_STREAM);
    builder.addPart("file", cbFile);
    return builder.build();
}

1 个答案:

答案 0 :(得分:0)

服务提供商更改了服务器配置。这解决了这个问题。