英文:
Convert bytes[] to com.aspose.words.Document - Java API
问题
我正在尝试在Java API中创建一个端点。
调用将需要将字节形式的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) {
...处理错误...
}
}
public Fields getFieldsFromFile(byte[] file, String apiKey) throws ServiceException {
{这是我需要将目前为字节数组的"file"变量转换为"com.aspose.words.Document"的部分}
try {
return new Fields(new HashSet<>(Arrays.asList(file.getMailMerge().getFieldNames())).toArray(new String[0]));
} catch (Exception e) {
...抛出错误...
}
}
英文:
I'm trying to make an endpoint in a 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.
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.
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...
}
}
答案1
得分: 1
如果您的字节已经处于正确的格式,就像我在这里的文档中看到的那样:https://apireference.aspose.com/words/java/com.aspose.words/Document
那么您可以使用以下构造函数:
public Fields getFieldsFromFile(byte[] file, String apiKey) throws ServiceException {
Document doc = new Document(new ByteArrayInputStream(file));
try {
return new Fields(new HashSet<>(Arrays.asList(doc.getMailMerge().getFieldNames())).toArray(new String[0]));
} catch (Exception e) {
...抛出错误...
}
}
英文:
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
there is a constructor 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...
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论