Expand my Community achievements bar.

Dive into Adobe Summit 2024! Explore curated list of AEM sessions & labs, register, connect with experts, ask questions, engage, and share insights. Don't miss the excitement.
SOLVED

AEM 6.5 multipart form JSON data servlet processing

Avatar

Level 8

Hello,

 

Could some help me on the below and provide me with pointers.

I have multipart form data being feed to servlet in the below JSON format.
My need is to read this and send the file and JSON data to another endpoint in the same JSON format with encoding of few values

 

 

Form JSON data to servlet:-

{
"param1": "value1",
"parm2": "value2",

"josnobjectParam1": {
"jsonobjparam1": "value3",
"jsonobjparam2": "value4"
},

"josnobjectParam2": {
"jsonobjparam3": "value5",
"jsonobjparam4": "value6"
},

"filesent": [
{filename: "name.pdf"}
]
}

 

A>How could I achieve it.

In the servlet ,I am trying to read the json form data using request.getRequestParameterMap and adding it to MultipartEntityBuilder . Now when reading the value of josnobjectParam1 and josnobjectParam2 it shows as object .How to read the values inside it .For example the key jsonobjparam1  and its value

I was using the below link as reference
https://github.com/Adobe-Consulting-Services/acs-aem-commons/blob/master/bundle/src/main/java/com/ad...

 

MultipartEntityBuilder requestEntity = MultipartEntityBuilder.create();


Map < String, RequestParameter[] > requestParameters = request.getRequestParameterMap();
for (final Map.Entry < String, RequestParameter[] > entry: requestParameters.entrySet()) {
RequestParameter[] reqParam = entry.getValue();
if ("josnobjectParam1".equals(entry.getKey()) || "josnobjectParam2".equals(entry.getKey())) {
for (RequestParameter fileParam: reqParam) {
/** Here how to read the jsonobjparam1 , jsonobjparam2 ,jsonobjparam3 ,jsonobjparam4
Also will need to encode the value3,value4,value5,value6 before adding to requestEntity**/
String paramValue = StringUtils.defaultIfBlank(String.valueOf(reqParam[0]), StringUtils.EMPTY);
requestEntity.addPart(entry.getKey(), paramValue);
}
}
}

b> Now  need to send the MultipartEntityBuilder data to other end point in JSON format , how to achieve it
I was using the below link as reference
https://gist.github.com/damianmcdonald/b720d9798e0d84092bbd#L39

 

requestEntity.addTextBody("characterProfile", jsonToSend.toString(), ContentType.APPLICATION_JSON);

can we add like the above and send to another endpoint .please clarify and also I would want to just send the JSON data without any key is that possible so that it is sent as object

{
"param1": "value1",
"parm2": "value2",

"josnobjectParam1": {
"jsonobjparam1": "value3",
"jsonobjparam2": "value4"
},

"josnobjectParam12": {
"jsonobjparam3": "value5",
"jsonobjparam4": "value6"
},


}

 

 

Thanks,

Srinivas

1 Accepted Solution

Avatar

Correct answer by
Community Advisor

Hi @srinivas_chann1 

 

You can send the file data in the form of form-data like this:

if(ajaxfile.files[0] !== undefined)
{
formData.append("file", ajaxfile.files[0]);
}

your ajax call should be like this

$.ajax({
type: "POST",
enctype: 'multipart/form-data',
url: path,
data: formData,
processData: false,
contentType: false,
cache: false,
success: function (data) {},
error: function (data, status, er) {}
});

Read the value in the servlet like this:

Part filePart = request.getPart("file");
if (filePart != null) {
}
}

Hope it helps!

Thanks,
Kiran Vedantam

View solution in original post

2 Replies

Avatar

Correct answer by
Community Advisor

Hi @srinivas_chann1 

 

You can send the file data in the form of form-data like this:

if(ajaxfile.files[0] !== undefined)
{
formData.append("file", ajaxfile.files[0]);
}

your ajax call should be like this

$.ajax({
type: "POST",
enctype: 'multipart/form-data',
url: path,
data: formData,
processData: false,
contentType: false,
cache: false,
success: function (data) {},
error: function (data, status, er) {}
});

Read the value in the servlet like this:

Part filePart = request.getPart("file");
if (filePart != null) {
}
}

Hope it helps!

Thanks,
Kiran Vedantam