package com.lovenav.controller; import com.alibaba.fastjson2.JSON; import com.alibaba.fastjson2.JSONArray; import com.alibaba.fastjson2.JSONObject; import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.node.ObjectNode; import com.lovenav.entity.UrlCateList; import com.lovenav.entity.UrlList; import com.lovenav.service.UrlCateListService; import com.lovenav.service.UrlListService; import com.lovenav.vo.CateAndUrl; import org.apache.poi.hssf.usermodel.HSSFCell; import org.apache.poi.hssf.usermodel.HSSFRow; import org.apache.poi.hssf.usermodel.HSSFSheet; import org.apache.poi.hssf.usermodel.HSSFWorkbook; import org.jsoup.Connection; import org.jsoup.Jsoup; import org.jsoup.nodes.Document; import org.jsoup.select.Elements; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; import org.springframework.web.bind.annotation.RestController; import javax.servlet.http.HttpServletResponse; import java.io.IOException; import java.net.MalformedURLException; import java.net.URISyntaxException; import java.net.URL; import java.util.*; import java.io.*; import java.util.regex.Matcher; import java.util.regex.Pattern; @RestController @RequestMapping("/UrlAndCate") public class UrlAndCateController { @Autowired ObjectMapper objectMapper; @Autowired UrlCateListService urlCateListService; @Autowired UrlListService urlListService; //处理JSON @RequestMapping("/disposeJson") public String disposeJson(@RequestBody String data2 ,String email) { String jsonString; //先转换成ObjectMapper类型 ObjectNode objectNode = objectMapper.createObjectNode(); try { JsonNode rootNode = objectMapper.readTree(data2); disposeBookmarkFunction1(rootNode,"top",email); countCateContainUrlNumber(email); HashMap result = new HashMap<>(); result.put("code", 200); result.put("msg", "查询成功"); jsonString = JSONObject.toJSONString(result); } catch (JsonProcessingException e) { throw new RuntimeException(e); } return jsonString; } public void disposeBookmarkFunction1(JsonNode rootNode,String parent,String email) { for(int i=0;i urlCateLists = urlCateListService.selectUrListByUserId(Integer.valueOf(userId)); List urlLists = urlListService.selectUrList(); List cateAndUrlList = new ArrayList<>(); //预处理为CateAndUrl对象 for( int i = 0 ; i < urlCateLists.size() ; i++) { CateAndUrl cateAndUrl = new CateAndUrl(); cateAndUrl.setFloder("true"); cateAndUrl.setName(urlCateLists.get(i).getName()); cateAndUrl.setParentId(urlCateLists.get(i).getRootCateId()); cateAndUrl.setCateId(urlCateLists.get(i).getId()); cateAndUrlList.add(cateAndUrl); } for( int i = 0 ; i < urlLists.size() ; i++) { CateAndUrl cateAndUrl = new CateAndUrl(); cateAndUrl.setFloder("false"); cateAndUrl.setName(urlLists.get(i).getName()); cateAndUrl.setParentId(urlLists.get(i).getCateId()); cateAndUrl.setUrlId(urlLists.get(i).getId()); cateAndUrlList.add(cateAndUrl); } List parentsList = new ArrayList<>(); //声明返回集合 List resultList = new ArrayList<>(); //对全部数据进行遍历 for (CateAndUrl disease : cateAndUrlList) { //判断是否是第一梯队,或者说是树的根节点,如果是根节点就加入到父类集合与返回集合 if (disease.getParentId() == 0) { parentsList.add(disease); resultList.add(disease); } else { //对于不是第一梯队的数据进行遍历 for (CateAndUrl parent : parentsList) { //对数据的pid与父类集合中的父节点进行配对,如果配对成功,就把数据加入到父节点中的子节点集合 if (parent.getFloder().equals("true") ) { if( parent.getCateId() == disease.getParentId()) { parent.getChildUC().add(disease); //当前数据有可能是别的数据的父节点,加到父类容器 parentsList.add(disease); break; } } if (parent.getFloder().equals("false") ) { if(parent.getUrlId() == disease.getParentId()) { parent.getChildUC().add(disease); //当前数据有可能是别的数据的父节点,加到父类容器 parentsList.add(disease); break; } } } } } ObjectMapper mapper = new ObjectMapper(); String Json; HashMap result = new HashMap<>(); // java对象转换为json字符换 try { Json = mapper.writeValueAsString(resultList); result.put("code", 200); result.put("msg", "处理成功"); result.put("data", resultList); } catch (JsonProcessingException e) { throw new RuntimeException(e); } return JSONObject.toJSONString(result); } @RequestMapping("/disposeBookmarkExhibitedToJson") public String disposeBookmarkExhibitedToJson() { List urlLists = urlListService.selectUrListByNeedLogin(); List cateAndUrlList = new ArrayList<>(); Set parentSet = new HashSet<>(); for( int i = 0 ; i < urlLists.size() ; i++) { CateAndUrl cateAndUrl = new CateAndUrl(); cateAndUrl.setFloder("false"); cateAndUrl.setName(urlLists.get(i).getName()); cateAndUrl.setParentId(urlLists.get(i).getCateId()); cateAndUrl.setUrlId(urlLists.get(i).getId()); cateAndUrlList.add(cateAndUrl); String parentString = urlCateListService.selectUrListCateByUrlCateId(urlLists.get(i).getCateId()); String [] parentList = parentString.split(","); for(String parent : parentList) { if(parent.equals("0")){ continue; } parentSet.add(parent); } } for (String str : parentSet) { UrlCateList urlCateList =urlCateListService.selectByPrimaryKey(Integer.valueOf(str)); CateAndUrl cateAndUrl = new CateAndUrl(); cateAndUrl.setFloder("true"); cateAndUrl.setName(urlCateList.getName()); cateAndUrl.setParentId(urlCateList.getRootCateId()); cateAndUrl.setCateId(urlCateList.getId()); cateAndUrlList.add(cateAndUrl); } Collections.reverse(cateAndUrlList); List parentsList = new ArrayList<>(); //声明返回集合 List resultList = new ArrayList<>(); //对全部数据进行遍历 for (CateAndUrl disease : cateAndUrlList) { //判断是否是第一梯队,或者说是树的根节点,如果是根节点就加入到父类集合与返回集合 if (disease.getParentId() == 0) { parentsList.add(disease); resultList.add(disease); } else { //对于不是第一梯队的数据进行遍历 for (CateAndUrl parent : parentsList) { //对数据的pid与父类集合中的父节点进行配对,如果配对成功,就把数据加入到父节点中的子节点集合 if (parent.getFloder().equals("true") ) { if( parent.getCateId() == disease.getParentId()) { parent.getChildUC().add(disease); //当前数据有可能是别的数据的父节点,加到父类容器 parentsList.add(disease); break; } } if (parent.getFloder().equals("false") ) { if(parent.getUrlId() == disease.getParentId()) { parent.getChildUC().add(disease); //当前数据有可能是别的数据的父节点,加到父类容器 parentsList.add(disease); break; } } } } } ObjectMapper mapper = new ObjectMapper(); String Json; HashMap result = new HashMap<>(); // java对象转换为json字符换 try { Json = mapper.writeValueAsString(resultList); result.put("code", 200); result.put("msg", "处理成功"); result.put("data", resultList); } catch (JsonProcessingException e) { throw new RuntimeException(e); } return JSONObject.toJSONString(result); } public String setResult(Integer code, String msg) { HashMap result = new HashMap<>(); result.put("code", code); result.put("msg", msg); String jsonString = JSONObject.toJSONString(result); return jsonString; } //详情页面 @RequestMapping("/clickUrl") public String clickUrl(String urlId) { UrlList urlList = urlListService.selectUrlListByUrlId(Long.valueOf(urlId)); HashMap result = new HashMap<>(); result.put("code", 200); result.put("msg", "查询成功"); result.put("data",urlList); String jsonString = JSONObject.toJSONString(result); return jsonString; } public String countCateContainUrlNumber(String email){ HashMap result = new HashMap<>(); result.put("code", 200); result.put("msg", "查询成功"); result.put("data",urlCateListService.countCateContainUrlNumber(email)); String jsonString = JSONObject.toJSONString(result); return jsonString; } //处理成JSON // @RequestMapping("/disposeJsonToExcel") // public String disposeJsonToExcel(String userId) // { // // HashMap result = new HashMap<>(); // result.put("code", 200); // result.put("msg", "查询成功"); // result.put("data",urlCateListService.getUrl(userId)); // String jsonString = JSONObject.toJSONString(result); // return jsonString; // } /** * json 转 excel * @param jsonArray * @return * @throws IOException */ public HSSFWorkbook jsonToExcel(JSONArray jsonArray) throws IOException { Set keys = new HashSet<>(); // 创建HSSFWorkbook对象 HSSFWorkbook wb = new HSSFWorkbook(); // 创建HSSFSheet对象 HSSFSheet sheet = wb.createSheet("sheet0"); List jsonObjects = jsonArray.toList(JSONObject.class); // 创建HSSFRow对象 HSSFRow row ; for (int i=0;i result = new HashMap<>(); result.put("code", 500); result.put("msg", "下载失败"); return JSON.toJSONString(result); } } @RequestMapping ("/disposeUrl") public String disposeUrl(String url){ HashMap result = new HashMap<>(); HashMap data = new HashMap<>(); Connection conn = Jsoup.connect(url); Document document = null; try { document = conn.get(); } catch (IOException e) { e.printStackTrace(); result.put("code", 500); result.put("msg", "url不可用"); // handle error } result.put("code", 200); result.put("msg", "解析成功"); //添加标题 data.put("title",document.title()); Elements linksContent = document.select("meta[name='keywords']"); Elements linksDescription = document.select("meta[name='description']"); Elements linksIcon = document.select("link[rel$=icon]"); String fengli="content=\"(.*?)\""; Pattern pafengli=Pattern.compile(fengli); Matcher description = pafengli.matcher(linksDescription.toString()); String target; if(description.find()) { target = description.group(1); //group为捕获组 data.put("description",target); } else { data.put("description","未找到"); } description = pafengli.matcher(linksContent.toString()); if(description.find()) { target = description.group(1); //group为捕获组 data.put("keywords",target); } else { data.put("keywords","未找到"); } String fengli1="href=\"(.*?)\""; Pattern pafengli1=Pattern.compile(fengli1); description = pafengli1.matcher(linksIcon.toString()); if(description.find()) { target = description.group(1); //group为捕获组 try { new URL(target).toURI(); data.put("iconUrl",target); } catch (MalformedURLException e) { data.put("iconUrl","url不合法"); } catch (URISyntaxException e) { data.put("iconUrl","url不合法"); } } else { data.put("iconUrl","未找到"); } result.put("data",data); return JSONObject.toJSONString(result); } }