本文整理匯總了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);
}
}