I am trying to read a stream request sent from another Servlet in Struts2
I am trying to write a servlet that will send a XML file (xml formatted string) to another servlet via a POST in STRUTS2.
servlet that will send a XML file:
String requestStr = "...........xml text........";
URLConnection co开发者_如何转开发n = new uRL("http://192.168.1.74/Project1/Request").openConnection();
con.setDoOutput(true);
OutputStream xmlResp = con.getOutputStream();
xmlResp.write(requestStr.getBytes("UTF-8"));
xmlResp.flush();
xmlResp.close();
servlet that will recive a XML file :
InputStream in=req.getInputStream();
StringBuffer xmlStr=new StringBuffer();
int d;
while((d=in.read()) != -1){
xmlStr.append((char)d);
}
System.out.println("xmlStr1--"+xmlStr.toString());
int iCont=req.getContentLength();
return xmlStr.toString();
in above case InputStream : in.read returns -1 but int iCont = req.getContentLength(); iCont returns value 1335....!
Above code worked fine when checked in non-struts Environment.....?
Solved :
If you are using inputStream in srvlet to read value stream, you are not suppose to use Request.getParameter()
.... before getting Stream value to InputStream through req.getInputStream()
...
Ex:
Correct-- method
InputStream in=req.getInputStream();
StringBuffer xmlStr=new StringBuffer();
int d;
while((d=in.read()) != -1){
xmlStr.append((char)d);
}
System.out.println("xmlStr1--"+xmlStr.toString());
Below method will cause ISSUE:
String str = req.getParameter("SOMETEXT");
InputStream in=req.getInputStream();
StringBuffer xmlStr=new StringBuffer();
int d;
while((d=in.read()) != -1){
xmlStr.append((char)d);
}
System.out.println("xmlStr1--"+xmlStr.toString());
精彩评论