初学者,请多见谅。我要用Post方式提交一些数据,这些数据要封装到流中,请问这个流要满足什么样的格式才能在servlet中的doPost方法中读取到。以下是java代码 InputStream is= request.getInputStream();
InputStreamReader isr = new InputStreamReader(is, "utf-8");
BufferedReader bf = new BufferedReader(isr);
// 可变字符串
StringBuffer sb = new StringBuffer();
// 存储一行数据(br.readLine每次读写一行)
String readLine=null;  
while((readLine=bf.readLine())!=null){  
sb.append(readLine).append("\n");  

System.out.println(sb.toString());

解决方案 »

  1.   

    不需要满足什么格式
    你表单字段的键和值会按照
    key1=value1&key2=value2&key3=value3的形式打印出来
      

  2.   


    我没有表单的,从客户端发上来的数据流,从indy组件中看到的是要在流里加上一些header之类的字符串,这样就能收到。multipart/form-data; boundary=
    Content-Type:
    Content-Transfer-Encoding:
    就这些东西,是不是一定要有?
      

  3.   

    不用表单就用HttpURLConnection//这里面两个参数
    //url 服务器端servlet的访问地址
    //aOutputData 字节数组 内容为key1=value1&key2=value2&key3=value3的形式 ,然后getBytes();
    HttpURLConnection http=url.openConnection();
    http.setConnectTimeout(5000);
    http.setReadTimeout(5000);
    http.setDoOutput(true);
    http.setDoInput(true);
    http.setAllowUserInteraction(false);
    http.setUseCaches(false);
    http.setRequestMethod("POST");
    http.setRequestProperty("Content-Type",
    "application/x-www-form-urlencoded");
    http.setRequestProperty("Content-Length", String
    .valueOf(aOutputData.length));
    http.setRequestProperty("Connection", "close");
    http
    .setRequestProperty(
    "User-Agent",
    "*** client");
    http.setRequestProperty("Accept", "text/xml");
    http.connect();
    OutputStream out = http.getOutputStream();
    out.write(aOutputData);
    out.flush();