无法处理XPage ServiceBean中multipart/form-data POST请求的主体。

huangapple go评论71阅读模式
英文:

Unable to get a handle on the body of a multipart/form-data POST request in XPage ServiceBean

问题

I have an xpage based on a bootstrap framework which uses jQuery $post to submit forms contained within the page. These get posted using multipart/form-data content-type so I have no control over that. I have set up a REST.xsp containing a RESTService which calls a java ServiceBean. The latest method I have tried is as follows:

我有一个基于Bootstrap框架的xpage,使用jQuery $post来提交页面中包含的表单。这些表单使用multipart/form-data内容类型进行提交,所以我无法控制它。我已经设置了一个包含RESTService的REST.xsp,该服务调用一个Java ServiceBean。我最近尝试的方法如下:

private void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException {
// TODO Auto-generated method stub

try{
    ServletFileUpload upload = new ServletFileUpload();
    FileItemIterator iterator = upload.getItemIterator(request);
    System.out.println("test");
    while (iterator.hasNext()) {
         System.out.println("test2");
        FileItemStream item = iterator.next();
        InputStream stream = item.openStream();

        if (item.isFormField()) {
            byte[] str = new byte[stream.available()];
            stream.read(str);
            String pFieldValue = new String(str,"UTF8");
            
            
            System.out.println("text Value : "+pFieldValue);
            

            if(item.getFieldName().equals("textBoxFormElement")){
                System.out.println("text Value : "+pFieldValue);
            }
          }
        }
}catch (Exception e) {
    e.printStackTrace();
}

}

but I just can't seem to get the server to pick up the POSTed form data. I can see the "test" on the console but it's not hitting "test2".

但是我似乎无法让服务器接收到已发布的表单数据。我可以在控制台上看到“test”,但它没有到达“test2”。

I have tried every suggestion I could find including:

我尝试了我能找到的每个建议,包括:

try {

	InputStream inputStream = request.getInputStream(); 
	Reader inputStreamReader = new InputStreamReader(inputStream);
	String allChars = "";
	System.out.println("test");
	int data = inputStreamReader.read();
	while(data != -1){
		System.out.println("test2");
	    char theChar = (char) data;
	    System.out.println(theChar);
	    allChars = allChars + theChar;
	    data = inputStreamReader.read();
	}

	inputStreamReader.close();
	
	
	  
  }
 catch (IOException e) {
  System.out.println("No body data readable: " + e.getMessage() );
}

(still no "test2")...and:

仍然没有“test2”)...以及:

String requestBody = extractPostRequestBody(request);
System.out.println(requestBody);

private static String extractPostRequestBody(HttpServletRequest request) {
Scanner s = null;

try {
s = new Scanner(request.getInputStream(), "UTF-8").useDelimiter("\A");
} catch (IOException e) {
e.printStackTrace();
}
return s.hasNext() ? s.next() : "";
}

(nothing printed to console).

(没有打印到控制台)。

I have also tried using a REST client to post some multipart form data values to the URL but again nothing is returned. However if I try POSTing some json using application/json content-type and use the last method shown above I can see the json getting printed to the console.

我还尝试使用REST客户端将一些多部分表单数据值发布到URL,但仍然没有返回任何内容。然而,如果我尝试使用application/json内容类型发布一些JSON数据,并使用上面显示的最后一种方法,我可以看到JSON数据被打印到控制台。

Is there maybe some issue with using the extLib REST control and a java servicebean with multipart POSTs? I am aware that you can only read the request stream once so that previous use of eg request.properties will render the stream empty thereafter but this is not the case and presumably the json wouldn't display either if it were.

也许使用extLib REST控件和Java ServiceBean进行多部分POST存在一些问题吗?我知道你只能读取请求流一次,因此之前使用request.properties等将使流在此后为空,但这并非如此,而且如果是JSON数据,也不会显示。

英文:

I have an xpage based on a bootstrap framework which uses jQuery $post to submit forms contained within the page. These get posted using multipart/form-data content-type so I have no control over that. I have set up a REST.xsp containing a RESTService which calls a java ServiceBean. The latest method I have tried is as follows:

private void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException {
    // TODO Auto-generated method stub
	
	try{
	    ServletFileUpload upload = new ServletFileUpload();
	    FileItemIterator iterator = upload.getItemIterator(request);
	    System.out.println("test");
	    while (iterator.hasNext()) {
	    	 System.out.println("test2");
	        FileItemStream item = iterator.next();
	        InputStream stream = item.openStream();

	        if (item.isFormField()) {
	            byte[] str = new byte[stream.available()];
	            stream.read(str);
	            String pFieldValue = new String(str,"UTF8");
	            
	            
	            System.out.println("text Value : "+pFieldValue);
	            

	            if(item.getFieldName().equals("textBoxFormElement")){
	                System.out.println("text Value : "+pFieldValue);
	            }
	          }
	        }
	}catch (Exception e) {
	    e.printStackTrace();
	}
	
}

but I just can't seem to get the server to pick up the POSTed form data. I can see the "test" on the console but it's not hitting "test2".

I have tried every suggestion I could find including:

    try {
	     
	    	InputStream inputStream = request.getInputStream(); 
	    	Reader inputStreamReader = new InputStreamReader(inputStream);
	    	String allChars = "";
	    	System.out.println("test");
	    	int data = inputStreamReader.read();
	    	while(data != -1){
	    		System.out.println("test2");
	    	    char theChar = (char) data;
	    	    System.out.println(theChar);
	    	    allChars = allChars + theChar;
	    	    data = inputStreamReader.read();
	    	}

	    	inputStreamReader.close();
	    	
	    	
	    	  
	      }
	     catch (IOException e) {
	      System.out.println("No body data readable: " + e.getMessage() );
	    }

(still no "test2")...and:

   	String requestBody = extractPostRequestBody(request);
	System.out.println(requestBody);


  private static String extractPostRequestBody(HttpServletRequest request) {
    Scanner s = null;

    try {
        s = new Scanner(request.getInputStream(), "UTF-8").useDelimiter("\\A");
    } catch (IOException e) {
        e.printStackTrace();
    }
    return s.hasNext() ? s.next() : "";
}

(nothing printed to console).

I have also tried using a REST client to post some multipart form data values to the URL but again nothing is returned. However if I try POSTing some json using application/json content-type and use the last method shown above I can see the json getting printed to the console.

Is there maybe some issue with using the extLib REST control and a java servicebean with multipart POSTs? I am aware that you can only read the request stream once so that previous use of eg request.properties will render the stream empty thereafter but this is not the case and presumably the json wouldn't display either if it were.

Many thanks

答案1

得分: 2

如果使用POST请求发送的是multipart/formdata,您可以像这样获取提交的数据句柄:

for (Object e : req.getParameterMap().entrySet()) {

    Map.Entry entry = (Map.Entry) e;                
    System.out.println("获取到 " + entry.getKey() + "/" + entry.getValue().getClass().getCanonicalName());

}

地图中的条目可以是表单字段或已上传的文件。

英文:

If it's multipart/formdata that's send with the POST request to the ExtLib Rest control, you can get a handle to the submitted data like this:

for (Object e : req.getParameterMap().entrySet()) {
				
    Map.Entry entry = (Map.Entry) e;				
	System.out.println("got " + entry.getKey() + "/" + entry.getValue().getClass().getCanonicalName());
				
}

The entries in the map can either be the form fields or a file that's uploaded.

huangapple
  • 本文由 发表于 2020年8月13日 21:57:20
  • 转载请务必保留本文链接:https://go.coder-hub.com/63396823.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定