aspose file tools*
The moose likes Servlets and the fly likes Servlet creating empty file on saving canvas image Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "Servlet creating empty file on saving canvas image" Watch "Servlet creating empty file on saving canvas image" New topic
Author

Servlet creating empty file on saving canvas image

Sumana Rakshit
Greenhorn

Joined: Dec 11, 2012
Posts: 2
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
Bartender

Joined: Oct 14, 2005
Posts: 18656
    
    8

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

Joined: Dec 11, 2012
Posts: 2
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Servlet creating empty file on saving canvas image