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

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

我正在尝试在 Java API 中创建一个端点。 该调用需要为端点传递一个字文件(以字节为单位)。然后我需要将这些字节转换成 com.aspose.words.Document,所以我将 .getMailMerge().getFieldNames() 应用于它。

基本上我想创建一个端点,它将接收一个以字节为单位的 word 文件 [],returns 该 word 文档中的字段。 我卡在了将字节放入文件的部分。

这是我目前的情况:

@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...
        }
    }

如果您的字节格式已经正确,正如我在此处的文档中看到的那样 https://apireference.aspose.com/words/java/com.aspose.words/Document

有构造函数Document(java.io.InputStreamstream)

所以你可以使用这个:

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...
        }
    }