Struts2 使用 Base64 上传图片的操作表单数据最大大小
Struts2 Action Form Data Maximum Size for Image Upload using Base64
我有一个 Struts2 操作,它接收一个包含 Base64 图像的字符串和另一个图像名称字符串。
一切都适用于小尺寸图像。但是当我尝试发送更大的图像时,Base64 和图像名称字符串在操作实现中被设置为 null。
在寻找解决方案时,我发现文件上传的默认最大大小为 2 MB。可以使用以下属性增加此限制:
<constant name="struts.multipart.maxSize" value="104857600" />
<param name="maximumSize">52428800</param>
<param name="fileUpload.maximumSize">52428800</param>
但是这不起作用。可能这个实现不是文件上传而是两个字符串 POST 请求。
有什么方法可以增加 post 请求的大小?或者是其他问题?
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;
}
...
}
编辑:
我忘了发布图片上传的代码。
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);
});
}
我把上传方式改成Form数据了
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);
});
}
使用此 post 方法 strus.xml 上的以下 属性 必须存在
<constant name="struts.multipart.maxSize" value="104857600" />
HTTP 协议规范未对 POST 消息的大小设置限制。但是,你的应用服务器做到了(主要是防止DDoS攻击)。
通常这个阈值是 10 兆字节,这就是您要达到的阈值。然后您应该能够根据您的 AS 规范自定义此设置。
也就是说,不鼓励这样做,并且可能会导致安全漏洞。
最好的办法是:
在 application/x-www-form-urlencoded
上使用 multipart/form-data
;
使用 File
而不是 String
因为您上传的是文件,而不是字符串。
我有一个 Struts2 操作,它接收一个包含 Base64 图像的字符串和另一个图像名称字符串。
一切都适用于小尺寸图像。但是当我尝试发送更大的图像时,Base64 和图像名称字符串在操作实现中被设置为 null。
在寻找解决方案时,我发现文件上传的默认最大大小为 2 MB。可以使用以下属性增加此限制:
<constant name="struts.multipart.maxSize" value="104857600" />
<param name="maximumSize">52428800</param>
<param name="fileUpload.maximumSize">52428800</param>
但是这不起作用。可能这个实现不是文件上传而是两个字符串 POST 请求。
有什么方法可以增加 post 请求的大小?或者是其他问题?
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;
}
...
}
编辑: 我忘了发布图片上传的代码。
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);
});
}
我把上传方式改成Form数据了
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);
});
}
使用此 post 方法 strus.xml 上的以下 属性 必须存在
<constant name="struts.multipart.maxSize" value="104857600" />
HTTP 协议规范未对 POST 消息的大小设置限制。但是,你的应用服务器做到了(主要是防止DDoS攻击)。
通常这个阈值是 10 兆字节,这就是您要达到的阈值。然后您应该能够根据您的 AS 规范自定义此设置。
也就是说,不鼓励这样做,并且可能会导致安全漏洞。
最好的办法是:
在
application/x-www-form-urlencoded
上使用multipart/form-data
;使用
File
而不是String
因为您上传的是文件,而不是字符串。