2

我是 OPENTEXT Rest API 的新手,虽然我能够使用它来验证/创建文件夹,但我无法让文档上传工作。以下是我一直在使用的代码:

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStreamReader;
import java.net.URISyntaxException;

import org.apache.http.HttpHeaders;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;

import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.utils.URIBuilder;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.mime.HttpMultipartMode;
import org.apache.http.entity.mime.MultipartEntityBuilder;
import org.apache.http.entity.mime.content.FileBody;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;


URIBuilder builder = new URIBuilder(https://bla.com/<restapiroot/v2/nodes");

builder.setParameter("type",                 "144")
       .setParameter("parent_id",            "123456")
       .setParameter("name",                 "bla.pdf")
       .setParameter("file",                 "C:\\My_Data\\bla.pdf");

MultipartEntityBuilder multiPartBuilder = MultipartEntityBuilder.create();

multiPartBuilder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
multiPartBuilder.addBinaryBody("ufile", new File (fullFileName), ContentType.DEFAULT_BINARY, fileName);
multiPartBuilder.setBoundary("aall12dk@@Joey");

HttpPost httpPostRequest = new HttpPost(builder.build());

httpPostRequest.addHeader( "<auth code name>", "value" );
httpPostRequest.addHeader( HttpHeaders.CONTENT_TYPE, "multipart/form-data; boundary=aall12dk@@Joey" );
httpPostRequest.addHeader( "Content-Disposition", "attachment;filename=" + "bla.pdf" );

httpPostRequest.setEntity(multiPartBuilder.build());

HttpResponse response = = httpClient.execute(httpPostRequest);

我收到以下错误:

00:47:47.694 [main] DEBUG org.apache.http.wire - http-outgoing-0 << "{"error":"Could not process object, invalid action \u0027create\u0027"}"
00:47:47.695 [main] DEBUG org.apache.http.headers - http-outgoing-0 << HTTP/1.1 500 Internal Server Error

我不确定我是否错误地调用了 API 和/或我是否完全错误地编写了文件上传逻辑。任何帮助将不胜感激。

谢谢

4

1 回答 1

2

我得到了解决方案。基本上,答案是只使用一个通用的 URIBuilder。其他一切都交给多部分构建器:

URIBuilder builder = new URIBuilder("https://bla.com/<restapiroot>/v2/nodes");

MultipartEntityBuilder multiPartBuilder = MultipartEntityBuilder.create();

multiPartBuilder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
multiPartBuilder.setBoundary("aall12dk@@RandomBoundary2019");           //Random value basically.

multiPartBuilder.addPart("type",                 new StringBody(String.valueOf(LAPI_DOCUMENTS.DOCUMENTSUBTYPE), ContentType.MULTIPART_FORM_DATA));
multiPartBuilder.addPart("parent_id",            new StringBody(String.valueOf(parentId),                       ContentType.MULTIPART_FORM_DATA));
multiPartBuilder.addPart("name",                 new StringBody(fileName,                                       ContentType.MULTIPART_FORM_DATA));
multiPartBuilder.addPart("file",                 new FileBody(new File (fullFileName),                          ContentType.DEFAULT_BINARY));
multiPartBuilder.addPart("description",          new StringBody(comments,                                       ContentType.MULTIPART_FORM_DATA));
multiPartBuilder.addPart("external_create_date", new StringBody("2017-12-10",                                   ContentType.MULTIPART_FORM_DATA)); 
multiPartBuilder.addPart("external_modify_date", new StringBody(LocalDate.now().toString(),                     ContentType.MULTIPART_FORM_DATA)); 
multiPartBuilder.addPart("external_source",      new StringBody("ftp",                                          ContentType.MULTIPART_FORM_DATA));

HttpPost httpPostRequest = new HttpPost(builder.build());
httpPostRequest.addHeader("<TicketHeaderName>", "<ticket value>");

httpPostRequest.setEntity(multiPartBuilder.build());

HttpResponse response = httpClient.execute(httpPostRequest);      //need to assign response to variable or you'll end up with hanging connections.

if ( response.getStatusLine().getStatusCode() != 200 ) {

    //Error handling logic
}
于 2019-01-02T17:11:46.823 回答