diff --git a/weixin4j-mp/pom.xml b/weixin4j-mp/pom.xml index e7c33000..946d1eb5 100644 --- a/weixin4j-mp/pom.xml +++ b/weixin4j-mp/pom.xml @@ -27,11 +27,6 @@ weixin4j-base ${project.version} - - org.apache.poi - poi-ooxml - ${poi.version} - jaxen jaxen diff --git a/weixin4j-mp/src/main/java/com/foxinmy/weixin4j/mp/api/Pay2Api.java b/weixin4j-mp/src/main/java/com/foxinmy/weixin4j/mp/api/Pay2Api.java index 9e3bbfeb..3f0ba1d7 100644 --- a/weixin4j-mp/src/main/java/com/foxinmy/weixin4j/mp/api/Pay2Api.java +++ b/weixin4j-mp/src/main/java/com/foxinmy/weixin4j/mp/api/Pay2Api.java @@ -1,23 +1,21 @@ package com.foxinmy.weixin4j.mp.api; import java.io.BufferedReader; +import java.io.BufferedWriter; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; +import java.io.FileWriter; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; -import java.io.OutputStream; import java.security.KeyStore; import java.security.SecureRandom; import java.security.cert.CertificateFactory; -import java.util.Arrays; import java.util.Calendar; import java.util.Date; import java.util.HashMap; import java.util.LinkedHashMap; -import java.util.LinkedList; -import java.util.List; import java.util.Map; import javax.net.ssl.KeyManagerFactory; @@ -25,7 +23,6 @@ import javax.net.ssl.SSLContext; import javax.net.ssl.TrustManagerFactory; import org.apache.http.Consts; -import org.apache.poi.hssf.usermodel.HSSFWorkbook; import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONObject; @@ -46,7 +43,6 @@ import com.foxinmy.weixin4j.mp.type.BillType; import com.foxinmy.weixin4j.mp.type.IdQuery; import com.foxinmy.weixin4j.mp.type.RefundType; import com.foxinmy.weixin4j.mp.type.SignType; -import com.foxinmy.weixin4j.mp.util.ExcelUtil; import com.foxinmy.weixin4j.token.TokenHolder; import com.foxinmy.weixin4j.util.ConfigUtil; import com.foxinmy.weixin4j.util.DateUtil; @@ -374,7 +370,7 @@ public class Pay2Api extends PayApi { } String formatBillDate = DateUtil.fortmat2yyyyMMdd(billDate); String bill_path = ConfigUtil.getValue("bill_path"); - String fileName = String.format("%s_%s_%s.xls", formatBillDate, + String fileName = String.format("%s_%s_%s.txt", formatBillDate, billType.name().toLowerCase(), weixinAccount.getId()); File file = new File(String.format("%s/%s", bill_path, fileName)); if (file.exists()) { @@ -394,28 +390,19 @@ public class Pay2Api extends PayApi { map.put("sign", sign.toLowerCase()); Response response = request.get(downloadbill_uri, map); BufferedReader reader = null; - OutputStream os = null; + BufferedWriter writer = null; + FileWriter fw = null; try { + fw = new FileWriter(file); + writer = new BufferedWriter(fw); reader = new BufferedReader( new InputStreamReader(response.getStream(), com.foxinmy.weixin4j.model.Consts.GBK)); String line = null; - List bills = new LinkedList(); while ((line = reader.readLine()) != null) { - bills.add(line.replaceAll("`", "").split(",")); + writer.write(line); + writer.newLine(); } - - List headers = Arrays.asList(bills.remove(0)); - List totalDatas = Arrays - .asList(bills.remove(bills.size() - 1)); - List totalHeaders = Arrays - .asList(bills.remove(bills.size() - 1)); - HSSFWorkbook wb = new HSSFWorkbook(); - wb.createSheet(formatBillDate + "对账单"); - ExcelUtil.list2excel(wb, headers, bills); - ExcelUtil.list2excel(wb, totalHeaders, totalDatas); - os = new FileOutputStream(file); - wb.write(os); } catch (IOException e) { throw new WeixinException(e.getMessage()); } finally { @@ -423,8 +410,9 @@ public class Pay2Api extends PayApi { if (reader != null) { reader.close(); } - if (os != null) { - os.close(); + if (writer != null) { + writer.close(); + fw.close(); } } catch (IOException ignore) { ; diff --git a/weixin4j-mp/src/main/java/com/foxinmy/weixin4j/mp/api/Pay3Api.java b/weixin4j-mp/src/main/java/com/foxinmy/weixin4j/mp/api/Pay3Api.java index e3527218..d49b5c86 100644 --- a/weixin4j-mp/src/main/java/com/foxinmy/weixin4j/mp/api/Pay3Api.java +++ b/weixin4j-mp/src/main/java/com/foxinmy/weixin4j/mp/api/Pay3Api.java @@ -1,25 +1,21 @@ package com.foxinmy.weixin4j.mp.api; import java.io.BufferedReader; +import java.io.BufferedWriter; import java.io.File; import java.io.FileInputStream; -import java.io.FileOutputStream; +import java.io.FileWriter; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; -import java.io.OutputStream; import java.io.UnsupportedEncodingException; import java.net.URLEncoder; -import java.util.Arrays; import java.util.Calendar; import java.util.Date; import java.util.HashMap; -import java.util.LinkedList; -import java.util.List; import java.util.Map; import org.apache.http.Consts; -import org.apache.poi.hssf.usermodel.HSSFWorkbook; import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.TypeReference; @@ -39,7 +35,6 @@ import com.foxinmy.weixin4j.mp.type.BillType; import com.foxinmy.weixin4j.mp.type.CurrencyType; import com.foxinmy.weixin4j.mp.type.IdQuery; import com.foxinmy.weixin4j.mp.type.IdType; -import com.foxinmy.weixin4j.mp.util.ExcelUtil; import com.foxinmy.weixin4j.token.TokenHolder; import com.foxinmy.weixin4j.util.ConfigUtil; import com.foxinmy.weixin4j.util.DateUtil; @@ -325,7 +320,7 @@ public class Pay3Api extends PayApi { } String formatBillDate = DateUtil.fortmat2yyyyMMdd(billDate); String bill_path = ConfigUtil.getValue("bill_path"); - String fileName = String.format("%s_%s_%s.xls", formatBillDate, + String fileName = String.format("%s_%s_%s.txt", formatBillDate, billType.name().toLowerCase(), weixinAccount.getId()); File file = new File(String.format("%s/%s", bill_path, fileName)); if (file.exists()) { @@ -341,27 +336,19 @@ public class Pay3Api extends PayApi { Response response = request.post(downloadbill_uri, param); BufferedReader reader = null; - OutputStream os = null; + BufferedWriter writer = null; + FileWriter fw = null; try { - reader = new BufferedReader(new InputStreamReader( - response.getStream(), Consts.UTF_8)); + fw = new FileWriter(file); + writer = new BufferedWriter(fw); + reader = new BufferedReader( + new InputStreamReader(response.getStream(), + com.foxinmy.weixin4j.model.Consts.GBK)); String line = null; - List bills = new LinkedList(); while ((line = reader.readLine()) != null) { - bills.add(line.replaceAll("`", "").split(",")); + writer.write(line); + writer.newLine(); } - - List headers = Arrays.asList(bills.remove(0)); - List totalDatas = Arrays - .asList(bills.remove(bills.size() - 1)); - List totalHeaders = Arrays - .asList(bills.remove(bills.size() - 1)); - HSSFWorkbook wb = new HSSFWorkbook(); - wb.createSheet(formatBillDate + "对账单"); - ExcelUtil.list2excel(wb, headers, bills); - ExcelUtil.list2excel(wb, totalHeaders, totalDatas); - os = new FileOutputStream(file); - wb.write(os); } catch (IOException e) { throw new WeixinException(e.getMessage()); } finally { @@ -369,8 +356,9 @@ public class Pay3Api extends PayApi { if (reader != null) { reader.close(); } - if (os != null) { - os.close(); + if (writer != null) { + writer.close(); + fw.close(); } } catch (IOException ignore) { ; diff --git a/weixin4j-mp/src/main/java/com/foxinmy/weixin4j/mp/util/ExcelUtil.java b/weixin4j-mp/src/main/java/com/foxinmy/weixin4j/mp/util/ExcelUtil.java deleted file mode 100644 index c00ab363..00000000 --- a/weixin4j-mp/src/main/java/com/foxinmy/weixin4j/mp/util/ExcelUtil.java +++ /dev/null @@ -1,438 +0,0 @@ -package com.foxinmy.weixin4j.mp.util; - -import java.io.BufferedInputStream; -import java.io.File; -import java.io.FileInputStream; -import java.text.DecimalFormat; -import java.text.SimpleDateFormat; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.Collection; -import java.util.Date; -import java.util.List; - -import org.apache.poi.hssf.usermodel.HSSFCell; -import org.apache.poi.hssf.usermodel.HSSFCellStyle; -import org.apache.poi.hssf.usermodel.HSSFFont; -import org.apache.poi.hssf.usermodel.HSSFRichTextString; -import org.apache.poi.hssf.usermodel.HSSFRow; -import org.apache.poi.hssf.usermodel.HSSFSheet; -import org.apache.poi.hssf.usermodel.HSSFWorkbook; -import org.apache.poi.hssf.util.HSSFColor; -import org.apache.poi.poifs.filesystem.POIFSFileSystem; -import org.apache.poi.ss.usermodel.Cell; -import org.apache.poi.ss.usermodel.CellStyle; -import org.apache.poi.ss.usermodel.DateUtil; -import org.apache.poi.ss.usermodel.Font; -import org.apache.poi.ss.usermodel.Row; -import org.apache.poi.ss.usermodel.Sheet; -import org.apache.poi.ss.usermodel.Workbook; -import org.apache.poi.xssf.usermodel.XSSFWorkbook; - -import com.alibaba.fastjson.JSONArray; -import com.alibaba.fastjson.JSONObject; -import com.foxinmy.weixin4j.util.StringUtil; - -/** - * excel工具类 - * - * @className ExcelUtil - * @author jy - * @date 2014年11月1日 - * @since JDK 1.7 - * @see - */ -public class ExcelUtil { - - /** - * 读取Excel2003,2007的内容,第一维数组存储的是一行中格列的值,二维数组存储的是多少个行 - * - * @param file - * 输入流 - * @param fileName - * 是2003还是2007 xls:2003,xlsx:2007 - * @throws Exception - */ - public static String[][] read(File file) throws Exception { - String fileExt = getExtension(file.getName()); - if (StringUtil.isNotBlank(fileExt)) { - if (fileExt.toLowerCase().equals("xls")) {// 2003 - BufferedInputStream in = new BufferedInputStream( - new FileInputStream(file)); - // 打开HSSFWorkbook - POIFSFileSystem fs = new POIFSFileSystem(in); - Workbook wb = new HSSFWorkbook(fs); - in.close(); - return readExcel(wb); - } else if (fileExt.toLowerCase().equals("xlsx")) {// 2007 - Workbook wb = new XSSFWorkbook(new FileInputStream(file)); - return readExcel(wb); - } - } - return null; - } - - public static String[][] read4Special(File file, String fileName, - int columnSize) throws Exception { - String fileExt = getExtension(fileName); - if (StringUtil.isNotBlank(fileExt)) { - if (fileExt.toLowerCase().equals("xls")) {// 2003 - BufferedInputStream in = new BufferedInputStream( - new FileInputStream(file)); - // 打开HSSFWorkbook - POIFSFileSystem fs = new POIFSFileSystem(in); - Workbook wb = new HSSFWorkbook(fs); - in.close(); - return readExcel4Special(wb, columnSize); - } else if (fileExt.toLowerCase().equals("xlsx")) {// 2007 - Workbook wb = new XSSFWorkbook(new FileInputStream(file)); - return readExcel4Special(wb, columnSize); - } - } - return null; - } - - /** - * 读取Excel文件中的值 - * - * @param wb - * @return String[][] - */ - private static String[][] readExcel(Workbook wb) throws Exception { - List result = new ArrayList(); - int rowSize = 0; - Cell cell = null; - for (int sheetIndex = 0; sheetIndex < wb.getNumberOfSheets(); sheetIndex++) { - Sheet st = wb.getSheetAt(sheetIndex); - // 第一行为标题,不取 - for (int rowIndex = 1; rowIndex <= st.getLastRowNum(); rowIndex++) { - Row row = st.getRow(rowIndex); - if (row == null) { - continue; - } - int tempRowSize = row.getLastCellNum() + 1; - if (tempRowSize > rowSize) { - rowSize = tempRowSize; - } - String[] values = new String[rowSize]; - Arrays.fill(values, ""); - boolean hasValue = false; - for (short columnIndex = 0; columnIndex <= row.getLastCellNum(); columnIndex++) { - String value = ""; - cell = row.getCell(columnIndex); - if (cell != null) { - // 注意:一定要设成这个,否则可能会出现乱码 - // cell.setEncoding(HSSFCell.ENCODING_UTF_16); - switch (cell.getCellType()) { - case Cell.CELL_TYPE_STRING: - value = cell.getStringCellValue(); - break; - case Cell.CELL_TYPE_NUMERIC: - if (DateUtil.isCellDateFormatted(cell)) { - Date date = cell.getDateCellValue(); - if (date != null) { - value = new SimpleDateFormat( - "yyyy-MM-dd HH:mm:ss").format(date); - } else { - value = ""; - } - } else { - value = getRightStr(cell.getNumericCellValue() - + ""); - // value = - // String.valueOf(cell.getNumericCellValue()); - } - break; - case Cell.CELL_TYPE_FORMULA: - // 导入时如果为公式生成的数据则无值 - if (!("").equals(cell.getStringCellValue())) { - value = cell.getStringCellValue(); - } else { - value = cell.getNumericCellValue() + ""; - } - break; - case Cell.CELL_TYPE_BLANK: - break; - case Cell.CELL_TYPE_ERROR: - value = ""; - break; - case Cell.CELL_TYPE_BOOLEAN: - value = (cell.getBooleanCellValue() == true ? "true" - : "false"); - break; - default: - value = ""; - } - } - if (columnIndex == 0 && value.trim().equals("")) { - break; - } - values[columnIndex] = rightTrim(value); - hasValue = true; - } - if (hasValue) { - result.add(values); - } - } - } - String[][] returnArray = new String[result.size()][rowSize]; - for (int i = 0; i < returnArray.length; i++) { - returnArray[i] = result.get(i); - } - return returnArray; - } - - /* - * 读取excel数据 - */ - private static String[][] readExcel4Special(Workbook wb, int columnSize) - throws Exception { - List result = new ArrayList(); - int rowSize = 0; - Cell cell = null; - for (int sheetIndex = 0; sheetIndex < wb.getNumberOfSheets(); sheetIndex++) { - Sheet st = wb.getSheetAt(sheetIndex); - // 第一行为标题,不取 - for (int rowIndex = 1; rowIndex <= st.getLastRowNum(); rowIndex++) { - Row row = st.getRow(rowIndex); - if (row == null) { - continue; - } - int tempRowSize = row.getLastCellNum() + 1; - if (tempRowSize > rowSize) { - rowSize = tempRowSize; - } - String[] values = new String[columnSize]; - Arrays.fill(values, ""); - boolean hasValue = false; - for (short columnIndex = 0; columnIndex < columnSize; columnIndex++) { - String value = ""; - cell = row.getCell(columnIndex); - if (cell != null) { - // 注意:一定要设成这个,否则可能会出现乱码 - // cell.setEncoding(HSSFCell.ENCODING_UTF_16); - switch (cell.getCellType()) { - case Cell.CELL_TYPE_STRING: - value = cell.getStringCellValue(); - break; - case Cell.CELL_TYPE_NUMERIC: - if (DateUtil.isCellDateFormatted(cell)) { - Date date = cell.getDateCellValue(); - if (date != null) { - value = new SimpleDateFormat( - "yyyy-MM-dd HH:mm:ss").format(date); - } else { - value = ""; - } - } else { - value = getRightStr(cell.getNumericCellValue() - + ""); - // value = - // String.valueOf(cell.getNumericCellValue()); - } - break; - case Cell.CELL_TYPE_FORMULA: - // 导入时如果为公式生成的数据则无值 - if (!("").equals(cell.getStringCellValue())) { - value = cell.getStringCellValue(); - } else { - value = cell.getNumericCellValue() + ""; - } - break; - case Cell.CELL_TYPE_BLANK: - break; - case Cell.CELL_TYPE_ERROR: - value = ""; - break; - case Cell.CELL_TYPE_BOOLEAN: - value = (cell.getBooleanCellValue() == true ? "true" - : "false"); - break; - default: - value = ""; - } - } else { - value = ""; - } - if (columnIndex == 0 && value.trim().equals("")) { - break; - } - values[columnIndex] = rightTrim(value); - hasValue = true; - } - if (hasValue) { - result.add(values); - } - } - } - String[][] returnArray = new String[result.size()][columnSize]; - for (int i = 0; i < returnArray.length; i++) { - returnArray[i] = result.get(i); - } - return returnArray; - } - - /** - * double 类型数据转换 - * - * @param sNum - * @return - */ - private static String getRightStr(String sNum) { - DecimalFormat decimalFormat = new DecimalFormat("#.000000"); - - String resultStr = decimalFormat.format(new Double(sNum)); - if (resultStr.equals(".000000")) - return String.valueOf(0d); - if (resultStr.matches("^[-+]?\\d+\\.[0]+$")) { - resultStr = resultStr.substring(0, resultStr.indexOf(".")); - } - return resultStr; - } - - /** - * 去掉字符串右边的空格 - * - * @param str要处理的字符串 - * @return 处理后的字符串 - */ - - public static String rightTrim(String str) { - if (str == null) { - return ""; - } - int length = str.length(); - for (int i = length - 1; i >= 0; i--) { - if (str.charAt(i) != 0x20) { - break; - } - length--; - } - return str.substring(0, length); - } - - /** - * 获取文件扩展名 - * - * @param filename - * @return - */ - private static String getExtension(String filename) { - if ((filename != null) && (filename.length() > 0)) { - int i = filename.lastIndexOf('.'); - - if ((i > 0) && (i < (filename.length() - 1))) { - return filename.substring(i + 1); - } - } - return ""; - } - - public static void list2excel(HSSFWorkbook workbook, List headers, - Collection datas) { - - JSONArray arrays = null; // - - String[] strings = null; // - - HSSFSheet sheet = null; // 工作表 - - HSSFRow row = null; // 单元行 - - HSSFCell cell = null; // 单元格 - - HSSFRichTextString richText = null; // 单元格内容 - - sheet = workbook.getSheetAt(0); // 创建表格 - - int rowNum = sheet.getLastRowNum(); // 数据行号 - if (rowNum != 0) { - rowNum++; - sheet.createRow(rowNum); - rowNum++; - } - - HSSFCellStyle cellStyle = workbook.createCellStyle();// 创建单元格样式(用于表头) - cellStyle.setFillForegroundColor(HSSFColor.SKY_BLUE.index);// 设置单元格样式 - cellStyle.setFillPattern(CellStyle.SOLID_FOREGROUND); - cellStyle.setBorderBottom(CellStyle.BORDER_THIN); - cellStyle.setBorderLeft(CellStyle.BORDER_THIN); - cellStyle.setBorderRight(CellStyle.BORDER_THIN); - cellStyle.setBorderTop(CellStyle.BORDER_THIN); - cellStyle.setAlignment(CellStyle.ALIGN_CENTER); - - HSSFFont f = workbook.createFont(); - f.setColor(HSSFColor.BLUE.index); - - HSSFFont font = workbook.createFont(); // 创建字体 - font.setColor(HSSFColor.VIOLET.index); // 设置字体属性 - font.setFontHeightInPoints((short) 12); - font.setBoldweight(Font.BOLDWEIGHT_BOLD); - - cellStyle.setFont(font); // 设置单元格的字体 - - row = sheet.createRow(rowNum); // 创建表格标题行 - - for (int i = 0; i < headers.size(); i++) { - // 填充标题行的单元格数据 - cell = row.createCell(i); - cell.setCellStyle(cellStyle); - richText = new HSSFRichTextString(headers.get(i)); - cell.setCellValue(richText); - sheet.autoSizeColumn(i); - } - rowNum++; - HSSFCellStyle contentStyle = workbook.createCellStyle();// 创建单元格样式(用于表内容) - contentStyle.setFillForegroundColor(HSSFColor.LIGHT_YELLOW.index); - contentStyle.setFillPattern(CellStyle.SOLID_FOREGROUND); - contentStyle.setBorderBottom(CellStyle.BORDER_THIN); - contentStyle.setBorderLeft(CellStyle.BORDER_THIN); - contentStyle.setBorderRight(CellStyle.BORDER_THIN); - contentStyle.setBorderTop(CellStyle.BORDER_THIN); - contentStyle.setAlignment(CellStyle.ALIGN_CENTER); - contentStyle.setVerticalAlignment(CellStyle.VERTICAL_CENTER); - - font = workbook.createFont(); // 创建字体 - font.setBoldweight(Font.BOLDWEIGHT_NORMAL); // 设置字体粗细 - contentStyle.setFont(font); // 设置单元格的字体样式 - - int j = 0; - for (Object obj : datas) { - row = sheet.getRow(rowNum); - if (row == null) { - row = sheet.createRow(rowNum); - } - if (obj instanceof JSONArray) { - arrays = (JSONArray) obj; - // 只能是 JSONObject - for (int i = 0; i < arrays.size(); i++) { - JSONObject jsonObj = arrays.getJSONObject(i); - cell = row.createCell(i); - cell.setCellStyle(contentStyle); - String val = jsonObj.getString(headers.get(i)); - richText = new HSSFRichTextString(val); - richText.applyFont(f); - cell.setCellValue(richText); - } - rowNum++; - } else if (obj instanceof String[]) { - strings = (String[]) obj; - for (int i = 0; i < strings.length; i++) { - cell = row.createCell(i); - cell.setCellStyle(contentStyle); - richText = new HSSFRichTextString(strings[i]); - richText.applyFont(f); - cell.setCellValue(richText); - } - rowNum++; - } else { - cell = row.createCell(j); - cell.setCellStyle(contentStyle); - richText = new HSSFRichTextString((String) obj); - richText.applyFont(f); - cell.setCellValue(richText); - j++; - } - } - } -} diff --git a/weixin4j-server/src/main/java/com/foxinmy/weixin4j/startup/WeixinServerBootstrap.java b/weixin4j-server/src/main/java/com/foxinmy/weixin4j/startup/WeixinServerBootstrap.java index eb362ff0..7a8a80b4 100644 --- a/weixin4j-server/src/main/java/com/foxinmy/weixin4j/startup/WeixinServerBootstrap.java +++ b/weixin4j-server/src/main/java/com/foxinmy/weixin4j/startup/WeixinServerBootstrap.java @@ -76,7 +76,7 @@ public final class WeixinServerBootstrap { /** * 明文模式 * - * * @param appid 公众号的唯一ID + * * @param token 开发者token * */ public WeixinServerBootstrap(String token) {