简体   繁体   English

将 bytes[] 转换为 com.aspose.words.Document - Java API

[英]Convert bytes[] to com.aspose.words.Document - Java API

I'm trying to make an endpoint in a Java API.我正在尝试在 Java API 中创建一个端点。 The call will need to pass a word file (which will be in bytes) for the endpoint.该调用需要为端点传递一个字文件(以字节为单位)。 Then I need to translate these bytes into a com.aspose.words.Document so I apply .getMailMerge().getFieldNames() to it.然后我需要将这些字节转换为 com.aspose.words.Document,因此我将.getMailMerge().getFieldNames()应用于它。

Basically I want to make an endpoint that will take in a word file that is in bytes[], and returns the fields in that word document.基本上,我想创建一个端点,该端点将接收以字节 [] 为单位的 word 文件,并返回该 word 文档中的字段。 I'm stuck on the part where I make the bytes into a file.我被困在将字节转换为文件的部分。

Here's what I have so far:这是我到目前为止所拥有的:

@RequestMapping(value = "getFields", method = POST, produces = MediaType.APPLICATION_JSON_VALUE)
    public Fields getFieldsFromFile(@RequestBody byte[] file, @RequestHeader(value = "Authorization") String apiKey) {
        try {
            return myService.getFieldsFromFile(file, apiKey);
        } catch (Exception e) {
           ...handles error...
        }
    }
public Fields getFieldsFromFile(bytes[] file, String apiKey) throws ServiceException {

       {THIS IS WHERE I NEED TO MAKE THE FILE VAR, WHICH IS CURRENTLY BYTES,
                       INTO A DOCUMENT(COM.ASPOSE.WORDS.DOCUMENT)}

        try {
            return new Fields(new HashSet<>(Arrays.asList(file.getMailMerge().getFieldNames())).toArray(new String[0]));
        } catch (Exception e) {
            ...throws error...
        }
    }

If your bytes are already in the good format, as i see in the documentation here https://apireference.aspose.com/words/java/com.aspose.words/Document如果你的字节已经是好的格式,正如我在文档中看到的https://apireference.aspose.com/words/java/com.aspose.words/Document

there is a constructor Document(java.io.InputStreamstream)有一个构造函数Document(java.io.InputStreamstream)

So you can use this :所以你可以使用这个:

public Fields getFieldsFromFile(bytes[] file, String apiKey) throws ServiceException {
        Document doc = new Document(new ByteArrayInputStream(file));
        try {
            return new Fields(new HashSet<>(Arrays.asList(file.getMailMerge().getFieldNames())).toArray(new String[0]));
        } catch (Exception e) {
            ...throws error...
        }
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM