简体   繁体   中英

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.

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.

In search for a solution I found that the default maximum size for the file upload is 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.

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. However, your application server does it (mainly to prevent DDoS attacks).

Usually this threshold is 10 MegaBytes, and it is the one you are hitting. You should then be able to customize this setting according to your AS specs.

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 ;

  2. use File instead of String since what you're uploading are files, and not strings.

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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