Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Servlet creating empty file on saving canvas image

 
Sumana Rakshit
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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);
}

 
Paul Clapham
Sheriff
Posts: 21416
33
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You seem to believe that the AJAX code somehow uploads the canvas to your servlet in such a way that the servlet can access the contents of the canvas. I don't know anything about the HTML 5 <canvas> element but I'm skeptical about that. My google time for this thread is up so... do you have some reason to believe that the canvas's URL is accessible from another machine?
 
Sumana Rakshit
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic