【发布时间】:2015-06-23 19:46:51
【问题描述】:
我在将 byte 数组(使用 Office.js 从 Microsoft Office 的任务窗格中获取)保存到 Word 文档文件(在服务器端)时遇到了一些麻烦。这就是我正在做的:
- 我正在使用library 获取 Word 文档的内容
JavaScript
$('#push').click(function () {
$.when(OffQuery.getContent({ sliceSize: 1000000 }, function (j, data, result, file, opt) {
// ...nothing interesting here
})).then(function (finalByteArray, file, opt) {
// (1) this line is changed...see the answer
var fileContent = Base64.encode(finalByteArray); //encode the byte array into base64 string.
$.ajax({
url: '/webcontext/api/v1/documents',
// (2) missing setting (see the answer)
data: fileContent,
type: 'POST'
}).then(function () {
// updateStatus('Done sending contents into server...');
});
}).progress(function(j, chunkOfData, result, file, opt){
// ...nothing interesting here
});
- 然后在 Spring 控制器中我这样做:
Java/Spring
@RequestMapping(method = RequestMethod.POST) // As OOXML
public void create(@RequestBody String fileContent, HttpServletRequest request) throws Exception { // TODO
LOGGER.debug("{} {}", request.getMethod(), request.getRequestURI());
//LOGGER.debug("fileContent: {}", fileContent);
try {
val base64 = Base64.decodeBase64(fileContent); // From Apache Commons Codecs
FileUtils.writeByteArrayToFile(new File("assets/tests/output/some_file.docx"), base64);
} catch (IOException e) {
LOGGER.error("Crash! Something went wrong here while trying to save that...this is why: ", e);
}
}
...但是文件按原样保存;基本上是将byte 数组作为文本文档保存到文件中。
我错过了什么吗?你有什么线索吗?有人使用过 Office.js、任务窗格和类似的东西吗?
提前谢谢...
更新 1
原来finalByteArray 正在转换为 Base64 字符串 (fileContent),但是当我尝试在 Java 中执行反向操作时不起作用...如果有人之前这样做过,请告诉我知道。我试过了:
...在 Java 端(将 Base64 String 解码为 byte 数组):
- 默认Base64编码器/解码器
- Base64 Apache codec
【问题讨论】:
-
什么是base64变量实数数据类型?
-
@stiv 它的类型为
byte[] -
如果是我,我会将“val”更改为“byte[]”,看看会发生什么。接下来,我将发送一个简单的 docx 文件(不使用 Office.js),然后将副本与原始文件进行比较。如果可行,则问题出在 Office.js 生成的文件上。这里有一些使用 Office.js 发送 docx 文件的 MS 示例:Get the whole document“为 Word 创建一个任务窗格应用程序,该应用程序可以引用所有当前文档的内容并将其发送到 Web 服务器。”
-
为什么在解码的时候把fileContent的最后一个字符截掉?
-
请记录 base64 长度和输出文件长度并在此处报告。
标签: javascript java spring ms-office openxml