使用springmvc框架
@Controller
public class Action{
/**
* 服务器端充当消费者的角色
*
* http://localhost:8080/SpringMVCDemo01/test2.action
*
* consumes="application/json":当前服务器端做为消费者,消费json格式的数据
* @return
*/
@RequestMapping(value="/test2",consumes="application/json")
//public String test2(HttpServletRequest request,HttpServletResponse response){
public void test2(HttpServletRequest request,HttpServletResponse response){
try {
//得到客户端发送过来的json字符串数据
InputStream inputStream=request.getInputStream();
//得到客户端发送过来数据的大小
int size=request.getContentLength();
System.out.println("size="+size);
byte[] data=new byte[size];
inputStream.read(data);
//将字节数组转换成字符串
String json=new String(data,request.getCharacterEncoding());
System.out.println("json="+json);
response.setCharacterEncoding("utf-8");
response.setContentType("text/plain;charset=utf-8");
PrintWriter out=response.getWriter();
out.println("访问服务器成功!");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("===ItemAction.test2(request="+request+")===");
//return "forward:/success.jsp";
}
/**
* 此时的服务器端充当生产者的角色
* http://localhost:8080/SpringMVCDemo01/test3.action
* @param response
* @return
*/
@RequestMapping(value="/test3",produces="application/json")
public void test3(HttpServletResponse response){
try {
response.setCharacterEncoding("utf-8");
//response.setContentType("application/json;charset=utf-8");
PrintWriter out=response.getWriter();
String jsonData="{\"userName\":\"小丽\",\"password\":\"123\"}";
out.println(jsonData);
System.out.println("===ItemAction.test3(HttpServletResponse response)==");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
@RequestMapping(value="/test7")
public void test7(InputStream inputStream,OutputStream outputStream){
//1.读取客户端发送过来的数据
byte[] buffer=new byte[1024];
try {
int len=inputStream.read(buffer);
String clientStr=new String(buffer,0,len);
System.out.println("clientStr="+clientStr);
//2.将服务器指定数据回传到客户端
outputStream.write("你好,我是test7".getBytes("utf-8"));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("==ItemAction.test7(InputStream inputStream="+inputStream+",OutputStream outputStream="+outputStream+")===");
}
}
=============
在另一个web项目中 用代码表示客户端和服务器
public class Client {
public static void main(String[] args) {
//String url="http://localhost:8080/SpringMVCDemo01/test2.action";
String url="http://localhost:8080/SpringMVCDemo01/test7.action";
try {
//1.得到请求对象
ClientHttpRequest request=new SimpleClientHttpRequestFactory().
createRequest(URI.create(url), HttpMethod.POST);
//2.设置请求头的内容类型和编码
request.getHeaders().set("Content-Type", "application/json;charset=utf-8");
//3.组拼发送到服务器端的json字符串
String jsonData="{\"userName\":\"小丽\",\"password\":\"123\"}";
//4.设置将客户端的数据发送到服务器端的数据
request.getBody().write(jsonData.getBytes("utf-8"));
//5.发送请求到服务器端并得到服务器返回的结果
ClientHttpResponse response=request.execute();
HttpStatus status=response.getStatusCode();
int statusCode=status.value();
System.out.println("statusCode="+statusCode);
if(statusCode==200){
//得到服务器端回传到客户端的数据
InputStream inputStream=response.getBody();
//得到服务器端回传到客户端数据的字节大小
int len=inputStream.available();
byte[] buffer=new byte[len];
inputStream.read(buffer);
String content=new String(buffer);
System.out.println("content="+content);
System.out.println("访问服务器成功并得到服务器返回的结果:"+response.getStatusText());
}else{
System.out.println("访问服务器失败并得到服务器返回的结果:"+response.getStatusText());
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
-----------
public class Server {
public static void main(String[] args) throws Exception {
String url="http://localhost:8080/SpringMVCDemo01/test3.action";
//1.实例化ClientHttpRequest对象
ClientHttpRequest request=new SimpleClientHttpRequestFactory().createRequest(new URI(url), HttpMethod.POST);
//2.设置客户端可以接收的媒体类型
request.getHeaders().set("Accept", "application/json");
//3.发送请求并得到响应结果
ClientHttpResponse response=request.execute();
//4.得到响应体的内容
InputStream is=response.getBody();
long len=response.getHeaders().getContentLength();
System.out.println("len="+len);
byte[] buffer=new byte[(int)len];
is.read(buffer);
//5.得到响应体的编码方式
//Charset charset=response.getHeaders().getContentType().getCharSet();
//String jsonData = new String(buffer,charset);
String jsonData = new String(buffer,"utf-8");
System.out.println("jsonData="+jsonData);
}
}