【问题标题】:Adding a new bitstream to DSpace item using DSpace REST API使用 DSpace REST API 向 DSpace 项目添加新的比特流
【发布时间】: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>

【问题讨论】:

    标签: rest dspace


    【解决方案1】:

    经过多次尝试,我使用 Dspace rest API 成功发布了比特流文件:

    1- URL 应包含名称和可选描述“无空格”
    例如:http://domain-name.com:8080/rest/items/120/bitstreams?name=my_image.jpg&amp;description=my_description

    2- 标头应包含:
    - “rest-dspace-token”,其值为登录令牌。
    - “Content-Type”可以是“multipart/form-data”或“text/plain”

    3- 发布的内容应该是文件的二进制文件,没有任何名称或密钥,如纯文本。

    示例但使用 php curl:

    $file_name_with_full_path = realpath('./my_image.jpg');
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, 'http://domain-name.com:8080/rest/items/120/bitstreams?name=my_image.jpg&description=my_description');
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: text/plain', 'Accept: application/json', 'rest-dspace-token: ' . $dspaceToken));
    curl_setopt($ch, CURLOPT_POSTFIELDS, file_get_contents($file_name_with_full_path));
    $result = curl_exec($ch);
    curl_close($ch);
    echo $result;
    

    【讨论】:

    • 感谢您的回答,我也使用ProcessBuildercurl通过java成功地做到了
    【解决方案2】:

    使用 curl 的示例:

    1. 首先使用 DSpace 5 REST API 进行身份验证,如下所示:

    curl -X POST http://localhost:8080/rest/login -d '{"email":"me@email.com", "password":"s3cret"}' -v -H "Content-Type: application/json"

    1. 然后在上传比特流时将令牌包含在rest-dspace-token标头的响应正文中:

    curl -v -X POST -H "Content-Type: multipart/form-data" -H "rest-dspace-token: 5f042a2a-3a11-4833-b5bf-07c161272bdb" --data-binary @/path/to/test2.pdf "http://localhost:8080/rest/items/120/bitstreams?name=test2.pdf"

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-01-18
      • 1970-01-01
      • 1970-01-01
      • 2017-12-29
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多