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

将字节[]转换为com.aspose.words.Document-Java API,java,spring,binary-data,aspose,Java,Spring,Binary Data,Aspose,我试图在JavaAPI中创建一个端点。 调用需要为端点传递一个word文件(以字节为单位)。然后我需要将这些字节转换为com.aspose.words.Document,以便对其应用.getMailMerge().getFieldNames() 基本上,我希望创建一个端点,该端点将接收以字节[]为单位的word文件,并返回该word文档中的字段。 我被困在将字节放入文件的部分 以下是我目前掌握的情况: @RequestMapping(value = "getFields",

我试图在JavaAPI中创建一个端点。 调用需要为端点传递一个word文件(以字节为单位)。然后我需要将这些字节转换为com.aspose.words.Document,以便对其应用
.getMailMerge().getFieldNames()

基本上,我希望创建一个端点,该端点将接收以字节[]为单位的word文件,并返回该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)引发ServiceException{
{这是我需要创建文件VAR的地方,当前为字节,
放入文档(COM.ASPOSE.WORDS.DOCUMENT)}
试一试{
返回新字段(新HashSet(Arrays.asList(file.getMailMerge().getFieldNames())).toArray(新字符串[0]);
}捕获(例外e){
…抛出错误。。。
}
}

如果您的字节格式已经很好,正如我在这里的文档中看到的那样

有一个构造函数
文档(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...
        }
    }
public Fields getFieldsFromFile(bytes[]file,String apiKey)引发ServiceException{
单据单据=新单据(新ByteArrayInputStream(文件));
试一试{
返回新字段(新HashSet(Arrays.asList(file.getMailMerge().getFieldNames())).toArray(新字符串[0]);
}捕获(例外e){
…抛出错误。。。
}
}

是的,这就是我需要的。泰
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...
        }
    }