首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何在使用RestTemplate进行post时添加/设置文件内容

如何在使用RestTemplate进行post时添加/设置文件内容
EN

Stack Overflow用户
提问于 2020-10-03 02:30:46
回答 2查看 414关注 0票数 1

我正在尝试使用以下方法将文件上传到DocLibrary文件夹:

代码语言:javascript
运行
复制
private static void postTheDocument() {
        final String restENDPoint = "http://servername:8080/alfresco/api/-default-/public/alfresco/versions/1/nodes/48eea6b2-fe9b-4cd2-8270-5caa35d7e8dc/children";
        RestTemplate restTemplate = new RestTemplate();
        HttpHeaders headers = new HttpHeaders();
        headers.setAccept(Arrays.asList(MediaType.APPLICATION_JSON));
        headers.setBasicAuth("admin", "admin");
        String requestJson = "{\"name\": \"TestName123s.doc\",\"nodeType\": \"hr:HR_Type\",\"properties\":{\"cm:title\":\"New Test title123\",\"hr:emp_no\":\"123456\",\"hr:lname\":\"Last_Name1\",\"hr:fname\":\"First_Name1\"}}";
        HttpEntity<String> entity = new HttpEntity<String>(requestJson, headers);
        HttpEntity<String> response = restTemplate.exchange(restENDPoint, HttpMethod.POST, entity,
                String.class);
        System.out.println(response.getBody().toString());
    }

文档建议这样做:

代码语言:javascript
运行
复制
curl -utest:test -X POST host:port/alfresco/api/-default-/public/alfresco/versions/1/nodes/12341234-2344-2344-43243242334/children -F filedata=@test.txt

使用方法postTheDocument()创建空文档。但是当我尝试下面的请求时:

代码语言:javascript
运行
复制
String requestJson = "{\"filedata\": \"@Z:/05test/YYYYest11qq890.pdf\",\"name\": \"TestName123s.doc\",\"nodeType\": \"hr:HR_Type\",\"properties\":{\"cm:title\":\"New Test title123\",\"hr:emp_no\":\"123456\",\"hr:lname\":\"Last_Name1\",\"hr:fname\":\"First_Name1\"}}";

我得到了以下异常:

代码语言:javascript
运行
复制
 Exception in thread "main" org.springframework.web.client.HttpClientErrorException$BadRequest: 400 : [{"error":{"errorKey":"Could not read content from HTTP request body: Unrecognized field \"filedata\" (class org.alfresco.rest.api.model.Node), not marked as ignorable (36 known properties: \"modifiedB... (1969 bytes)]

我在这里做错了什么,还是遗漏了什么?

EN

回答 2

Stack Overflow用户

发布于 2020-10-15 08:36:53

我认为常见的方法是在Alfresco中创建节点,然后将文件以分块上传的方式发送到节点"http://localhost:8080/alfresco/api/-default-/public/alfresco/versions/1/nodes/-root-/children“(有关分块上传的示例,请参阅:https://www.baeldung.com/httpclient-multipart-upload)。

要从创建的节点中获取节点id:

代码语言:javascript
运行
复制
    if (response != null) {
        try {
            JSONObject result;
            result = new JSONObject(response.getBody());
            System.out.println(result.toString());
              
            JSONObject oj = result.getJSONObject("entry");
            String id = (String) oj.get("id");

            System.out.println(id);
            fileupload(file_upload,id);

        } catch (JSONException e2) {
            e2.printStackTrace();
        }

    }        
票数 1
EN

Stack Overflow用户

发布于 2020-12-08 03:25:26

发布对我有效的解决方案。它不是基于RestTemplate的,但它可以工作。这是我从Alfresco支持人员那里得到的解决方案,它工作得很好(请参阅内联评论):

代码语言:javascript
运行
复制
private static void postFileAndMetadataToAlfresco() throws IOException, AuthenticationException {

        CloseableHttpClient httpClient = HttpClients.createDefault();
        HttpPost httpPost = new HttpPost("http://someservername:8080/alfresco/api/-default-/public/alfresco/versions/1/nodes/12341234-1234-1234-1234-123412341234/children");
        UsernamePasswordCredentials creds = new UsernamePasswordCredentials ("admin","adminpswd");
        httpPost.addHeader (new BasicScheme().authenticate(creds,httpPost, null));

        File payload = new File ("/path/to/my/file.pdf");

        MultipartEntityBuilder builder = MultipartEntityBuilder.create(); // Entity builder

        builder.addPart("filedata", new FileBody(payload)); // this is where I was struggling
        builder.addTextBody ("name", "thenamegoeshere");
        builder.addTextBody ("foo", "foo");
        builder.addTextBody ("bar", "bar");
        builder.addTextBody ("description", "descriptiongoeshere");

        builder.addTextBody ("overwrite", "true");

        HttpEntity entity = builder.build();

        httpPost.setHeader("Accept","application/json");
        httpPost.setEntity(entity);

        CloseableHttpResponse response = httpClient.execute(httpPost); // Post the request and get response

        System.out.println(response.toString()); // Response print to console

        httpClient.close();  // close the client
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/64176613

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档