Servlet个人学习笔记

附件中是以前的servlet学习笔记,下面来补充学习一个servlet中request.getInputStream()的个人认识(可能不对,还请指教)

@RequestMapping(value = "message", method = RequestMethod.POST)
    public void request(HttpServletRequest request, HttpServletResponse response) {

        try {
            // 接收密文
            InputStream is = request.getInputStream();
            BufferedReader br = new BufferedReader(new InputStreamReader(is, "utf-8"));
            String line = null;
            StringBuilder buff = new StringBuilder();
            while (null != (line = br.readLine())) {
                buff.append(line);
            }
            is.close();
            br.close();

            String ciphertext = buff.toString();
            if (null == ciphertext || ciphertext.equals("")) {
                throw new Exception("-0002");
            }
            // 解密密文
            //MessageModel msgModel = ClienUtils.decoder(ciphertext, PlatformKeys.platformPriKey);

//此段简单描述输入流
InputStream is = request.getInputStream();
InputStreamReader ir = new InputStreamReader(is,"utf-8");
BufferReader br = new BufferReader(ir);
String str = null;
while(null !=(str = br.readLine()))
 {
   System.out.println(str);
 }
-------------------------------------------------------------------------------------------------------------------
//此段简单描述OutputStream输出流
OutputStream ou = response.getOutputStream();
OutputStreamWriter ow = new OutputStreamWriter(ou,"utf-8");
ow.write(str)

还有一种输出流就是PrintWriter(直接向response管道写数据,通常用于ajax异步请求这里就不说它了)

 

本文出自 “重新来学JAVA” 博客,转载请与作者联系!

你可能感兴趣的:(request,servlet,Inputstream)