[英]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 請求。
有沒有辦法增加帖子請求的大小? 或者問題出在別的什么地方?
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);
});
}
HTTP 協議規范沒有對 POST 消息的大小設置限制。 但是,您的應用程序服務器會這樣做(主要是為了防止 DDoS 攻擊)。
通常這個閾值是 10 兆字節,這就是你要達到的閾值。 然后,您應該能夠根據您的 AS 規范自定義此設置。
也就是說,這是不鼓勵的,並且可能導致安全漏洞。
最好的辦法是:
在application/x-www-form-urlencoded
使用multipart/form-data
;
使用File
而不是String
因為您上傳的是文件,而不是字符串。
我將上傳方法更改為表單數據。
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" />
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.