1.下载需要的客户端、服务端Jar,客户端jar包链接:  https://github.com/zhangliqingyun/jarlist/tree/master/webserviceclient    服务端jar包链接:https://github.com/zhangliqingyun/jarlist/tree/master/webserviceservice

2.配置web service服务,实现客户端与服务端通信

(1)web.xml内容

<display-name>xxfw</display-name>

  <servlet>

    <servlet-name>Way REST Service</servlet-name>

    <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>

    <init-param>  <!—初始化服务à

      <param-name>jersey.config.server.provider.packages</param-name>

      <param-value>com.rdtsw.service</param-value>

    </init-param>

    <load-on-startup>1</load-on-startup>

  </servlet>

  <servlet>

    <description></description>

    <servlet-name>MsgSevlet</servlet-name>

    <servlet-class>com.rdtsw.action.MsgSevlet</servlet-class>

  </servlet>

  

  <servlet-mapping>

    <servlet-name>Way REST Service</servlet-name>

    <url-pattern>/ws/*</url-pattern>

  </servlet-mapping>

  <servlet-mapping>

    <servlet-name>MsgSevlet</servlet-name>

    <url-pattern>/MsgSevlet</url-pattern>

  </servlet-mapping>

(2)服务类

@Path("/msg")

public class MsgService

(3)服务器端接收文件代码

//文件接收

  @POST

  @Path("/RecXMLData/{fileName}")

  @Produces({"application/json;charset=utf-8","text/plain"})   

  public String receiveFile(@PathParam("fileName") String fileName, @Context HttpServletRequest request, @Context HttpServletResponse response) {

  try {   

      FileOutputStream fos = new FileOutputStream(new File(path + fileName));

      byte[] buffer = new byte[8096];



      InputStream is = request.getInputStream();

      int len;

      while ((len = is.read(buffer)) != -1)

      {

        fos.write(buffer, 0, len);

      }

      fos.flush();

      fos.close();

      return "ok";

    } catch (Exception e) {

      return "no"; }



  }

(4)服务器端发送文件

//文件发送

  @POST

  @Path("/SendXmlData/{fileName}")

  @Produces({"application/json;charset=utf-8","text/plain"})  

  public String sendFile(@PathParam("fileName") String fileName, @Context HttpServletRequest request, @Context HttpServletResponse response) {

      

 try {

      File testFile = new File(path + fileName);

      FileInputStream fi = new FileInputStream(testFile);

      byte[] buf = new byte[2048];



      DataOutputStream dos = new DataOutputStream(response.getOutputStream());

      dos.writeUTF(testFile.getName());

      int len1;

      while ((len1 = fi.read(buf)) != -1)

      {

        dos.write(buf, 0, len1);

      }

      dos.flush();

      dos.close();

      fi.close();

      return "200";

    } catch (Exception e) {

      return "404"; }   

  }

(5)客户端发送文件

public static void send(String fileName,String deskUrl){



//String filename = "bbb.xml";

HttpClient ht = new DefaultHttpClient();

HttpPost post = new HttpPost("http://192.168.0.123:9080/xxfw/ws/msg/SendXmlData/"+fileName);

HttpResponse rs = null;

try {

rs = ht.execute(post);

DataInputStream is  = new DataInputStream(rs.getEntity().getContent());

byte[] buffer = new byte[2048];

int len2;

//读出文件名

String filename = is.readUTF();

FileOutputStream fos = new FileOutputStream(new File(deskUrl+filename));  

while((len2=is.read(buffer))!=-1){

fos.write(buffer, 0, len2);

}

fos.flush();

fos.close();

is.close();

} catch (Exception e) {

e.printStackTrace();

}finally{

System.out.println("文件发送成功");

post.abort();

}



}

(6)客户端接收文件

public static void receive(String fileName,String deskUrl) throws JSONException, URISyntaxException, FileNotFoundException{

//String filename = "bbb.xml";

Client c = Client.create();  

URI u =new URI("http://192.168.0.123:9080/xxfw/ws/msg/RecXMLData/"+fileName);

WebResource resource = c.resource(u);



MultivaluedMapImpl params = new MultivaluedMapImpl();



File testFile = new File(deskUrl+fileName);



FileInputStream fis = new FileInputStream(testFile);



String aaa = resource.entity(fis).post(String.class);



System.out.println("result:"+aaa);

}

(7)客户传递参数

public static void regist() throws URISyntaxException{

Client client = Client.create();  

    URI u = new URI(getUrl("RegJY4GSvr"));      

    WebResource resource = client.resource(u);    

    MultivaluedMapImpl params = new MultivaluedMapImpl();     //传递多个参数的方法   

    params.add("xh", "炫号");  

    params.add("zclx", "注册类型");  

    params.add("imei", "imei");  

    String result = resource.queryParams(params).get(String.class);   

    System.out.println(result);   



}

(8)服务端接收参数  

//注册服务

  @GET  

  @Path("RegJY4GSvr")

  @Produces({"text/plain"})   

  public String regist(@QueryParam("xh") String xh,@QueryParam("zclx") String zclx,@QueryParam("imei") String imei,@Context HttpServletRequest request,@Context HttpServletResponse response){

  

  java.util.Date now = new java.util.Date();

  String registDate = format.format(now);  //注册日全部

  String zcdwdm = "000001";  //注册单位代码

  map.put("xh", xh);

  map.put("zclx", zclx);

  map.put("imei", imei);

  map.put("zcrq", registDate);    

  map.put("zcdwdm", zcdwdm);

  try {

xxfwDao.insertRegist(map);

} catch (Exception e) {

e.printStackTrace();

}  

    return "注册成功";  

  }

(9)客户端接收服务端反回的list集合

public static void init() throws URISyntaxException{



  ClientConfig clientConfig = new DefaultClientConfig();

  clientConfig.getFeatures().put(JSONConfiguration.FEATURE_POJO_MAPPING, Boolean.TRUE);

  Client client = Client.create(clientConfig);

  MultivaluedMapImpl params = new MultivaluedMapImpl();

  String zcdwdm ="000001";

  String xxlx = "xxlx";

  params.add("zcdwdm", zcdwdm);

  params.add("xxlx", xxlx);

  

  WebResource resource = client.resource(getUrl("InitJY4GSvr"));    //得到注册服务初始化的url

  GenericType<List<Demo>> hsp = new GenericType<List<Demo>>(){};    //返回结果集合类型

  List<Demo> dwdmList=  resource.queryParams(params).get(hsp);      //单位代码和油料代码的集合

  System.out.println(dwdmList.get(0).getYldmList().get(0).getYldm());  

(10)服务端返回list集合  

//得到单位代码

  @GET

  @Path("InitJY4GSvr")

  @Produces({ MediaType.APPLICATION_JSON,MediaType.APPLICATION_XML})

  public Response InitJY4GSvr(@QueryParam("zcdwdm") String zcdwdm,@QueryParam("xxlx") String xxlx, @Context HttpServletRequest request, @Context HttpServletResponse response){

  map.put("zcdwdm", zcdwdm);

  map.put("xxlx", xxlx);

  List<Demo> list = new ArrayList<Demo>();

  try {

  List<Dwdm> dwdmList = xxfwDao.getDwdmList(map);                     //得到单位代码集合

  List<Yldm> yldmList = xxfwDao.getYldmList(map);                     //得到油料代码集合

  Demo demo = new Demo();

  demo.setDwdmList(dwdmList);

  demo.setYldmList(yldmList);

  list.add(demo);

  GenericEntity entity = new GenericEntity<List<Demo>>(list) {};  //返回结果类型

      return Response.ok(entity).build();

} catch (Exception e) {

  e.printStackTrace();

}

  return null;

  }

 

 

 

 

Logo

旨在为数千万中国开发者提供一个无缝且高效的云端环境,以支持学习、使用和贡献开源项目。

更多推荐