因此,我正在向Java发送一个JavaScript ArrayBuffer对象,以通过XMLHttpRequest将其发送到服务器。但是,当我的Java代码收到它时,它会以 [object ArrayBuffer] 作为值转换为String变量。
其他信息:
将ArrayBuffer发送到Java的代码段:
var headerSize = 36;
var imageInFile = new Packages.java.io.FileInputStream(file);
var imageData = Byte.createByte(file);
imageInFile.read(imageData);
var dataUrl = Packages.java.util.Base64.getEncoder().encodeToString(imageData);
dataUrl = 'data:image/jpeg;base64,' + dataUrl;
var bytesArray = Bytes.fromBase64(dataUrl);
var buffer = new ArrayBuffer((headerSize + bytesArray.length));
var bufferView = new Uint8Array(buffer);
bufferView.set(Bytes.fromGuid(videoPushParams.VideoId, 16)); // Video id
bufferView.set(Bytes.fromInt(new Date().getTime(), 8), 16); // Timestamp
bufferView.set(Bytes.fromInt(++counter, 4), 24); // Frame count
bufferView.set(Bytes.fromInt(bytesArray.length, 4), 28); // Frame size in bytes
bufferView.set(Bytes.fromInt(headerSize, 2), 32); // Header size in bytes
bufferView.set(Bytes.fromInt(0, 2), 34); // Header extension flags
bufferView.set(bytesArray, headerSize);
var ajaxRequest = new Packages.org.jdesktop.http.async.XmlHttpRequest();
ajaxRequest.open('POST', address);
ajaxRequest.responseType = 'arraybuffer';
ajaxRequest.setRequestHeader('Content-type', 'text/xml; charset=utf-8');
ajaxRequest.send(buffer);
应该接收ArrayBuffer的Java代码:
public void send() {
send((String) null);
}
public void send(String content) {
if (readyState != ReadyState.OPEN) {
throw new IllegalStateException(
"AsyncHttpRequest must be in an OPEN state before " + "invokation of the send() method");
}
worker = createAsyncWorker(content);
worker.sendRequest(getSession(), content);
}
public void send(Document dom) {
// convert the dom to a String, and send that
if (dom == null) {
send((String) null);
} else {
send(XPathUtils.toXML(dom));
}
}
我还尝试添加另一种方法,该方法将字节数组作为参数,但仍然无法正常工作。