Java tutorial
/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package com.cn.controller; import com.alibaba.fastjson.JSONObject; import com.cn.bean.ClassDescription; import com.cn.bean.FieldDescription; import com.cn.util.DatabaseOpt; import com.cn.util.ExportExcel; import com.cn.util.Units; import java.beans.PropertyDescriptor; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.InputStream; import java.io.OutputStream; import java.lang.reflect.Field; import java.lang.reflect.Method; import java.util.ArrayList; import java.util.Iterator; import java.util.List; import org.apache.log4j.Logger; import org.apache.poi.hssf.usermodel.HSSFWorkbook; import org.apache.poi.ss.usermodel.Cell; import org.apache.poi.ss.usermodel.Row; import org.apache.poi.ss.usermodel.Sheet; import org.apache.poi.xssf.usermodel.XSSFWorkbook; /** * * @author LFeng */ public class InterfaceController { private static final Logger logger = Logger.getLogger(InterfaceController.class); public String createOperate(int pageSize, String type, String jsonPackagePath, String beanPackage, String tableName, String orderField, String connType) throws Exception { return createOperateWithFilter(pageSize, type, jsonPackagePath, beanPackage, tableName, "", orderField, connType); } /** * ?? * * @param type * @param tableName * @param orderField * @return * @throws FileNotFoundException * @throws Exception */ public String createOperateWithFilter(int pageSize, String type, String jsonPackagePath, String beanPackage, String tableName, String whereCase, String orderField, String conn) throws Exception { String json; CommonController commonController = new CommonController(); String path = this.getClass().getClassLoader().getResource("/").getPath().replaceAll("%20", " "); String result = Units.returnFileContext(path + jsonPackagePath, tableName + ".json"); Class objClass = Class.forName(beanPackage + tableName); Method method = objClass.getMethod("getRecordCount", new Class[0]); if (result != null) { List<Object> list = commonController.dataBaseQuery(type, beanPackage, tableName, "*", whereCase, pageSize, 1, orderField, 0, conn); if (list != null && list.size() > 0) { StringBuffer buffer = new StringBuffer(result); buffer.insert(buffer.lastIndexOf("}"), ", \"datas\":" + JSONObject.toJSONString(list, Units.features)); buffer.insert(buffer.lastIndexOf("}"), ", \"counts\":" + method.invoke(null, new Object[] {})); result = buffer.toString(); } json = Units.objectToJson(0, "", result); } else { json = Units.objectToJson(-1, "?!", null); } return json; } public String createOperateOnDate(int pageSize, String type, String jsonPackagePath, String beanPackage, String tableName, String datas, String rely, String whereCase, String orderField, String dataType) throws Exception { String json; CommonController commonController = new CommonController(); DatabaseOpt opt = new DatabaseOpt(); String conn; if (dataType.compareToIgnoreCase("isHis") == 0) { conn = DatabaseOpt.HIS; } else { conn = DatabaseOpt.DATA; } String path = this.getClass().getClassLoader().getResource("/").getPath().replaceAll("%20", " "); String result = Units.returnFileContext(path + jsonPackagePath, tableName + ".json"); Class objClass = Class.forName(beanPackage + tableName); Method method = objClass.getMethod("getRecordCount", new Class[0]); String whereSql = commonController.getWhereSQLStrWithDate(objClass, datas, rely, true); if (Units.strIsEmpty(whereSql)) { whereSql = whereCase; } else { whereSql = whereSql + (Units.strIsEmpty(whereCase) ? "" : " and " + whereCase); } if (result != null) { List<Object> list = commonController.dataBaseQuery(type, beanPackage, tableName, "*", whereSql, pageSize, 1, orderField, 0, conn); if (list != null && list.size() > 0) { StringBuffer buffer = new StringBuffer(result); buffer.insert(buffer.lastIndexOf("}"), ", \"datas\":" + JSONObject.toJSONString(list, Units.features)); buffer.insert(buffer.lastIndexOf("}"), ", \"counts\":" + method.invoke(null, new Object[] {})); result = buffer.toString(); } json = Units.objectToJson(0, "", result); } else { json = Units.objectToJson(-1, "?!", null); } return json; } /** * ??, ? * @param beanPackage * @param type * @param tableName * @param orderField * @param keyWord * @param conn * @param pageSize * @param pageIndex * @return * @throws Exception */ public List<Object> queryData(String beanPackage, String type, String tableName, String orderField, String keyWord, String conn, int pageSize, int pageIndex) throws Exception { CommonController commonController = new CommonController(); Class objClass = Class.forName(beanPackage + tableName); return commonController.dataBaseQuery(type, beanPackage, tableName, "*", commonController.getWhereSQLStr(objClass, keyWord, "{}", true), pageSize, pageIndex, orderField, 0, conn); } public String queryOperate(String beanPackage, String type, String tableName, String orderField, String keyWord, String rely, boolean isAll, String conn, int pageSize, int pageIndex) throws Exception { return queryOperateWithFilter(beanPackage, type, tableName, orderField, keyWord, rely, null, isAll, conn, pageSize, pageIndex); } /** * ?? * @param beanPackage * @param type * @param tableName * @param orderField * @param keyWord * @param rely * @param whereCase * @param isAll * @param conn * @param pageSize * @param pageIndex * @return * @throws Exception */ public String queryOperateWithFilter(String beanPackage, String type, String tableName, String orderField, String keyWord, String rely, String whereCase, boolean isAll, String conn, int pageSize, int pageIndex) throws Exception { String json; String result = "{}"; CommonController commonController = new CommonController(); Class objClass = Class.forName(beanPackage + tableName); Method method = objClass.getMethod("getRecordCount", new Class[0]); String whereSql = commonController.getWhereSQLStr(objClass, keyWord, rely, isAll); if (Units.strIsEmpty(whereSql)) { whereSql = whereCase; } else { whereSql = whereSql + (Units.strIsEmpty(whereCase) ? "" : " and " + whereCase); } // System.out.println("whereSql:" + whereSql); List<Object> list = commonController.dataBaseQuery(type, beanPackage, tableName, "*", whereSql, pageSize, pageIndex, orderField, 0, conn); // System.out.println("list size:" + list.size()); if (list != null && list.size() > 0) { StringBuffer buffer = new StringBuffer(result); buffer.insert(buffer.lastIndexOf("}"), "\"datas\":" + JSONObject.toJSONString(list, Units.features)); buffer.insert(buffer.lastIndexOf("}"), ",\"counts\":" + method.invoke(null, new Object[] {})); //buffer.insert(buffer.lastIndexOf("}"), ",\"rely\":" + rely); result = buffer.toString(); json = Units.objectToJson(0, "", result); } else { json = Units.objectToJson(-1, "?!", null); } return json; } public List<Object> queryOperate(String beanPackage, String type, String tableName, String orderField, String keyWord, String rely, String whereCase, boolean isAll, String conn, int pageSize, int pageIndex) throws Exception { CommonController commonController = new CommonController(); Class objClass = Class.forName(beanPackage + tableName); Method method = objClass.getMethod("getRecordCount", new Class[0]); String whereSql = commonController.getWhereSQLStr(objClass, keyWord, rely, isAll); if (Units.strIsEmpty(whereSql)) { whereSql = whereCase; } else { whereSql = whereSql + (Units.strIsEmpty(whereCase) ? "" : " and " + whereCase); } return commonController.dataBaseQuery(type, beanPackage, tableName, "*", whereSql, pageSize, pageIndex, orderField, 0, conn); } /** * ?? * @param beanPackage * @param type * @param tableName * @param orderField * @param keyWord * @param rely , ?: {"start": "startTime", "end": "endTime"} * @param whereCase * @param isAll * @param dataType * @param pageSize * @param pageIndex * @return * @throws Exception */ public String queryOnDateOperate(String beanPackage, String type, String tableName, String orderField, String keyWord, String rely, String whereCase, boolean isAll, String dataType, int pageSize, int pageIndex) throws Exception { String json; String result = "{}"; CommonController commonController = new CommonController(); DatabaseOpt opt = new DatabaseOpt(); String conn; if (dataType.compareToIgnoreCase("isHis") == 0) { conn = DatabaseOpt.HIS; } else { conn = DatabaseOpt.DATA; } Class objClass = Class.forName(beanPackage + tableName); Method method = objClass.getMethod("getRecordCount", new Class[0]); String whereSql = commonController.getWhereSQLStrWithDate(objClass, keyWord, rely, isAll); if (Units.strIsEmpty(whereSql)) { whereSql = whereCase; } else { whereSql = whereSql + (Units.strIsEmpty(whereCase) ? "" : " and " + whereCase); } // System.out.println("where SQL:" + whereSql); List<Object> list = commonController.dataBaseQuery(type, beanPackage, tableName, "*", whereSql, pageSize, pageIndex, orderField, 0, conn); if (list != null && list.size() > 0) { StringBuffer buffer = new StringBuffer(result); buffer.insert(buffer.lastIndexOf("}"), "\"datas\":" + JSONObject.toJSONString(list, Units.features)); buffer.insert(buffer.lastIndexOf("}"), ",\"counts\":" + method.invoke(null, new Object[] {})); //buffer.insert(buffer.lastIndexOf("}"), ",\"rely\":" + rely); result = buffer.toString(); json = Units.objectToJson(0, "", result); } else { json = Units.objectToJson(-1, "?!", null); } return json; } /** * ?, ?, ?key * @param target * @param beanPackage * @param type * @param tableName * @param orderField * @param keyWord * @param rely * @param isAll * @param conn * @param pageSize * @param pageIndex * @param keys ?key * @param keysName ?key??(keys???) * @param keysWidth ?key(keys???) * @param fieldsName key(keys???) * @return * @throws Exception */ public String queryOperate(String target, String beanPackage, String type, String tableName, String orderField, String keyWord, String rely, boolean isAll, String conn, int pageSize, int pageIndex, String[] keys, String[] keysName, int[] keysWidth, String[] fieldsName) throws Exception { String json; CommonController commonController = new CommonController(); Class objClass = Class.forName(beanPackage + tableName); Method method = objClass.getMethod("getRecordCount", new Class[0]); List<Object> list = commonController.dataBaseQuery(type, beanPackage, tableName, "*", commonController.getWhereSQLStr(objClass, keyWord, rely, isAll), pageSize, pageIndex, orderField, 0, conn); if (null != list && list.size() > 0) { StringBuffer buffer = new StringBuffer(); buffer.append("{\"titles\":{"); for (int i = 0; i < keys.length; i++) { buffer.append("\"").append(keys[i]).append("\"").append(":"); buffer.append("\"").append(keysName[i]).append(",").append(keysWidth[i]).append("%").append("\"") .append(","); } buffer.deleteCharAt(buffer.length() - 1); buffer.append("},\"datas\":["); for (Iterator<Object> it = list.iterator(); it.hasNext();) { Object object = it.next(); buffer.append("{"); for (int i = 0; i < keys.length; i++) { PropertyDescriptor descriptor = new PropertyDescriptor(fieldsName[i], objClass); Method getMethod = descriptor.getReadMethod(); buffer.append("\"").append(keys[i]).append("\":").append("\"").append(getMethod.invoke(object)) .append("\"").append(","); } buffer.deleteCharAt(buffer.length() - 1); buffer.append("},"); } buffer.deleteCharAt(buffer.length() - 1); buffer.append("]"); buffer.append(",\"counts\":").append(method.invoke(null, new Object[] {})); buffer.append(",\"target\":").append("\"").append(target).append("\""); buffer.append(",\"rely\":").append(rely); if (objClass.isAnnotationPresent(ClassDescription.class)) { ClassDescription description = (ClassDescription) objClass.getAnnotation(ClassDescription.class); buffer.append(",\"module\":").append("\"").append(description.classDesc()).append("\""); } buffer.append("}"); json = Units.objectToJson(0, "", buffer.toString()); } else { json = Units.objectToJson(-1, "?!", null); } return json; } public String queryOperate(String target, String beanPackage, String type, String tableName, String orderField, String keyWord, String rely, String whereCase, boolean isAll, String conn, int pageSize, int pageIndex, String[] keys, String[] keysName, int[] keysWidth, String[] fieldsName) throws Exception { String json; CommonController commonController = new CommonController(); Class objClass = Class.forName(beanPackage + tableName); Method method = objClass.getMethod("getRecordCount", new Class[0]); String whereSql = commonController.getWhereSQLStr(objClass, keyWord, rely, isAll); if (Units.strIsEmpty(whereSql)) { whereSql = whereCase; } else { whereSql = whereSql + (Units.strIsEmpty(whereCase) ? "" : " and " + whereCase); } List<Object> list = commonController.dataBaseQuery(type, beanPackage, tableName, "*", whereSql, pageSize, pageIndex, orderField, 0, conn); if (null != list && list.size() > 0) { StringBuffer buffer = new StringBuffer(); buffer.append("{\"titles\":{"); for (int i = 0; i < keys.length; i++) { buffer.append("\"").append(keys[i]).append("\"").append(":"); buffer.append("\"").append(keysName[i]).append(",").append(keysWidth[i]).append("%").append("\"") .append(","); } buffer.deleteCharAt(buffer.length() - 1); buffer.append("},\"datas\":["); for (Iterator<Object> it = list.iterator(); it.hasNext();) { Object object = it.next(); buffer.append("{"); for (int i = 0; i < keys.length; i++) { PropertyDescriptor descriptor = new PropertyDescriptor(fieldsName[i], objClass); Method getMethod = descriptor.getReadMethod(); buffer.append("\"").append(keys[i]).append("\":").append("\"").append(getMethod.invoke(object)) .append("\"").append(","); } buffer.deleteCharAt(buffer.length() - 1); buffer.append("},"); } buffer.deleteCharAt(buffer.length() - 1); buffer.append("]"); buffer.append(",\"counts\":").append(method.invoke(null, new Object[] {})); buffer.append(",\"target\":").append("\"").append(target).append("\""); buffer.append(",\"rely\":").append(rely); if (objClass.isAnnotationPresent(ClassDescription.class)) { ClassDescription description = (ClassDescription) objClass.getAnnotation(ClassDescription.class); buffer.append(",\"module\":").append("\"").append(description.classDesc()).append("\""); } buffer.append("}"); json = Units.objectToJson(0, "", buffer.toString()); } else { json = Units.objectToJson(-1, "?!", null); } return json; } public String getSpecialTableJsonStr(List<Object> list, String className, String[] keys, String[] keysName, int[] keysWidth, String[] fieldsName, String target, String rely) throws Exception { Class objClass = Class.forName(className); Method method = objClass.getMethod("getRecordCount", new Class[0]); StringBuffer buffer = new StringBuffer(); buffer.append("{\"titles\":{"); for (int i = 0; i < keys.length; i++) { buffer.append("\"").append(keys[i]).append("\"").append(":"); buffer.append("\"").append(keysName[i]).append(",").append(keysWidth[i]).append("%").append("\"") .append(","); } buffer.deleteCharAt(buffer.length() - 1); buffer.append("},\"datas\":["); for (Iterator<Object> it = list.iterator(); it.hasNext();) { Object object = it.next(); buffer.append("{"); for (int i = 0; i < keys.length; i++) { PropertyDescriptor descriptor = new PropertyDescriptor(fieldsName[i], objClass); Method getMethod = descriptor.getReadMethod(); buffer.append("\"").append(keys[i]).append("\":").append("\"").append(getMethod.invoke(object)) .append("\"").append(","); } buffer.deleteCharAt(buffer.length() - 1); buffer.append("},"); } buffer.deleteCharAt(buffer.length() - 1); buffer.append("]"); buffer.append(",\"counts\":").append(method.invoke(null, new Object[] {})); buffer.append(",\"target\":").append("\"").append(target).append("\""); buffer.append(",\"rely\":").append(rely); if (objClass.isAnnotationPresent(ClassDescription.class)) { ClassDescription description = (ClassDescription) objClass.getAnnotation(ClassDescription.class); buffer.append(",\"module\":").append("\"").append(description.classDesc()).append("\""); } buffer.append("}"); return Units.objectToJson(0, "", buffer.toString()); } /** * ???? * @param beanPackage * @param tableName * @param update * @param add * @param delete * @param connType * @return * @throws Exception */ public String submitOperate(String beanPackage, String tableName, String update, String add, String delete, String connType) throws Exception { CommonController commonController = new CommonController(); DatabaseOpt opt = new DatabaseOpt(); String conn = (connType.compareTo("base") == 0) ? DatabaseOpt.BASE : DatabaseOpt.DATA; if (!Units.strIsEmpty(update) && !(update.compareTo("[]") == 0)) { ArrayList<Integer> updateResult = commonController.dataBaseOperate(update, beanPackage, tableName, "update", conn); if (updateResult.get(0) != 0) { return Units.objectToJson(-1, "?!", null); } } if (!Units.strIsEmpty(add) && !(add.compareTo("[]") == 0)) { ArrayList<Integer> addResult = commonController.dataBaseOperate(add, beanPackage, tableName, "add", conn); if (addResult.get(0) != 0) { return Units.objectToJson(-1, "?!", null); } } if (!Units.strIsEmpty(delete) && !(delete.compareTo("[]") == 0)) { ArrayList<Integer> delResult = commonController.dataBaseOperate(delete, beanPackage, tableName, "delete", conn); if (delResult.get(0) != 0) { return Units.objectToJson(-1, "?!", null); } } return Units.objectToJson(0, "??!", null); } /** * Excel? * @param beanPackage * @param tableName * @param fileName * @param conn * @return * @throws Exception */ public String importData(String beanPackage, String tableName, String fileName, String conn) throws Exception { String json; CommonController commonController = new CommonController(); //??? Class objClass = Class.forName(beanPackage + tableName); Field[] fields = objClass.getDeclaredFields(); ArrayList<Field> accessFields = new ArrayList<>(); ArrayList<String> fieldDes = new ArrayList<>(); for (Field field : fields) { if (field.isAnnotationPresent(FieldDescription.class)) { FieldDescription description = field.getAnnotation(FieldDescription.class); if (description.operate().compareTo("import") == 0) { fieldDes.add(description.description()); accessFields.add(field); } } } //?, ?Excel? InputStream inputStream = null; File file = new File(fileName); inputStream = new FileInputStream(file); Sheet sheet; if (fileName.endsWith(".xls")) { HSSFWorkbook workbook = new HSSFWorkbook(inputStream); sheet = workbook.getSheetAt(0); } else if (fileName.endsWith(".xlsx")) { XSSFWorkbook workbook = new XSSFWorkbook(inputStream); sheet = workbook.getSheetAt(0); } else { logger.info("?Excel!"); return null; } Row headerRow = sheet.getRow(0); //????, ??? // System.out.println("cells num:" + headerRow.getPhysicalNumberOfCells() + ",des size:" + fieldDes.size()); if (headerRow == null || headerRow.getPhysicalNumberOfCells() != fieldDes.size()) { json = Units.objectToJson(-1, "???, ?, ???", null); return json; } //????? int[] templateDataIndex = new int[fieldDes.size()]; for (int i = 0; i < fieldDes.size(); i++) { Cell cell = headerRow.getCell(i); cell.setCellType(Cell.CELL_TYPE_STRING); String fieldName = cell.getStringCellValue(); templateDataIndex[fieldDes.indexOf(fieldName)] = i; } ArrayList<Object> result = new ArrayList<>(); //??, List for (int i = 1; i <= sheet.getPhysicalNumberOfRows(); i++) { Row row = sheet.getRow(i); if (row == null) { continue; } if (Units.isEmptyRowForExcel(row)) { continue; } Object object = objClass.newInstance(); for (int j = 0; j < accessFields.size(); j++) { Field field = accessFields.get(j); field.setAccessible(true); Cell cell = row.getCell(templateDataIndex[j]); // String fieldType = field.getGenericType().toString(); if (field.getType() == int.class) { if (cell == null) { field.set(object, 0); } else { if (Units.strIsEmpty(Units.getStringCellValue(cell))) { field.set(object, 0); } else { field.set(object, Double.valueOf(Units.getStringCellValue(cell)).intValue()); } } } else if (field.getType() == float.class) { if (cell == null) { field.set(object, 0); } else { if (Units.strIsEmpty(Units.getStringCellValue(cell))) { field.set(object, 0); } else { field.set(object, Double.valueOf(Units.getStringCellValue(cell)).floatValue()); } } } else if (field.getType() == double.class) { if (cell == null) { field.set(object, 0); } else { if (Units.strIsEmpty(Units.getStringCellValue(cell))) { field.set(object, 0); } else { field.set(object, Double.valueOf(Units.getStringCellValue(cell))); } } } else if (field.getType() == boolean.class) { if (cell == null) { field.set(object, false); } else { if (Units.strIsEmpty(Units.getCellValue(cell))) { field.set(object, false); } else { field.set(object, Boolean.valueOf(Units.getStringCellValue(cell))); } } } else { if (cell == null) { field.set(object, null); } else { field.set(object, Units.getStringCellValue(cell)); } } } result.add(object); } ArrayList<Integer> addResult = commonController.dataBaseOperate( JSONObject.toJSONString(result, Units.features), beanPackage, tableName, "add", conn); // System.out.println("import res:" + Arrays.toString(addResult.toArray())); if (addResult.get(0) == 0) { json = Units.objectToJson(0, "?" + (addResult.size() - 1) + "??!", null); } else if (addResult.get(0) == 2) { json = Units.objectToJson(2, "?!", null); } else { int successNum = 0, failedNum = 0; String failedMsg = ":"; for (int i = 1; i < addResult.size(); i++) { int res = addResult.get(i); if (res == 1) { successNum++; } else { failedNum++; failedMsg += (i + 1) + ","; } } json = Units.objectToJson(-1, "?" + successNum + "??, " + failedNum + "??!" + failedMsg, null); } return json; } /** * ?Excel * @param filePath * @param servletPath * @param beanPackage * @param tableName * @param datas * @return * @throws Exception */ public String exportData(String filePath, String servletPath, String beanPackage, String tableName, ArrayList<Object> datas) throws Exception { Class objClass = Class.forName(beanPackage + tableName); Field[] fields = objClass.getDeclaredFields(); ArrayList<String> fieldDes = new ArrayList<>(); for (Field field : fields) { if (field.isAnnotationPresent(FieldDescription.class)) { FieldDescription description = field.getAnnotation(FieldDescription.class); fieldDes.add(description.description()); } } // String filePath = getServletContext().getRealPath("/").replace("\\", "/") + "exportFile/"; String fileName = Units.getNowTimeNoSeparator() + ".xls"; File file = Units.createNewFile(filePath, fileName); OutputStream stream = new FileOutputStream(file); ExportExcel exportExcel = new ExportExcel(); String[] headers = new String[fieldDes.size()]; for (int i = 0; i < fieldDes.size(); i++) { headers[i] = fieldDes.get(i); } exportExcel.exportExcel("", headers, datas, stream, "yyyy-MM-dd HH:mm:ss"); return Units.objectToJson(0, "?!", "{\"fileUrl\":\"" + servletPath + "/exportFile/" + fileName + "\"}"); } /** * ? * @param filePath * @param servletPath * @param beanPackage * @param tableName * @param datas * @return * @throws Exception */ public String exportTemplate(String filePath, String servletPath, String beanPackage, String tableName, ArrayList<Object> datas) throws Exception { Class objClass = Class.forName(beanPackage + tableName); Field[] fields = objClass.getDeclaredFields(); ArrayList<String> fieldDes = new ArrayList<>(); for (Field field : fields) { if (field.isAnnotationPresent(FieldDescription.class)) { FieldDescription description = field.getAnnotation(FieldDescription.class); if (description.operate().compareTo("import") == 0) { fieldDes.add(description.description()); } } } String fileName = Units.getNowTimeNoSeparator() + ".xls"; File file = Units.createNewFile(filePath, fileName); OutputStream stream = new FileOutputStream(file); ExportExcel exportExcel = new ExportExcel(); String[] headers = new String[fieldDes.size()]; for (int i = 0; i < fieldDes.size(); i++) { headers[i] = fieldDes.get(i); } exportExcel.exportExcel("", headers, datas, stream, "yyyy-MM-dd HH:mm:ss"); return Units.objectToJson(0, "?!", "{\"fileUrl\":\"" + servletPath + "/exportFile/" + fileName + "\"}"); } /** * ?Excel(??) * @param filePath * @param servletPath * @param beanPackage * @param tableName * @param datas * @return * @throws Exception */ public String exportDataReturnFileName(String filePath, String servletPath, String beanPackage, String tableName, List<Object> datas) throws Exception { Class objClass = Class.forName(beanPackage + tableName); Field[] fields = objClass.getDeclaredFields(); ArrayList<String> fieldDes = new ArrayList<>(); for (Field field : fields) { if (field.isAnnotationPresent(FieldDescription.class)) { FieldDescription description = field.getAnnotation(FieldDescription.class); fieldDes.add(description.description()); } } //String filePath = getServletContext().getRealPath("/").replace("\\", "/") + "exportFile/"; String fileName = Units.getNowTimeNoSeparator() + ".xls"; File file = Units.createNewFile(filePath, fileName); OutputStream stream = new FileOutputStream(file); ExportExcel exportExcel = new ExportExcel(); String[] headers = new String[fieldDes.size()]; for (int i = 0; i < fieldDes.size(); i++) { headers[i] = fieldDes.get(i); } exportExcel.exportExcel("", headers, datas, stream, "yyyy-MM-dd HH:mm:ss"); return servletPath + "/exportFile/" + fileName; //return Units.objectToJson(0, "?!", "{\"fileUrl\":\"" + getServletContext().getContextPath() + "/exportFile/" + fileName + "\"}"); } }