Eolink上传文件到Java后台进行处理的示例代码
短信预约 -IT技能 免费直播动态提醒
Eolink上传文件配置:
接收文件请求并进行业务处理
@RequestMapping(value = "shangchuan")
@ResponseBody
public synchronized R fileUpload(HttpServletRequest request) {
try {
String[] fields = { "gddname", "lineName", "gddgpsjd", "gddgpswd", "remarks" };
List<DqGddname> list = ExcelImportUtil.getImportData(fields, DqGddname.class, request);
for (DqGddname dqGddname : list) {
EntityWrapper<DqLinename> entityWrapper = new EntityWrapper<DqLinename>();
Wrapper<DqLinename> wrapper = entityWrapper.eq("linename", dqGddname.getLineName());
DqLinename dqLinename = dqLinenameService.selectOne(wrapper);
if (dqLinename == null) {
return R.error("线路数据不存在");
} else {
dqGddname.setLineid(dqLinename.getId());
}
if (!"".equals(dqGddname.getGddgpsjd()) || !"".equals(dqGddname.getGddgpswd())) {
Double pox = Double.parseDouble(dqGddname.getGddgpsjd());
Double poy = Double.parseDouble(dqGddname.getGddgpswd());
Gps gps = PositionUtil.gps84_To_Gcj02(poy, pox);
Double px = gps.getWgLat();
Double py = gps.getWgLon();
dqGddname.setGpsGcjWd(String.valueOf(px));
dqGddname.setGpsGcjJd(String.valueOf(py));
}
EntityWrapper<DqGddname> eWrapper = new EntityWrapper<>();
eWrapper.eq("lineid", dqGddname.getLineid()).eq("gddname", dqGddname.getGddname());
DqGddname temp = dqGddnameService.selectOne(eWrapper);
if (temp != null) {
dqGddname.setId(temp.getId());
}
}
dqGddnameService.insertOrUpdateBatch(list);
return R.ok();
} catch (Exception e) {
logger.error("ERROR:", e);
return R.error("上传失败");
}
}
public static <T> List<T> getImportData(String[] fields, Class<T> clz, HttpServletRequest request)
throws IOException, InvalidFormatException, InstantiationException, IllegalAccessException,
NoSuchMethodException, InvocationTargetException, NoSuchFieldException, SecurityException {
return ExcelImportUtil.getImportData(0, fields, clz, request);
}
public static <T> List<T> getImportData(int sheetIndex, String[] fields, Class<T> clz, HttpServletRequest request)
throws IOException, InvalidFormatException, InstantiationException, IllegalAccessException,
NoSuchMethodException, InvocationTargetException, NoSuchFieldException, SecurityException {
List<T> datas = new ArrayList<>();
Workbook wb = getWorkbookFromRequest(request);
Sheet sheet = wb.getSheetAt(sheetIndex);
// 获取总行数
int rows = sheet.getPhysicalNumberOfRows();
if (rows >= 2) {
for (int start = 1; start < rows; start++) {
// 从第三行开始逐行获取
Row row = sheet.getRow(start);
if (row == null) {
continue;
}
if (fields != null) {
T obj = clz.getDeclaredConstructor().newInstance();
for (int i = 0; i < fields.length; i++) {
Cell cell = row.getCell(i);
String cellValue = getCellValue(cell);
String fieldName = fields[i];
Field field = null;
try {
field = obj.getClass().getDeclaredField(fieldName);
} catch (NoSuchFieldException e) {
field = obj.getClass().getSuperclass().getDeclaredField(fieldName);
}
field.setAccessible(true);
setFieldValue(field, obj, cellValue);
}
datas.add(obj);
}
}
}
return datas;
}
public static <T> List<T> getImportData(String[] fields, Class<T> clz, HttpServletRequest request)
throws IOException, InvalidFormatException, InstantiationException, IllegalAccessException,
NoSuchMethodException, InvocationTargetException, NoSuchFieldException, SecurityException {
return ExcelImportUtil.getImportData(0, fields, clz, request);
}
private static String getCellValue(Cell cell) {
String result = "";
if (cell != null) {
switch (cell.getCellType()) {
// 数字类型 +日期类型
case HSSFCell.CELL_TYPE_NUMERIC:
if (HSSFDateUtil.isCellDateFormatted(cell)) {// 处理日期格式、时间格式
SimpleDateFormat sdf = null;
if (cell.getCellStyle().getDataFormat() == HSSFDataFormat.getBuiltinFormat("h:mm")) {
sdf = new SimpleDateFormat("HH:mm");
} else {// 日期
sdf = new SimpleDateFormat("yyyy-MM-dd");
}
Date date = cell.getDateCellValue();
result = sdf.format(date);
} else if (cell.getCellStyle().getDataFormat() == 58) {
// 处理自定义日期格式:m月d日(通过判断单元格的格式id解决,id的值是58)
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
double value = cell.getNumericCellValue();
Date date = org.apache.poi.ss.usermodel.DateUtil.getJavaDate(value);
result = sdf.format(date);
} else {
HSSFDataFormatter dataFormatter = new HSSFDataFormatter();
result = String.valueOf(dataFormatter.formatCellValue(cell));
}
break;
// String类型
case HSSFCell.CELL_TYPE_STRING:
result = String.valueOf(cell.getStringCellValue());
break;
case HSSFCell.CELL_TYPE_BLANK:
result = "";
default:
result = null;
break;
}
}
return result;
}
private static void setFieldValue(Field field, Object obj, String value) throws IllegalAccessException {
Class<?> typeClass = field.getType();
if (typeClass == int.class || typeClass == Integer.class) {
if (StringUtils.isEmpty(value)) {
field.set(obj, 0);
} else {
field.set(obj, Integer.valueOf(value));
}
} else if (typeClass == short.class || typeClass == Short.class) {
if (StringUtils.isEmpty(value)) {
field.set(obj, 0);
} else {
field.set(obj, Short.valueOf(value));
}
} else if (typeClass == byte.class || typeClass == Byte.class) {
if (StringUtils.isEmpty(value)) {
field.set(obj, 0);
} else {
field.set(obj, Byte.valueOf(value));
}
} else if (typeClass == double.class || typeClass == Double.class) {
if (StringUtils.isEmpty(value)) {
field.set(obj, 0);
} else {
field.set(obj, Double.valueOf(value));
}
} else if (typeClass == long.class || typeClass == Long.class) {
if (StringUtils.isEmpty(value)) {
field.set(obj, 0L);
} else {
field.set(obj, Long.valueOf(value));
}
} else if (typeClass == String.class) {
if (StringUtils.isEmpty(value)) {
field.set(obj, "");
} else {
field.set(obj, value);
}
} else if (typeClass == boolean.class || typeClass == Boolean.class) {
if (StringUtils.isEmpty(value)) {
field.set(obj, false);
} else {
field.set(obj, Boolean.valueOf(value));
}
} else if (typeClass == BigDecimal.class) {
if (StringUtils.isEmpty(value)) {
field.set(obj, BigDecimal.ZERO);
} else {
field.set(obj, new BigDecimal(value));
}
} else if (typeClass == Date.class) {
field.set(obj, StringUtils.isEmpty(value) ? null : DateUtils.parseDate(value));
} else {
field.set(obj, value);
}
}
这里是上传的excel表格数据并转换为java集合对象、然后进行业务逻辑处理判断最后保存到数据库
到此这篇关于Eolink上传文件到Java后台进行处理的文章就介绍到这了,更多相关Eolink上传文件内容请搜索编程网以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程网!
免责声明:
① 本站未注明“稿件来源”的信息均来自网络整理。其文字、图片和音视频稿件的所属权归原作者所有。本站收集整理出于非商业性的教育和科研之目的,并不意味着本站赞同其观点或证实其内容的真实性。仅作为临时的测试数据,供内部测试之用。本站并未授权任何人以任何方式主动获取本站任何信息。
② 本站未注明“稿件来源”的临时测试数据将在测试完成后最终做删除处理。有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341