2

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);

});
}
Andrea Ligios
  • 49,480
  • 26
  • 114
  • 243
Raimundo
  • 605
  • 7
  • 21

2 Answers2

1

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.

Andrea Ligios
  • 49,480
  • 26
  • 114
  • 243
  • 1
    This approach should have been be used since the beginning of the development. It is much easier to configure file upload in Struts2 than the the approach taken . Thanks. – Raimundo Nov 16 '15 at 13:30
0

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" />
Raimundo
  • 605
  • 7
  • 21