工作中多个项目之间数据传递
编程技术  /  houtizong 发布于 3年前   75
// 分页查询public List<Barcodegoods> queryBarcodegoodsByPage(String page,String pageSize) {Integer current;//当前页码Integer pageCount;//每页条数if (page != null && !"".equals(page)) {current = Integer.valueOf(page);} else {current = currentpage;}if (pageSize != null && !"".equals(pageSize)) {pageCount = Integer.valueOf(pageSize);} else {pageCount = pagesize;}StringBuffer buffer = new StringBuffer();buffer.append("select * from barcodegoods ");buffer.append("limit ");buffer.append((current - 1) * pageCount);buffer.append(",");buffer.append(pageCount);String sql = buffer.toString();List<Barcodegoods> bgs = new ArrayList<Barcodegoods>();try {conn = DriverManager.getConnection(URL, USERNAME, PASSWORD);pstmt = conn.prepareStatement(sql);res = pstmt.executeQuery();Barcodegoods bg = null;while (res.next()) {bg = new Barcodegoods();bg.setBarcode(res.getString("barcode"));bg.setColorid(res.getString("colorid"));bg.setGoods_no(res.getString("goods_no"));bg.setSize(res.getString("size"));bgs.add(bg);}} catch (SQLException e) {e.printStackTrace();}return bgs;}2)输出XML格式信息
public void createXml(List<Barcodegoods> list, PrintWriter out) {Document document = DocumentHelper.createDocument();Element stocks = document.addElement("stocks");// 创建根节点Barcodegoods bean = null;for (int i = 0; i < list.size(); i++) {bean = list.get(i);Element stock = stocks.addElement("stock");Element barcode = stock.addElement("barcode");Element goodsNo = stock.addElement("goods_no");Element colorid = stock.addElement("colorid");Element size = stock.addElement("size");barcode.setText(bean.getBarcode());goodsNo.setText(bean.getGoods_no());colorid.setText(bean.getColorid());size.setText(bean.getSize());}// 输出try {OutputFormat format = new OutputFormat(" ", true);format.setEncoding("gb2312");// 输出创建的xml文档XMLWriter output = new XMLWriter(out, format);output.write(document);out.flush();output.close();} catch (IOException e) {e.printStackTrace();}}利用了Dom4j当中的XMLWriter类包装PrintWriter进行输出
public void doPost(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {PrintWriter out = response.getWriter();//获取PrintWriterString current = "1";//当前页码String pageSize = "20";//每页总数String page = request.getParameter("page");if(page!=null){String parray[] = page.split(",");current = parray[0];pageSize = parray[1];}String checkCode = request.getParameter("checkcode");if("passing".equals(checkCode)){response.setContentType("text/xml;charset=gb2312");//内容类型和字符集response.setHeader("Cache-Control", "no-cache");//响应头XmlApi api = new XmlApi();List<Barcodegoods> bgs = api.queryBarcodegoods(current, pageSize);api.createXml(bgs, out);}else{out.print("check failed!!!");return;}out.flush();out.close();}可以命令请求时需要带参数,并且参数验证通过后才给与返回,不然就不接受请求!
<stocks><stock><barcode>FL009620010110</barcode><goods_no>FL00962</goods_no><colorid>0010</colorid><size>110</size></stock><stock><barcode>B622180202200</barcode><goods_no>B622180</goods_no><colorid>2022</colorid><size>00</size></stock><stock><barcode>B622180107100</barcode><goods_no>B622180</goods_no><colorid>1071</colorid><size>00</size></stock><stock><barcode>BB13150203900</barcode><goods_no>BB13150</goods_no><colorid>2039</colorid><size>00</size></stock><stock><barcode>BB13150222000</barcode><goods_no>BB13150</goods_no><colorid>2220</colorid><size>00</size></stock></stocks>结果正确可以正常显示库存信息
public static void main(String[] args) throws Exception {URL url = new URL("http://localhost:8083/webdemo/XmlApiServlet?checkcode=passing");URLConnection conn = url.openConnection();conn.setDoInput(true);//默认也是true。如果设置成false,这无法读取InputStreamInputStream in = conn.getInputStream();SAXReader reader = new SAXReader();List<List<String>> list = new ArrayList<List<String>>();List<String> list1 = null;Document document = reader.read(in);//通过IO流产生Document对象Element stocks = document.getRootElement();//获取根元素for (Iterator i = stocks.elementIterator(); i.hasNext();) {//遍历Stock节点list1 = new ArrayList();//一个Stcok中的所有属性Element stock = (Element) i.next();// 获得每个stockfor (Iterator j = stock.elementIterator(); j.hasNext();) {//遍历Stock节点下的属性节点Element node = (Element) j.next();// 获得每个stock中的属性list1.add(node.getText());// 把属性存放到List1中}list.add(list1);//存放所有的stock的信息}//存放数据List<Barcodegoods> bars = new ArrayList<Barcodegoods>();Barcodegoods goods = null;for(List<String> data : list){goods = new Barcodegoods();//new出barcodegoods对象,为它赋值goods.setBarcode(data.get(0));goods.setGoods_no(data.get(1));goods.setColorid(data.get(2));goods.setSize(data.get(3));bars.add(goods);}//输出产品的条形码for(Barcodegoods b :bars){System.out.println(b.getBarcode());}}通过获取连接中的InputStream,来解析XML。
//构造JSON字符串public String makeJsonString(List<Barcodegoods> list) {if (list != null && list.size() > 0) {List<JSONObject> objects = new ArrayList<JSONObject>();Map<String, JSONObject> omap = new HashMap<String, JSONObject>();Map<String,String> map = null;//JSON对象每个属性for(Barcodegoods bg : list){map = new HashMap<String, String>();map.put("barcode", bg.getBarcode());map.put("goodsno", bg.getGoods_no());map.put("colorid", bg.getColorid());map.put("size", bg.getSize());JSONObject jobj = JSONObject.fromObject(map);//将每个MAP转换成key-value的JSON对象objects.add(jobj);//存放所有的JSON对象}Map<String,Object> olists = new HashMap<String, Object>();//olists.put("barcodegoods", objects.toArray());//把Barcodegoods当做key,把objects当做valueolists.put("status", "success");//添加status属性JSONObject obj = JSONObject.fromObject(olists);//转化成JSON//return obj.toString();omap.put("message", obj);return omap.toString();}else{return "failed";}}JSON数据的构成可以依据需求进行构造,只要符合业务需求的数据构造就可以了
public void doPost(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {PrintWriter out = response.getWriter();String page = request.getParameter("page");String current = "1";// 当前页String pageSize = "10";// 每页总数if (page != null) {// 判断是否自定义分页String[] pages = page.split(",");if (pages.length == 2) {//两个参数缺一不可current = pages[0];pageSize = pages[1];}}String checkcode = request.getParameter("checkcode");// 验证参数if ("passing".equals(checkcode)) {// 判断验证参数JsonApi api = new JsonApi();List<Barcodegoods> bgs = api.queryBarcodegoods(current, pageSize);// 查询数据String jsonString = api.makeJsonString(bgs);// 构造JSON字符串out.print(jsonString);// 输出JSON字符串} else {out.print("check failed");}}可以命令请求时需要带参数,并且参数验证通过后才给与返回,不然就不接受请求!
{message={"barcodegoods":[{"goodsno":"FL00962","colorid":"0010","barcode":"FL009620010110","size":"110"},{"goodsno":"B622180","colorid":"2022","barcode":"B622180202200","size":"00"},{"goodsno":"B622180","colorid":"1071","barcode":"B622180107100","size":"00"},{"goodsno":"BB13150","colorid":"2039","barcode":"BB13150203900","size":"00"},{"goodsno":"BB13150","colorid":"2220","barcode":"BB13150222000","size":"00"}],"status":"success"}}3)JAVA解析输出的数据
public static void main(String[] args) throws Exception {URL url = new URL("http://localhost:8083/webdemo/JsonApiServlet?checkcode=passing");URLConnection conn = url.openConnection();conn.setDoInput(true);// 默认也是true。如果设置成false,这无法读取InputStreamInputStream in = conn.getInputStream();byte[] b = new byte[1024];int length = 0;StringBuffer buffer = new StringBuffer();while((length=in.read(b))>0){buffer.append(new String(b,0,length));//读取JSON字符串}JSONObject json = JSONObject.fromObject(buffer.toString());//转换为JSON对象JSONObject message = json.getJSONObject("message");//获取Message属性JSONArray barcodegoods = message.getJSONArray("barcodegoods");//获取barcodegoods集合属性List<Barcodegoods> bgs = new ArrayList<Barcodegoods>();Barcodegoods bg = null;for(int i=0;i<barcodegoods.size();i++){//遍历集合bg = new Barcodegoods();JSONObject goods = barcodegoods.getJSONObject(i);//遍历集合中的json对象bg.setBarcode(goods.getString("barcode"));//获取值,并且赋值bg.setColorid(goods.getString("colorid"));bg.setGoods_no(goods.getString("goodsno"));bg.setSize(goods.getString("size"));bgs.add(bg);}for(Barcodegoods bs : bgs){System.out.println(bs.getBarcode());}}这个是当时的做法,后来由于其他原因,就改成了WebService的方法进行传递
请勿发布不友善或者负能量的内容。与人为善,比聪明更重要!
技术博客集 - 网站简介:
前后端技术:
后端基于Hyperf2.1框架开发,前端使用Bootstrap可视化布局系统生成
网站主要作用:
1.编程技术分享及讨论交流,内置聊天系统;
2.测试交流框架问题,比如:Hyperf、Laravel、TP、beego;
3.本站数据是基于大数据采集等爬虫技术为基础助力分享知识,如有侵权请发邮件到站长邮箱,站长会尽快处理;
4.站长邮箱:[email protected];
文章归档
文章标签
友情链接