摘要:从request中获取json数据的方法从request中获取json数据的方法

小宝典致力于为广大程序猿(媛)提供高品质的代码服务,请大家多多光顾小站,小宝典在此谢过。

从request中获取json数据的方法

 package com.newpp.core.interceptor;  import java.io.BufferedReader; import java.io.ByteArrayInputStream; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader;  import javax.servlet.ServletInputStream; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequestWrapper;  /**  * MyRequestWrapper.  *   * @author iechenyb  */ public class MyRequestWrapper extends HttpServletRequestWrapper {     private final String body;      public MyRequestWrapper(HttpServletRequest request) throws IOException {         super(request);         StringBuilder stringBuilder = new StringBuilder();         BufferedReader bufferedReader = null;         try {             InputStream inputStream = request.getInputStream();             if (inputStream != null) {                 bufferedReader = new BufferedReader(new InputStreamReader(inputStream));                 char[] charBuffer = new char[128];                 int bytesRead = -1;                 while ((bytesRead = bufferedReader.read(charBuffer)) > 0) {                     stringBuilder.append(charBuffer, 0, bytesRead);                 }             } else {                 stringBuilder.append("");             }         } catch (IOException ex) {             throw ex;         } finally {             if (bufferedReader != null) {                 try {                     bufferedReader.close();                 } catch (IOException ex) {                     throw ex;                 }             }         }         body = stringBuilder.toString();     }      @Override     public ServletInputStream getInputStream() throws IOException {         final ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(body.getBytes());         ServletInputStream servletInputStream = new ServletInputStream() {             public int read() throws IOException {                 return byteArrayInputStream.read();             }         };         return servletInputStream;     }      @Override     public BufferedReader getReader() throws IOException {         return new BufferedReader(new InputStreamReader(this.getInputStream()));     }      public String getBody() {         return this.body;     } }

从request中获取json数据的方法

 package com.newpp.core.interceptor;  import java.io.BufferedReader; import java.io.ByteArrayInputStream; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader;  import javax.servlet.ServletInputStream; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequestWrapper;  /**  * MyRequestWrapper.  *   * @author iechenyb  */ public class MyRequestWrapper extends HttpServletRequestWrapper {     private final String body;      public MyRequestWrapper(HttpServletRequest request) throws IOException {         super(request);         StringBuilder stringBuilder = new StringBuilder();         BufferedReader bufferedReader = null;         try {             InputStream inputStream = request.getInputStream();             if (inputStream != null) {                 bufferedReader = new BufferedReader(new InputStreamReader(inputStream));                 char[] charBuffer = new char[128];                 int bytesRead = -1;                 while ((bytesRead = bufferedReader.read(charBuffer)) > 0) {                     stringBuilder.append(charBuffer, 0, bytesRead);                 }             } else {                 stringBuilder.append("");             }         } catch (IOException ex) {             throw ex;         } finally {             if (bufferedReader != null) {                 try {                     bufferedReader.close();                 } catch (IOException ex) {                     throw ex;                 }             }         }         body = stringBuilder.toString();     }      @Override     public ServletInputStream getInputStream() throws IOException {         final ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(body.getBytes());         ServletInputStream servletInputStream = new ServletInputStream() {             public int read() throws IOException {                 return byteArrayInputStream.read();             }         };         return servletInputStream;     }      @Override     public BufferedReader getReader() throws IOException {         return new BufferedReader(new InputStreamReader(this.getInputStream()));     }      public String getBody() {         return this.body;     } }

觉得可用,就经常来吧!Javascript技巧 脚本宝典 欢迎评论哦! js技巧,巧夺天工,精雕玉琢。小宝典献丑了!

本文固定链接: http://www.js-code.com/js/js_1333.html