使用ajax发送图像数据URL并将文件另存为png

时间:2013-05-30 19:21:14

标签: java javascript image servlets

我正在尝试从画布获取图像并将图像作为dataurl字符串发送到服务器 在服务器上我想将dataurl保存为png图像。 我做了类似的事。

1>上传代码以将dataurl字符串发送到服务器。

function upload(){
    var url ="<%=request.getContextPath()%>/ImageSaver";
    xmlhttp = initialize();
    xmlhttp.open('POST',url,true);
    xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
    xmlhttp.send("imgdata="+img);
    xmlhttp.onreadystatechange= function()
    {
        if(xmlhttp.readyState==4 && (xmlhttp.status==200 ||xmlhttp.status==404))    
        {
            alert("Image Uploaded Succesfully.");
            document.getElementById('img').src = img;
            if(xmlhttp.status==404 )
            {
                //  next.style.display="none";
            }
        }
        else
        {
            content.innerHTML = "Error";
        }   
    };
}

2 - ;在服务器上,我得到了dataurl并将其保存为图像。

try{
            String img64 = request.getParameter("imgdata");
            img64 = img64.replaceFirst("data:image/png;base64,","");
            byte[] decodedBytes = DatatypeConverter.parseBase64Binary(img64);
            System.out.println(""+img64);
            BufferedImage bfi = ImageIO.read(new ByteArrayInputStream(decodedBytes));    
            File outputfile = new File("D:/saved.png");
            ImageIO.write(bfi,"png",outputfile);
            bfi.flush();
         }
         catch(Exception e)
         {  
              e.printStackTrace();    
         }

但我在服务器端收到此错误

  

javax.imageio.IIOException:读取PNG图像数据时出错   com.sun.imageio.plugins.png.PNGImageReader.readImage(未知来源)     at com.sun.imageio.plugins.png.PNGImageReader.read(Unknown Source)     在javax.imageio.ImageIO.read(未知来源)at   javax.imageio.ImageIO.read(未知来源)at   com.tcs.ilp.t198.osms.prdm.controller.ImageSaver.doPost(ImageSaver.java:50)     在javax.servlet.http.HttpServlet.service(HttpServlet.java:637)at   javax.servlet.http.HttpServlet.service(HttpServlet.java:717)at at   org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)     在   org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)     在   org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)     在   org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)     在   org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:470)     在   org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)     在   org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103)     在   org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)     在   org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:293)     在   org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:861)     在   org.apache.coyote.http11.Http11Protocol $ Http11ConnectionHandler.process(Http11Protocol.java:606)     在   org.apache.tomcat.util.net.JIoEndpoint $ Worker.run(JIoEndpoint.java:489)     在java.lang.Thread.run(未知来源)引起:   java.util.zip.ZipException:无效的位长重复在   java.util.zip.InflaterInputStream.read(未知来源)at   java.io.BufferedInputStream.fill(未知来源)at   java.io.BufferedInputStream.read(未知来源)at   java.io.FilterInputStream.read(未知来源)at   com.sun.imageio.plugins.png.PNGImageReader.decodePass(未知来源)     at com.sun.imageio.plugins.png.PNGImageReader.decodeImage(Unknown)   来源)... 20多个

0 个答案:

没有答案