简体   繁体   中英

How to parse multi-part form data in wicket

When my page gets hit from a third party page, I get the below data in request payload:

Content-type: multipart/form-data, boundary----------14048
Content-Length = 590

----------14048
Content-disposition: form-data; name ="xyz"

{"abc":"lmn","def":"ghi"}
----------14048

I need to read the JSON string from this parameter in my Java class. How can I do that?

My current code looks like this:

IRequestParameters requestParameters = getRequest().getPostParameters();
    if (requestParameters != null && requestParameters.getParameterNames().contains( "abc" )&&requestParameters.getParameterValue( "abc" ) != null){
        value = requestParameters.getParameterValue( "abc" ).toString();
}

Thanks in advance.

First, you need to parse multipart form data in Wicket :

MultipartServletWebRequest multiPartRequest = 
      webRequest.newMultipartWebRequest(getMaxSize(), "ignored");
// multiPartRequest.parseFileParts(); // this is needed after Wicket 6.19.0+
IRequestParameters params = multiPartRequest.getRequestParameters();

Then you need to parse the JSON fragment , one way to do that is by using org.json .

import org.json.*;

JSONObject jsondict = new JSONObject(params.getParameter("xyz");

Then you need to get the JSON parameter you are interested in:

string payload = jsondict.getString("abc");

The below code works fine for me.

HttpSevletRequest request = (HttpSevletRequest )getRequest.getContainerRequest();
try{
 InputStreamReader inputReader = new InputStreamReader(request.getInputStream());
    BufferedReader reader = new BufferedReader(inputReader );
    for(String line;(line = reader.readLine())!=null;){
        if(line.contains("abc")){
            //perform task....
     }
    }
}catch(IOException e){
 //logs
}

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