如何在 java websocket 服务器上读取通过 websocket 发送的 javascript BLOB 数据
How to read javascript BLOB data sent over websocket on java websocket server
我正在尝试通过 javascript 将图片文件作为 BLOB 类型的文件上传到我的网页,然后将该文件发送到我的 java websocket 服务器以将文件存储在服务器上用于网页。
我在websocker服务器java端的代码是读取BLOB数据和写入文件如下,
public void testMethod(final WebMessage message){
ByteBuffer b = null;
try {
b = message.getByteBufferArg(0);
} catch (WebMessageException e2) {
// TODO Auto-generated catch block
e2.printStackTrace();
}
File file = new File("/user/Desktop/pic.jpg");
try {
file.createNewFile();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
try {
FileOutputStream stream = new FileOutputStream(file.getAbsoluteFile());
stream.write(b.array());
logger.info("done writing file");
stream.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
logger.trace(e.getMessage());
}
}
然后当我尝试打开图片时显示 Error interpreting JPEG image file (Not a JPEG file: starts with 0x00 0x00)
。
基本上我想知道如何读取发送到我的 websocket 服务器的 BLOB 数据,以及如何将这些数据写入可用文件?
解决这个问题的方法是不要从 javascript 端将数据作为 BLOB 发送,您需要将数据读取为某种可读格式。在这种情况下,我使用 FileReader
API 将 BLOB 数据读取为 dataurl
。使用该方法读取后,数据为 base64
可读格式,可以通过 websocket 发送,并在解码 base64 字符串后写入另一端的文件。
所以从 Blob 读取的唯一方法是使用 FileReader (https://developer.mozilla.org/en-US/docs/Web/API/FileReader), once you've read the data from a blob (I would just read in a BinaryArray - https://developer.mozilla.org/en-US/docs/Web/API/FileReader/readAsBinaryString) 然后您可以直接将其发送到输出流中的文件。
来自文档 - https://developer.mozilla.org/en/docs/Web/API/Blob:
Example for extracting data from a Blob
The only way to read content from a Blob is to use a FileReader. The following code reads the content of a Blob as a typed array.
var reader = new FileReader();
reader.addEventListener("loadend", function() {
// reader.result contains the contents of blob as a typed array
});
reader.readAsArrayBuffer(blob);
By using other methods of FileReader, it is possible to read the contents of a Blob as a string or a data URL.
我正在尝试通过 javascript 将图片文件作为 BLOB 类型的文件上传到我的网页,然后将该文件发送到我的 java websocket 服务器以将文件存储在服务器上用于网页。
我在websocker服务器java端的代码是读取BLOB数据和写入文件如下,
public void testMethod(final WebMessage message){
ByteBuffer b = null;
try {
b = message.getByteBufferArg(0);
} catch (WebMessageException e2) {
// TODO Auto-generated catch block
e2.printStackTrace();
}
File file = new File("/user/Desktop/pic.jpg");
try {
file.createNewFile();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
try {
FileOutputStream stream = new FileOutputStream(file.getAbsoluteFile());
stream.write(b.array());
logger.info("done writing file");
stream.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
logger.trace(e.getMessage());
}
}
然后当我尝试打开图片时显示 Error interpreting JPEG image file (Not a JPEG file: starts with 0x00 0x00)
。
基本上我想知道如何读取发送到我的 websocket 服务器的 BLOB 数据,以及如何将这些数据写入可用文件?
解决这个问题的方法是不要从 javascript 端将数据作为 BLOB 发送,您需要将数据读取为某种可读格式。在这种情况下,我使用 FileReader
API 将 BLOB 数据读取为 dataurl
。使用该方法读取后,数据为 base64
可读格式,可以通过 websocket 发送,并在解码 base64 字符串后写入另一端的文件。
所以从 Blob 读取的唯一方法是使用 FileReader (https://developer.mozilla.org/en-US/docs/Web/API/FileReader), once you've read the data from a blob (I would just read in a BinaryArray - https://developer.mozilla.org/en-US/docs/Web/API/FileReader/readAsBinaryString) 然后您可以直接将其发送到输出流中的文件。
来自文档 - https://developer.mozilla.org/en/docs/Web/API/Blob:
Example for extracting data from a Blob
The only way to read content from a Blob is to use a FileReader. The following code reads the content of a Blob as a typed array.
var reader = new FileReader();
reader.addEventListener("loadend", function() {
// reader.result contains the contents of blob as a typed array
});
reader.readAsArrayBuffer(blob);
By using other methods of FileReader, it is possible to read the contents of a Blob as a string or a data URL.