+
 新版

HttpServletRequest获取流无数据

Tint 发布于 2015/04/07 10:25
阅读 2K+
收藏 0

使用getInputStream和getReader均无数据,

请求url为127.0.0.1:8080/test.htm?test=1

ServletInputStream input = request.getInputStream();

   int length = 0;  
   int BUFFER_SIZE = 512;
   byte[] buffer = new byte[BUFFER_SIZE];  
   ByteArrayOutputStream baos = new ByteArrayOutputStream(); 
   do {  
       length = input.read(buffer);  
       if (length > 0) {  
           baos.write(buffer, 0, length);  
       }  
   } while (length * 2 == BUFFER_SIZE);  
 

   String bodyData = new String(baos.toByteArray(), "UTF-8");  


BufferedReader reader = null;        
StringBuffer     msg  = null;
        int           perchar = 0;
        try {
            reader = request.getReader();
            msg    = new StringBuffer();
            while ((perchar = reader.read()) != -1) {
                msg.append((char)perchar);
            }
        } catch (IOException e) {
        log.error("读取失败",e);
        } finally {
            try {
                if (reader != null) {
                    reader.close();
                }                
            } catch (IOException e1) {
            log.error("流关闭失败",e1);
            }
        }        

加载中
0
Tint
Tint
使用HttpServer HttpHandler却可以得到
0
jack984
jack984
这代码看起来太长了,java真恶心.用php的$_GET,$_POST,$_REQUEST实现多简单.
0
Jack_Q
Jack_Q
你是form提交吗,如果是form post提交,是否没设置
enctype="multipart/form-data"
Tint
Tint
不能url拼接吗?
OSCHINA
登录后可查看更多优质内容
返回顶部
顶部