I am trying to upload a picture file on my webpage through javascript as a BLOB type of file, then send that file to my java websocket server to store the file on the server used for the webpage.
My code on the websocker server java side is as to read the BLOB data and write the file is as follows,
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());
}
}
Then when I try to open the picture it says Error interpreting JPEG image file (Not a JPEG file: starts with 0x00 0x00).
Basically I would like to know then how I can read this BLOB data that was sent to my websocket server, and how can I write this data out to a useable file?