Sumana Rakshit

Greenhorn
+ Follow
since Dec 11, 2012
Merit badge: grant badges
For More
Cows and Likes
Cows
Total received
0
In last 30 days
0
Total given
0
Likes
Total received
0
Received in last 30 days
0
Total given
0
Given in last 30 days
0
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by Sumana Rakshit

The json is passing the encoded string of canvas and is trying to decode it in java file. but the file which is being created is not saving the canvas image. I wanted to kno what is wrong with my java code.
11 years ago
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);
}

11 years ago