【发布时间】:2020-01-24 02:57:25
【问题描述】:
我正在尝试使用 rest 调用将新的比特流文件添加到 DSpace(5.2 版)项目。我正在通过 java 程序进行其余的调用。我能够通过我的程序成功登录到 REST API。这是我的代码段:
HttpPost post = new HttpPost(dspace_rest_url+"login");
StringEntity input = new StringEntity("{\"email\":\""+dspace_email+"\",\"password\":\""+dspace_password+"\"}");
input.setContentType("application/json");
post.setEntity(input);
HttpResponse response = client.execute(post);
但是,我对如何使用 REST 调用发布比特流感到困惑。 DSpace REST Documentation 没有明确指定如何将比特流发布到 DSpace。我有一个要添加到项目的图像文件(我知道项目 ID)。根据文档:
POST /items/{item id}/bitstreams - 将比特流添加到项目。你必须发布一个比特流
如何以比特流的形式发布我的图像文件?例如,为了登录 REST API,需要 JSON 数组中的电子邮件和密码。 API 期望比特流采用哪种格式。
希望有人能提供帮助。
这是我尝试过的:
HttpClient client = new DefaultHttpClient();
HttpPost post = new HttpPost(dspace_rest_url+"items/"+itemID+"/bitstreams");
post.addHeader("rest-dspace-token", token);
File postFile = new File(thumbnailPath);
MultipartEntityBuilder builder = MultipartEntityBuilder.create();
builder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
FileBody cbFile = new FileBody(postFile, "image/jpeg");
builder.addPart("userfile", cbFile);
HttpEntity entity = builder.build();
post.setEntity(entity);
System.out.println("executing request " + post.getRequestLine());
HttpResponse response = client.execute(post);
DSpace REST 返回的响应:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<bitstream>
<expand>parent</expand>
<expand>policies</expand>
<expand>all</expand>
<id>461945</id>
<type>bitstream</type>
<bundleName>ORIGINAL</bundleName>
<checkSum checkSumAlgorithm="MD5">d281b5cbf5d2001e266ed3252a50fb2d</checkSum>
<format>Unknown</format>
<mimeType>application/octet-stream</mimeType>
<retrieveLink>/bitstreams/461945/retrieve</retrieveLink>
<sequenceId>-1</sequenceId>
<sizeBytes>5677</sizeBytes>
</bitstream>
【问题讨论】: