0

I'm unable to save a Data URI in JSP. I am trying like this, is there any mistake in the following code?

<%@ page import="java.awt.image.*,java.io.*,javax.imageio.*,sun.misc.*" %>

function save_photo() 
{
    Webcam.snap(function(data_uri) 
    {
         document.getElementById('results').innerHTML =
                 '<h2>Here is your image:</h2>' + '<img src="'+data_uri+'"/>';
         var dat = data_uri;

         <% 
            String st = "document.writeln(dat)";

            BufferedImage image = null;
            byte[] imageByte;

            BASE64Decoder decoder = new BASE64Decoder();
            imageByte = decoder.decodeBuffer(st);
            ByteArrayInputStream bis = new ByteArrayInputStream(imageByte);
            image = ImageIO.read(bis);
            bis.close();

            if (image != null)
                ImageIO.write(image, "jpg", new File("d://1.jpg"));
            out.println("value=" + st); // here it going to displaying base64 chars
            System.out.println("value=" + st); //but here it is going to displaying document.writeln(dat)  
        %>
    }
}

Finally, the image is not saved.

msrd0
  • 7,816
  • 9
  • 47
  • 82
user3201607
  • 79
  • 1
  • 1
  • 9
  • Note that you mustn't use `BASE64Decoder` anymore. Since Java8, there is a [`java.util.Base64`](http://docs.oracle.com/javase/8/docs/api/java/util/Base64.html) class to handle this. Just replace `decoder.decodeBuffer(st)` with [`Base64.getDecoder().decode(st)`](http://docs.oracle.com/javase/8/docs/api/java/util/Base64.Decoder.html#decode-java.lang.String-) – msrd0 Sep 23 '14 at 16:35

2 Answers2

1

I think you didn't get the difference between JSP and JavaScript. While JSP is executed on the Server at the time your browser requires the web page, JavaScript is executed at the Client side, so in your browser, when you do an interaction that causes the JavaScript to run.

You Server (eg Apache Tomcat) will firstly execute your JSP code:

String st = "document.writeln(dat)";

BufferedImage image = null;
byte[] imageByte;

BASE64Decoder decoder = new BASE64Decoder();
imageByte = decoder.decodeBuffer(st);
ByteArrayInputStream bis = new ByteArrayInputStream(imageByte);
image = ImageIO.read(bis);
bis.close();

if (image != null)
    ImageIO.write(image, "jpg", new File("d://1.jpg"));
out.println("value=" + st);
System.out.println("value=" + st);

As you can see, nowhere is the value of st changed. Your broser will receive the following snippet from your server:

value=document.writeln(dat);

Since your browser is the one that executes JavaScript, he will execute it and show the Base64-encoded Image - but your server won't.

For the exact difference, read this article.


To make the code working, the easiest way is to redirect the page:

function(data_uri)
{
    // redirect
    document.location.href = 'saveImage.jsp?img='+data_uri;
}

Now, you can have a JSP-page called saveImage.jsp that saves the Image, and returns the webpage you had already, and write the dara_uri into the element results.

Another, but more difficult way is to use AJAX. Here is an introduction to it.

msrd0
  • 7,816
  • 9
  • 47
  • 82
  • document.href = 'saveImage.jsp?img='+data_uri; eventhough its not working. in saveImage.jsp i'm writing the above java code to save img. – user3201607 Sep 23 '14 at 16:47
  • @user3201607 Sorry, its `document.location.href` – msrd0 Sep 23 '14 at 16:49
  • Agreed. The general answer to this question first must be to help the OP understand the difference between 'Java' as a server-side programming language in this example (Java Server Page) versus 'JavaScript' or ECMA - which is a completely different programming language, generally related to web-browser execution (though not exclusively as used in variations of Google's V8 engine). After this, one can get into the details of how to upload a file using one or the other. Here is one: http://stackoverflow.com/questions/2422468/how-to-upload-files-to-server-using-jsp-servlet – Darrell Teague Sep 23 '14 at 16:55
  • sorry, its redirecting to savaimage.jsp page but its displaying empty page n image also not saved. url look like this after redirecting http://localhost:8084/webcam/savaimage.jsp?img=data:image/jpeg;base64,/9j/4AA... – user3201607 Sep 23 '14 at 16:55
  • @user3201607 You need to url-encode it (so that / becomes %2F and so on), else you will not be able to see the correct image. I just posted an example and didn't cared about things like that – msrd0 Sep 23 '14 at 16:58
  • in server its displaying error Sep 23, 2014 10:22:20 PM org.apache.coyote.http11.AbstractHttp11Processor proces s INFO: Error parsing HTTP request header Note: further occurrences of HTTP header parsing errors will be logged at DEBUG level. – user3201607 Sep 23 '14 at 17:00
  • @user3201607 This happens if you call an not-encoded url in your browser containing chars like '/' in the arguments of your script. See my comment above – msrd0 Sep 23 '14 at 17:01
  • i'm doing like var a=encodeURI(data_uri); but same problem – user3201607 Sep 23 '14 at 17:22
0

You are trying to use JavaScript variables in Java code. Java code is running on your server, while Javascript code runs in user's browser. By the time JavaScript code executes, your Java code has already been executed. Whatever you're trying to do, you have to do it in pure javascript, or send an AJAX call to your server when your Javascript code has done it's thing.

dimoniy
  • 5,820
  • 2
  • 24
  • 22