Przeglądaj źródła

:zap: POI工具类、短连接工具类

Aron 6 lat temu
rodzic
commit
7eeda568f3

+ 85 - 0
src/main/java/com/ifast/common/utils/ShortUrlUtils.java

@@ -0,0 +1,85 @@
+package com.ifast.common.utils;
+
+import lombok.Data;
+import org.apache.commons.lang3.StringUtils;
+import org.springframework.web.client.RestTemplate;
+
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.List;
+
+/**
+ * <pre>
+ *
+ * </pre>
+ * <small> 2018/12/3 11:14 | Aron</small>
+ */
+public class ShortUrlUtils {
+
+    private final static RestTemplate restTempate = new RestTemplate();
+
+    private final static String WEIBO_API_SHORT_URL = "http://api.t.sina.com.cn/short_url/shorten.json?source=3271760578&";
+
+    /**
+     * <pre>
+     * 转微博短连接
+     * </pre>
+     * <small> 2018/12/3 11:25 | Aron</small>
+     *
+     * @param urlList 原始链接数组
+     * @return
+     */
+    public static List<UrlObject> convertShortUrl(List<String> urlList) {
+        List<String> apiUrlList = new ArrayList<>();
+        StringBuilder url_long = new StringBuilder();
+        int size = urlList.size();
+        for (int i = 0; i < size; i++) {
+            String originUrl = urlList.get(i);
+            if (0 == (i + 1) % 20) {
+                url_long.append("&url_long=").append(originUrl);
+                String apiUrl = WEIBO_API_SHORT_URL + url_long;
+                apiUrlList.add(apiUrl);
+                url_long.delete(0, url_long.length());
+
+            } else {
+                if (StringUtils.isBlank(url_long)) {
+                    url_long.append("url_long=").append(originUrl);
+                } else {
+                    url_long.append("&url_long=").append(originUrl);
+                }
+                // 最后一个
+                if (i == size - 1) {
+                    String apiUrl = WEIBO_API_SHORT_URL + url_long;
+                    apiUrlList.add(apiUrl);
+                }
+            }
+        }
+
+        List<UrlObject> result = new ArrayList<>();
+        apiUrlList.forEach(url -> {
+            List<UrlObject> urlObjects = JSONUtils.jsonToList(restTempate.getForObject(url, String.class), UrlObject.class);
+            urlObjects.forEach(urlObject -> result.add(urlObject));
+
+        });
+        return result;
+    }
+
+    public static UrlObject convertShortUrl(String url) {
+        return convertShortUrl(Arrays.asList(new String[]{url})).get(0);
+    }
+
+
+    public static void main(String[] args) {
+        UrlObject urlObject = convertShortUrl("http://ifast.site");
+        System.out.println(" - - - - - - - - - - - - - - - - - - - - - - - ");
+        System.out.println(urlObject);
+        System.out.println(" - - - - - - - - - - - - - - - - - - - - - - - ");
+    }
+}
+
+@Data
+class UrlObject {
+    private String url_short;
+    private String url_long;
+    private int type;
+}

+ 85 - 0
src/main/java/com/ifast/demo/controller/PoiUtils.java

@@ -0,0 +1,85 @@
+package com.ifast.demo.controller;
+
+import org.apache.poi.ss.usermodel.*;
+
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.IOException;
+import java.text.DecimalFormat;
+import java.util.*;
+
+/**
+ * excel 表格工具类
+ *
+ * @author zenglong
+ */
+public class PoiUtils {
+
+    /**
+     * 通用读取方法
+     * Excel要求 :
+     * 第一行为表头,表头字段即map的键;
+     * 第二行为数据内容;
+     * 注意:
+     * 这里默认只读取第一个工作簿;
+     *
+     * @param filePath
+     * @return ListMap
+     */
+    public static List<Map<String, Object>> getList(String filePath) {
+        File excelFile = new File(filePath);
+        FileInputStream is = null;
+        Workbook workbook = null;
+        List<Map<String, Object>> listMap = new ArrayList<>();
+        List<String> titles = new ArrayList<>();
+        try {
+            is = new FileInputStream(excelFile);
+            if (is != null) {
+                workbook = WorkbookFactory.create(is);
+                Sheet sheet = workbook.getSheetAt(0);
+                Iterator<Row> rows = sheet.rowIterator();
+                System.out.println("开始读取表头 ");
+                Row title = rows.next();
+                // 列数
+                int cols = 0;
+                for (int i = 0; i < title.getPhysicalNumberOfCells(); i++) {
+                    titles.add(title.getCell(i).toString());
+                    cols++;
+                }
+                System.out.println("表头:" + titles);
+
+                System.out.println("开始读取数据 ");
+                while (rows.hasNext()) {
+                    Map<String, Object> map = new HashMap<>(256);
+                    org.apache.poi.ss.usermodel.Row row = rows.next();
+                    for (int i = 0; i < cols; i++) {
+                        Cell cell = row.getCell(i);
+                        if (cell != null) {
+                            if (cell.getCellType() == Cell.CELL_TYPE_NUMERIC) {
+                                String temp = new DecimalFormat("#.########").format(cell.getNumericCellValue());
+                                map.put(titles.get(i), temp);
+                            } else {
+                                map.put(titles.get(i), cell.getStringCellValue());
+                            }
+                        } else {
+                            map.put(titles.get(i), "");
+                        }
+                    }
+                    listMap.add(map);
+                }
+                System.out.println("读取完毕 ,总数:" + listMap.size());
+            }
+        } catch (Exception e) {
+            e.printStackTrace();
+        } finally {
+            if (is != null) {
+                try {
+                    is.close();
+                } catch (IOException e) {
+                    System.out.println("流关闭失败");
+                }
+            }
+        }
+        return listMap.size() == 0 ? null : listMap;
+    }
+}