如何在wicket中解析多部分表单数据

时间:2017-04-15 06:18:13

标签: java wicket

当我的页面从第三方页面被点击时,我在请求有效负载中获得以下数据:

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

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

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

我需要在Java类中读取此参数的JSON字符串。我怎么能这样做?

我目前的代码如下:

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

提前致谢。

2 个答案:

答案 0 :(得分:2)

首先,您需要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();

然后您需要parse the JSON fragment,一种方法是使用org.json

import org.json.*;

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

然后你需要获得你感兴趣的JSON参数:

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

答案 1 :(得分:-1)

以下代码对我来说很好。

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
}