java—如何使用struts2 mvc架构将画布图像存储到服务器

eit6fx6z  于 2021-06-30  发布在  Java
关注(0)|答案(1)|浏览(355)

这里是ajax调用请求

//sending request to action
var canvas=document.getElementById('can');
var dataURL = canvas.toDataURL("image/png"); 
$.ajax({    type: 'POST',  
            url: "/FMVMLAST/Canvasimage.action?img64="+dataURL.replace(/^data:image\/(png|jpg);base64,/, ""), 
            dataType: 'json', 
            async: false , 
            contentType: 'application/json; charset=utf-8', 
            success: function(data){}
});

下面的java代码将接收请求并将base64数据转换为 image/png 格式化并存储到服务器。

import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.File;

import javax.imageio.ImageIO;
import javax.imageio.stream.ImageInputStream;
import javax.xml.bind.DatatypeConverter;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;

public class Canvasimage extends ActionSupport{
String img64= ServletActionContext.getRequest().getParameter("img64");

public String Canvasimage()
{
    try{
    byte[] imgData = DatatypeConverter.parseBase64Binary(img64 );
    BufferedImage bfi = ImageIO.read((ImageInputStream) new ByteArrayInputStream(imgData));    
    File outputfile = new File("saved.png");
    ImageIO.write(bfi,"png", outputfile);
    bfi.flush();

    }catch(Exception e){System.out.println("In canvas"+e.getMessage());}
    return "success";
}

public String getImg64() {
    return img64;
}

public void setImg64(String img64) {
    this.img64 = img64;
}

}

我在struts.xml中添加了这个操作

<action name="Canvasimage" class="user.actions.Canvasimage" method="Canvasimage">   
           <result type="json"></result>
</action>

每当我让这个请求控制台显示404错误时,任何人都可以告诉我代码中哪里出错了。
编辑
使用ajax发送请求

var canvas=document.getElementById('can');
var dataURL = canvas.toDataURL("image/png");
var data = {
        img64:dataURL.replace(/^data:image\/(png|jpg);base64,/, ""),
    };

$.ajax( 
        {   
            type: 'POST',  
            url: "/FMVMLAST/Canvasimage", 
            data:data,
            success: function(data){}
        });

struts.xml文件

<struts>

    <bean type="org.apache.struts2.dispatcher.multipart.MultiPartRequest" name="jakartaStream" class="org.apache.struts2.dispatcher.multipart.JakartaMultiPartRequest" scope="default" />
<constant name="struts.multipart.parser" value="jakarta-stream" />
<constant name="struts.multipart.maxSize" value="1000000" />
<package name="default" extends="struts-default,json-default">
     <action name="Canvasimage" class="user.actions.Canvasimage" method="Canvasimage">   
            <result type="json"></result>
    </action>
</package>

java类

import org.apache.commons.codec.binary.Base64;
import org.apache.struts2.interceptor.ServletRequestAware;

import com.opensymphony.xwork2.ActionSupport;

public class Canvasimage extends ActionSupport{

    private String img64;
    public byte[] valueDecoded;

    public String Canvasimage()
    {
        setImg64(img64);
        System.out.println(""+img64);
        try{

              byte[] decoded_id=valueDecoded;
              FileOutputStream file1 = new  FileOutputStream("C:\\image.png");

              file1.write(decoded_id);
              file1.close();

            System.out.println("incanvas"+decoded_id);
        }catch(Exception e){System.out.println("In canvas"+e);}
        return "success";
    }

    public String getImg64() {
        return img64;
    }

    public void setImg64(String img64) {
        this.img64 = img64;
        toImg64Byte();
    }

    private void toImg64Byte() {
        // TODO Auto-generated method stub
        byte[] img=img64.getBytes();
         this.valueDecoded = Base64.decodeBase64(img);
    }

}
vm0i2vca

vm0i2vca1#

你为什么要通过查询字符串发送这么长的参数|
使用post参数:

var dataURL = document.getElementById('can').toDataURL("image/png"); 

var data = {
    img64 : dataURL.replace(/^data:image\/(png|jpg);base64,/, "")
}

$.ajax({
   type : 'POST',  
    url : "/FMVMLAST/Canvasimage.action",
   data : data
...

您可能达到了某个querystring长度上限,或者您发送的数据不是有效的json,所以在发布它时不要使用json数据类型。
还可以使用私有限定符、公共getter和setter声明变量,避免手动读取请求:

private String img64;

public String getImg64() {
    return img64;
}

public void setImg64(String img64) {
    this.img64 = img64;
}

相关问题