0

我正在尝试从画布中获取图像并将图像作为 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:在 javax 的 com.sun.imageio.plugins.png.PNGImageReader.read(Unknown Source) 的 com.sun.imageio.plugins.png.PNGImageReader.readImage(Unknown Source) 处读取 PNG 图像数据时出错。 imageio.ImageIO.read(Unknown Source) at javax.imageio.ImageIO.read(Unknown Source) at com.tcs.ilp.t198.osms.prdm.controller.ImageSaver.doPost(ImageSaver.java:50) at javax.servlet .http.HttpServlet.service(HttpServlet.java:637) 在 javax.servlet.http.HttpServlet.service(HttpServlet.java:717) 在 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) at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:470) at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127) at org .apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103) at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109) at 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) at java.lang.Thread.run(Unknown Source) 原因:java.util.zip.ZipException:在 java.io.BufferedInputStream.fill(Unknown Source) 在 java.io.BufferedInputStream.read(Unknown Source) 在 java.io.FilterInputStream.read 的 java.util.zip.InflaterInputStream.read(Unknown Source) 的无效位长度重复(Unknown Source) at com.sun.imageio.plugins.png.PNGImageReader.decodePass(Unknown Source) at com.sun.imageio.plugins.png.PNGImageReader.decodeImage(Unknown Source) ... 20 更多

4

0 回答 0