자바 웹 http 전송 요청

자바 웹,request 의 요청 인 자 를 어떻게 가 져 옵 니까?
/***
	 * Get request query string
	 * @param request
	 * @return   byte[]
	 */
	public byte[] getRequestStr(HttpServletRequest request){
		int contentLength = request.getContentLength();
		byte buffer[] = new byte[contentLength];
		for (int i = 0; i < contentLength;) {
			try {

				int readlen = request.getInputStream().read(buffer, i,
						contentLength - i);
				if (readlen == -1) {
					break;
				}
				i += readlen;
			} catch (IOException ioexception) {
				ioexception.printStackTrace();
			} finally {
				// logger.info("Json Request:" + requestPacket);
			}
		}
		return buffer;
	}

상술 한 방법 은 byte 배열 을 되 돌려 줍 니 다.
다음 방법 은 문자열 을 직접 되 돌려 줍 니 다:
/***
	 * Get request query string
	 * 
	 * @param request
	 * @return
	 * @throws UnsupportedEncodingException
	 */
	public String getRequestStr(HttpServletRequest request) throws UnsupportedEncodingException{
		byte buffer[]=getRequestBytes(request);
		String charEncoding=request.getCharacterEncoding();
		if(charEncoding==null){
			charEncoding="UTF-8";
		}
		return new String(buffer,charEncoding);
	}

활용 단어 참조
:상기 방법 은 일반적으로 filter(javax.servlet.Filter)에서 요청 파 라 메 터 를 가 져 와 전송 하 는 데 사 용 됩 니 다.
자바 웹 에서 response 응답 체(응답 체)를 다시 쓰 고 응답 체 에 지정 한 내용 을 어떻게 기록 합 니까?
/***
	 * Send http request
	 * 
	 * @param response
	 * @param bytes  :    
	 * @param contentType  :if is null,default value is  "application/json"
	 * @param encoding   :      
	 * @throws IOException
	 */
	public static void sendRequestWriter(HttpServletResponse response, byte[] bytes,
			String contentType,String encoding) throws IOException {
		response.setContentLength(bytes.length);
		if (contentType == null) {
			contentType = "application/json";
		}
		response.setContentType(contentType);

		PrintWriter printer = response.getWriter();
		printer.println(new String(bytes,encoding));
		printer.flush();
		printer.close();
	}
	
	/***
	 * 
	 * @param response
	 * @param sendData   :<code>String</code>
	 * @param contentType
	 * @param encoding : such as GBK/utf-8
	 * @throws IOException
	 */
	public static void sendRequestWriter(HttpServletResponse response, String sendData,
			String contentType,String encoding) throws IOException {
//		response.setContentLength(sendData.getBytes(encoding).length);
		byte[]bytes=sendData.getBytes(encoding);
		sendRequestWriter(response, bytes, contentType, encoding);
	}

 이상 의 방법 은 모두 PrintWriter 를 사용 하여 response 를 기록 합 니 다.
다음 방식 은 스 트림 으로 response 를 기록 하 는 것 입 니 다.
/***
	 * test ok
	 * @param response
	 * @param bytes
	 * @param contentType
	 * @param encoding
	 * @throws IOException
	 */
	public static void sendRequestStream(HttpServletResponse response, byte[] bytes,
			String contentType) throws IOException {
		response.setContentLength(bytes.length);
		if (contentType == null) {
			contentType = "application/json";
		}
		response.setContentType(contentType);

		ServletOutputStream sos = response.getOutputStream();
		sos.write(bytes, 0, bytes.length);
		sos.flush();
		sos.close();
	}

 응용:게 이 트 웨 이 에서 전송 요청 과 응답 을 하 는 데 사용 합 니 다.

좋은 웹페이지 즐겨찾기