i'm new to java web App i was trying to create simple chat webapp that send images to webserver then the server will send them to other client at first i had problem that the websocket wont send large data so i send my images using
http request
after encoding them to base64 then after i was able to decode it and store it,i was trying to seend it from the server to the other client
i did not find any solution the only way to send it to the other client is using websocket
this is the third day of searching and i'm thinking of given up since there is no resources for noobies
i saw alot of code but they was in high level i hope if some one have the answer of
How can i send the image to the client ?
please Try to explain it in a simple way
the javascript function responsible for sending the img
send=function () {
var file =document.getElementById("image").files[0];
var content_type ={
headers:{
"content-type" : "multipart/form-data"
}
};
var formdata = new FormData();
formdata.append('image',file);
axios.post('AddImage',formdata,content_type)
.then(function (response) {
console.log(response);
})
.catch(function (error) {
console.log(error);
});
}
the post method that send the file
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
Part file=request.getPart("image");
InputStream in = file.getInputStream();
byte[] Byte_file = new byte[in.available()];
in.read(Byte_file);
String encodedString = "{encodedString : "+"data:image/png;base64,"+new String(Byte_file)+"}";
//sending the String to the other client via his session
socket.session.getAsyncRemote().sendText(encodedString);
}