简体   繁体   English

使用 Base64 上传图像的 Struts2 操作表单数据最大大小

[英]Struts2 Action Form Data Maximum Size for Image Upload using Base64

I have a Struts2 action that receives a string containing an image in Base64 and another string for the image name.我有一个 Struts2 操作,它接收一个包含 Base64 图像的字符串和另一个用于图像名称的字符串。

Everything works well for small sized images.一切都适用于小尺寸图像。 But when I try to send a larger image, the Base64 and the image name strings are set to null in the action implementation.但是当我尝试发送更大的图像时,Base64 和图像名称字符串在操作实现中设置为 null。

In search for a solution I found that the default maximum size for the file upload is 2 MB.在寻找解决方案时,我发现文件上传的默认最大大小为 2 MB。 This limit can be increased with the following properties:可以使用以下属性增加此限制:

<constant name="struts.multipart.maxSize" value="104857600" />

<param name="maximumSize">52428800</param>

<param name="fileUpload.maximumSize">52428800</param>

However this is not working.然而,这是行不通的。 Probably this implementation is not a file upload but a two string POST request.可能这个实现不是文件上传,而是两个字符串的 POST 请求。

Is there a way I can increase the size of the post request?有没有办法增加帖子请求的大小? Or the problem is something else?或者问题出在别的什么地方?

public class GalleryAction  extends BaseAction {

private String imageBase64;

private String imageName;

...

public final String uploadOperation() throws Exception {

    if (this.imageBase64 == null || this.imageBase64.length() == 0
            || this.imageName == null || this.imageName.length() == 0) {
        throw new Exception("Invalid argument");
    }
    byte[] decodedBytes = Base64Decoder.decode2bytes(this.imageBase64);
    InputStream is = new ByteArrayInputStream(decodedBytes);

    Graphics graphics = MGraphics.insertImageToDataBase(this.imageName, is);

    // Issue server to sync image.
    RestUtils.syncImage(graphics.getId());

    JSONObject response = new JSONObject();

    response.put("statusCode", "0");
    jsonString  = response.toString();

    return JSON_RESPONSE;
}

...
}

EDIT: I forgot to publish the code for the image upload.编辑:我忘了发布图片上传的代码。

Gallery.prototype.upload = function(base64, imageName) {
var galleryObject = this;

galleryObject.loadingCallback(true);

$.post(this.urlBase + "/upload", {
    "imageBase64" : base64.match(/,(.*)$/)[1],
    "imageName" : imageName
}, function(data) {
    galleryObject.handlerErrorMessageCallback();

    galleryObject.loadingCallback(false);

    galleryObject.refreshImageGalleryCallback();

}, "json")

.fail(function() {
    galleryObject.handlerErrorMessageCallback("error.upload.image");

    galleryObject.loadingCallback(false);

});
}

The HTTP protocol specifications don't set a limit to the size of a POST message. HTTP 协议规范没有对 POST 消息的大小设置限制。 However, your application server does it (mainly to prevent DDoS attacks).但是,您的应用程序服务器会这样做(主要是为了防止 DDoS 攻击)。

Usually this threshold is 10 MegaBytes, and it is the one you are hitting.通常这个阈值是 10 兆字节,这就是你要达到的阈值。 You should then be able to customize this setting according to your AS specs.然后,您应该能够根据您的 AS 规范自定义此设置。

That said, this is not encouraged, and could lead to security vulnerabilities.也就是说,这是不鼓励的,并且可能导致安全漏洞。

The best thing would be to:最好的办法是:

  1. use multipart/form-data over application/x-www-form-urlencoded ;application/x-www-form-urlencoded使用multipart/form-data

  2. use File instead of String since what you're uploading are files, and not strings.使用File而不是String因为您上传的是文件,而不是字符串。

I changed the upload method to Form data.我将上传方法更改为表单数据。

Gallery.prototype.upload = function(base64, imageName) {

var galleryObject = this;
galleryObject.loadingCallback(true);

var request = new FormData();                   
request.append("imageBase64", base64.match(/,(.*)$/)[1]);
request.append("imageName", imageName);

$.ajax({url:this.urlBase + "/upload",
    data: request,
    type: "POST",
    processData: false,
    contentType: false,
    success: function(result)
        {
            galleryObject.handlerErrorMessageCallback();
            galleryObject.loadingCallback(false);
            galleryObject.refreshImageGalleryCallback();
        }
}).fail(function() {
    galleryObject.handlerErrorMessageCallback("error.upload.image");
    galleryObject.loadingCallback(false);
});
}

Using this post method the following property on strus.xml must exists使用此 post 方法, strus.xml 上的以下属性必须存在

<constant name="struts.multipart.maxSize" value="104857600" />

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM