Eolink上傳文件到Java后臺(tái)進(jìn)行處理的示例代碼
Eolink上傳文件配置:

接收文件請(qǐng)求并進(jìn)行業(yè)務(wù)處理
@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("線路數(shù)據(jù)不存在");
} 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);
}
/**
* 從上傳文件中解析出數(shù)據(jù)對(duì)象
* @param sheetIndex 第幾個(gè)sheet
* @param fields 解析字段
* @param clz 解析對(duì)象
* @param request
* @return
* @throws IOException
* @throws InvalidFormatException
* @throws InstantiationException
* @throws IllegalAccessException
* @throws NoSuchMethodException
* @throws InvocationTargetException
* @throws NoSuchFieldException
* @throws SecurityException
*/
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);
// 獲取總行數(shù)
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;
}
/**
* 從上傳文件中第一個(gè)sheet解析出數(shù)據(jù)對(duì)象
* @param fields
* @param clz
* @param request
* @return
* @throws IOException
* @throws InvalidFormatException
* @throws InstantiationException
* @throws IllegalAccessException
* @throws NoSuchMethodException
* @throws InvocationTargetException
* @throws NoSuchFieldException
* @throws SecurityException
*/
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()) {
// 數(shù)字類型 +日期類型
case HSSFCell.CELL_TYPE_NUMERIC:
if (HSSFDateUtil.isCellDateFormatted(cell)) {// 處理日期格式、時(shí)間格式
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表格數(shù)據(jù)并轉(zhuǎn)換為java集合對(duì)象、然后進(jìn)行業(yè)務(wù)邏輯處理判斷最后保存到數(shù)據(jù)庫(kù)
到此這篇關(guān)于Eolink上傳文件到Java后臺(tái)進(jìn)行處理的文章就介紹到這了,更多相關(guān)Eolink上傳文件內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Java數(shù)據(jù)類型轉(zhuǎn)換實(shí)例解析
這篇文章主要介紹了Java數(shù)據(jù)類型轉(zhuǎn)換實(shí)例解析,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-11-11
教你用Java在個(gè)人電腦上實(shí)現(xiàn)微信掃碼支付
今天給大家?guī)?lái)的是Java實(shí)戰(zhàn)的相關(guān)知識(shí),文章圍繞著Java在個(gè)人電腦上實(shí)現(xiàn)微信掃碼支付展開,文中有非常詳細(xì)的介紹及代碼示例,需要的朋友可以參考下2021-06-06
ByteArrayInputStream簡(jiǎn)介和使用_動(dòng)力節(jié)點(diǎn)Java學(xué)院整理
ByteArrayInputStream 是字節(jié)數(shù)組輸入流。它繼承于InputStream。這篇文章主要介紹了ByteArrayInputStream簡(jiǎn)介和使用_動(dòng)力節(jié)點(diǎn)Java學(xué)院整理,需要的朋友可以參考下2017-05-05
Springboot整合JwtHelper實(shí)現(xiàn)非對(duì)稱加密
本文主要介紹了Springboot整合JwtHelper實(shí)現(xiàn)非對(duì)稱加密,主要介紹兩種方式,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2022-03-03
SpringBoot?如何將項(xiàng)目打包成?jar?包
這篇文章主要介紹了SpringBoot如何將項(xiàng)目打包成jar包,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-08-08
SpringBoot集成thymeleaf渲染html模板的步驟詳解
這篇文章主要給大家詳細(xì)介紹了SpringBoot集成thymeleaf如何使實(shí)現(xiàn)html模板的渲染,文中有詳細(xì)的代碼示例,具有一定的參考價(jià)值,需要的朋友可以參考下2023-06-06

