本文整理汇总了Java中javax.servlet.http.HttpServletRequest.getInputStream方法的典型用法代码示例。如果您正苦于以下问题:Java HttpServletRequest.getInputStream方法的具体用法?Java HttpServletRequest.getInputStream怎么用?Java HttpServletRequest.getInputStream使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类javax.servlet.http.HttpServletRequest
的用法示例。
在下文中一共展示了HttpServletRequest.getInputStream方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: parseXml
import javax.servlet.http.HttpServletRequest; //导入方法依赖的package包/类
/**
* @param @param request
* @param @throws Exception
* @Description: 解析微信发来的请求(XML)
*/
@SuppressWarnings("unchecked")
public static Map<String, String> parseXml(HttpServletRequest request) throws Exception {
// 将解析结果存储在 HashMap 中
Map<String, String> map = new HashMap<String, String>();
// 从 request 中取得输入流
InputStream inputStream = request.getInputStream();
// 读取输入流
SAXReader reader = new SAXReader();
Document document = reader.read(inputStream);
// 得到 xml 根元素
Element root = document.getRootElement();
// 得到根元素的所有子节点
List<Element> elementList = root.elements();
// 遍历所有子节点
for (Element e : elementList) {
map.put(e.getName(), e.getText());
}
// 释放资源
inputStream.close();
inputStream = null;
return map;
}
示例2: notify_wechat_pay
import javax.servlet.http.HttpServletRequest; //导入方法依赖的package包/类
/***
* 需提供外部访问地址
* 此处可以是SpringMVC、Struts2、Servlet 请根据项目前端框架编写如下代码.
* 调用微信统一下单时,传入 UnifiedOrderInput.notify_url,应为该放方法的访问路径
*/
public void notify_wechat_pay(HttpServletRequest request, HttpServletResponse response){
String status="SUCCESS",msg = "处理成功";
PrintWriter writer = null;
try {
writer = response.getWriter();
StringBuffer xmlResult = new StringBuffer();
InputStream is = request.getInputStream();
BufferedReader br = new BufferedReader(new InputStreamReader(is, "UTF-8"));
String buffer = null;
while ((buffer = br.readLine()) != null){
xmlResult.append(buffer);
}
log.info("微信异步返回信息:"+ ThlwsBeanUtil.formatXml(xmlResult.toString()));
NotifyOutput notifyOutput = WechatUtil.parseNotifyMsg(xmlResult.toString());
//notifyOutput 是微信推送数据转换为Java对象,直接从该对象取值并进行相关业务操作
//TODO 业务逻辑
} catch (IOException e) {
e.printStackTrace();
}finally {
writer.println("<xml><return_code><![CDATA["+status+"]]></return_code><return_msg><![CDATA["+msg+"]]></return_msg></xml>");
}
}
示例3: getValue
import javax.servlet.http.HttpServletRequest; //导入方法依赖的package包/类
@Override
public Object getValue(HttpServletRequest request) throws Exception {
Object body = request.getAttribute(RestConst.BODY_PARAMETER);
if (body != null) {
return convertValue(body, targetType);
}
// for standard HttpServletRequest, getInputStream will never return null
// but for mocked HttpServletRequest, maybe get a null
// like org.apache.servicecomb.provider.springmvc.reference.ClientToHttpServletRequest
InputStream inputStream = request.getInputStream();
if (inputStream == null) {
return null;
}
String contentType = request.getContentType();
if (contentType != null && !contentType.toLowerCase(Locale.US).startsWith(MediaType.APPLICATION_JSON)) {
// TODO: we should consider body encoding
return IOUtils.toString(inputStream, "UTF-8");
}
return RestObjectMapper.INSTANCE.readValue(inputStream, targetType);
}
示例4: putObject
import javax.servlet.http.HttpServletRequest; //导入方法依赖的package包/类
/**
* Adds an object to a bucket.
*
* http://docs.aws.amazon.com/AmazonS3/latest/API/RESTObjectPUT.html
*
* @param bucketName the Bucket in which to store the file in.
* @param request http servlet request
* @return ResponseEntity with Status Code and ETag
*/
@RequestMapping(value = "/{bucketName:.+}/**", method = RequestMethod.PUT)
public ResponseEntity<String> putObject(@PathVariable final String bucketName,
final HttpServletRequest request) {
final String filename = filenameFrom(bucketName, request);
try (ServletInputStream inputStream = request.getInputStream()) {
final S3Object s3Object = fileStore.putS3Object(bucketName,
filename,
request.getContentType(),
inputStream,
isV4SigningEnabled(request));
final HttpHeaders responseHeaders = new HttpHeaders();
responseHeaders.setETag("\"" + s3Object.getMd5() + "\"");
responseHeaders.setLastModified(s3Object.getLastModified());
return new ResponseEntity<>(responseHeaders, HttpStatus.CREATED);
} catch (final IOException e) {
LOG.error("Object could not be saved!", e);
return new ResponseEntity<>(e.getMessage(), HttpStatus.INTERNAL_SERVER_ERROR);
}
}
示例5: doPost
import javax.servlet.http.HttpServletRequest; //导入方法依赖的package包/类
/**
* Returns a resource object as a result of a get request.
**/
protected void doPost( HttpServletRequest req, HttpServletResponse resp ) throws ServletException, IOException {
int length = req.getIntHeader( "Content-length" );
String contentType = req.getHeader( "Content-type" );
resp.setContentType( contentType );
InputStreamReader isr = new InputStreamReader( req.getInputStream() );
BufferedReader br = new BufferedReader( isr );
resp.getWriter().print( length );
String line = br.readLine();
while (line != null) {
resp.getWriter().print( "\n" );
resp.getWriter().print( line );
line = br.readLine();
}
resp.getWriter().flush();
resp.getWriter().close();
}
示例6: doPost
import javax.servlet.http.HttpServletRequest; //导入方法依赖的package包/类
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
// Swallow any request body
int read = 0;
int len = 0;
byte[] buffer = new byte[4096];
InputStream is = req.getInputStream();
boolean contentOK = true;
while (len > -1) {
len = is.read(buffer);
read = read + len;
for (int i=0; i<len && contentOK; i++) {
contentOK = (buffer[i] == DATA);
}
}
// len will have been -1 on last iteration
read++;
// Report the number of bytes read
resp.setContentType("text/plain");
if (contentOK)
resp.getWriter().print("OK-" + read);
else
resp.getWriter().print("CONTENT-MISMATCH-" + read);
}
示例7: read
import javax.servlet.http.HttpServletRequest; //导入方法依赖的package包/类
protected void read(CometEvent event, HttpServletRequest request, HttpServletResponse response)
throws IOException {
InputStream is = request.getInputStream();
byte[] buf = new byte[512];
while (is.available() > 0) {
log("Available: " + is.available());
int n = is.read(buf);
if (n > 0) {
log("Read " + n + " bytes: " + new String(buf, 0, n)
+ " for session: " + request.getSession(true).getId());
} else if (n < 0) {
log("End of file: " + n);
end(event, request, response);
return;
}
}
}
示例8: readInternal
import javax.servlet.http.HttpServletRequest; //导入方法依赖的package包/类
@Override
protected Object readInternal(Class<? extends Object> clazz, HttpServletRequest request)
throws IOException, HttpMessageNotReadableException {
InputStream in = request.getInputStream();
return gson.fromJson(new InputStreamReader(in), clazz);
}
示例9: BufferedRequestWrapper
import javax.servlet.http.HttpServletRequest; //导入方法依赖的package包/类
public BufferedRequestWrapper(HttpServletRequest req) throws IOException {
super(req);
// Read InputStream and store its content in a buffer.
InputStream is = req.getInputStream();
this.baos = new ByteArrayOutputStream();
byte buf[] = new byte[1024];
int letti;
while ((letti = is.read(buf)) > 0) {
this.baos.write(buf, 0, letti);
}
this.buffer = this.baos.toByteArray();
}
示例10: getBody
import javax.servlet.http.HttpServletRequest; //导入方法依赖的package包/类
private GerritProjectEvent getBody(HttpServletRequest req) throws IOException {
char[] body = new char[req.getContentLength()];
try (InputStreamReader is = new InputStreamReader(req.getInputStream())) {
IOUtils.readFully(is, body);
String bodyString = new String(body);
log.info("Received body: " + bodyString);
return gson.fromJson(bodyString, GerritProjectEvent.class);
}
}
示例11: determinePSPParams
import javax.servlet.http.HttpServletRequest; //导入方法依赖的package包/类
/**
* Converts the input given in the request to a properties object.
*
* @param request
* The received request.
* @return The properties contained in the request.
* @throws IOException
* Thrown in case the request information could not be
* evaluated.
*/
private boolean determinePSPParams(HttpServletRequest request, Properties p) {
try {
ServletInputStream inputStream = request.getInputStream();
if (inputStream == null) {
return false;
}
BufferedReader br = new BufferedReader(new InputStreamReader(
inputStream, "UTF-8"));
String line = br.readLine();
StringBuffer sb = new StringBuffer();
while (line != null) {
sb.append(line);
line = br.readLine();
}
String params = sb.toString();
StringTokenizer st = new StringTokenizer(params, "&");
while (st.hasMoreTokens()) {
String nextToken = st.nextToken();
String[] splitResult = nextToken.split("=");
String key = splitResult[0];
String value = "";
if (splitResult.length > 1) {
value = URLDecoder.decode(splitResult[1], "UTF-8");
}
p.setProperty(key, value);
}
return validateResponse(p);
} catch (IOException e) {
// if the request information cannot be read, we cannot determine
// whether the registration worked or not. Hence we assume it
// failed, log a warning and return the failure-URL to the PSP.
logger.logWarn(Log4jLogger.SYSTEM_LOG, e,
LogMessageIdentifier.WARN_HEIDELPAY_INPUT_PROCESS_FAILED);
}
return false;
}
示例12: handleRequest
import javax.servlet.http.HttpServletRequest; //导入方法依赖的package包/类
@Override
public void handleRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
if (!"POST".equals(request.getMethod())) {
throw new HttpRequestMethodNotSupportedException(request.getMethod(), new String[]{"POST"}, "ThriftServiceExporter only supports POST requests");
}
InputStream in = request.getInputStream();
OutputStream out = response.getOutputStream();
try {
ThriftContextHolder.init();
response.setContentType("application/x-thrift");
TTransport transport = new TIOStreamTransport(in, out);
TProtocol protocol = getProtocolFactory().getProtocol(transport);
TProcessor processor = ThriftUtil.buildProcessor(getServiceInterface(), getProxyForService());
processor.process(protocol, protocol);
} catch (Throwable e) {
response.setContentType("text/plain; charset=UTF-8");
response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
e.printStackTrace(new PrintWriter(out, true));
if (LOGGER.isErrorEnabled()) {
LOGGER.error("Thrift server direct error", e);
}
} finally {
ThriftContextHolder.reset();
}
}
示例13: getRequestOriginalXml
import javax.servlet.http.HttpServletRequest; //导入方法依赖的package包/类
/**
* 读取post请求原始xml内容
*
* @param request
* @return
* @throws IOException
*/
private String getRequestOriginalXml(HttpServletRequest request) throws IOException {
BufferedReader in = new BufferedReader(
new InputStreamReader(request.getInputStream(), Charset.forName("UTF-8")));
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = in.readLine()) != null) {
sb.append(line);
}
return sb.toString();
}
示例14: putMethod
import javax.servlet.http.HttpServletRequest; //导入方法依赖的package包/类
/**
* Handles the PUT method, which uploads a file to the server.
*
* @param req HTTP Request from client
* @param resp HTTP Response to client
*/
public void putMethod(HttpServletRequest req, HttpServletResponse resp, StagingFile staging, String filename)
throws IOException
{
try( InputStream in = req.getInputStream() )
{
fileSystemService.write(staging, filename, in, false);
resp.setStatus(HttpServletResponse.SC_CREATED);
}
}
示例15: readJavaType
import javax.servlet.http.HttpServletRequest; //导入方法依赖的package包/类
private Object readJavaType(JavaType javaType, HttpServletRequest request) {
try {
InputStream in = request.getInputStream();
return this.objectMapper.readValue(in, javaType);
} catch (IOException ex) {
throw new HttpMessageNotReadableException("Could not read JSON: " + ex.getMessage(), ex);
}
}