Hey All,
Below is my code where I am trying to decode the dataUrl recieved from jsp page and trying to save it as png file. But the png file is empty.
Just tell me what I am missing.
JSP Page:
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
<script type="text/javascript" src="js/wizard_js/jquery.js"></script>
<script>
function publish()
{
var canvas=document.getElementById('mycanvas');
//var image = new Image();
var gg= canvas.toDataURL();
// alert(cat);
var jsString=$.ajax({
url:"new_file",
async:false,
type: 'post',
data:"image_path="+gg
}).responseText;
alert(jsString);
}
</script>
</head>
<body>
<canvas id="mycanvas" style="width:200px;height:300px;background-color: red">
</canvas>
<input type="button" value="ok" id="btn1" onclick="publish()"/>
</body>
</html>
And here is the servlet:
String img = request.getParameter("image_path");
try {
int start = img.indexOf(",");
img = img.substring(start + 1);
out.println(img);
BASE64Decoder bd = new BASE64Decoder();
byte[] buffer = bd.decodeBuffer(img);
BufferedImage bfi = ImageIO.read(new ByteArrayInputStream(buffer));
File outputfile = new File("C:/hello/saved.png");
ImageIO.write(bfi , "png", outputfile);
bfi.flush();
}
catch(Exception e)
{
out.println(e);
}