Combined with the easyUI front-end ajax upload file component
Code behind
controller
@RequestMapping("/excelUploadItemList") @ResponseBody public CommonResponse excelUploadItemList(@RequestParam MultipartFile upFile, HttpServletRequest request, HttpServletResponse response) { String result = null; try { if (request instanceof MultipartHttpServletRequest) { String filename = upFile.getOriginalFilename(); String tempPath; logger.info("生成路径"); //String fsRoot = ConfigPool.getConfigValue("storage.fs.root"); String fsRoot = "C:\\TempFile"; tempPath = fsRoot + File.separator + "temp" + File.separator + filename; File f = new File(fsRoot + File.separator + "temp"); if (!f.exists()) { f.mkdirs(); } File newFile = new File(tempPath); upFile.transferTo(newFile); if (ExcelPublic.checkArray (tempPath)) { // The first line has a value // read the value of the first line logger.info ("Start reading the file!" ); ArrayList <String> al = new ArrayList <String> (); // Read the first line, the title field line al = ExcelPublic.readExcelFirstline (tempPath); // [Product ID, product title, product selling point, product price] System .out.println ("al:" + al); // Read all content in excel String [] [] content = ExcelPublic.dyadicArray (tempPath, 1, 0 ); // [[Product ID, product title, product Selling point, commodity price], [123456, K20Pro, large memory, 2799], [234567, iPhoneSE, new machine, 3299]] System.out.println ("content:" + content); logger.info ( "Get column after reading file" ); Map <String, Integer> locationMap = new HashMap <String, Integer> (); // The location of the data to be inserted String [] finalFieldArr = {"Product ID", "Product Title", "Product Selling Point", "Product Price " }; // Read the title field and its location locationMap = readLocation (tempPath, finalFieldArr); // {Product title = 1, Product selling point = 2, Product ID = 0, Product price = 3} System.out .println ("locationMap:" + locationMap); if (locationMap.containsValue (-1 )) { result = "The header is incorrect, the correct header is:" + Arrays.asList (finalFieldArr); throw new Exception (result); } logger.info ( "Import into actual file" ); result = this.itemService.excelUploadItemList(al, content, locationMap); } } } catch (Exception e) { // Failed result = e.toString (); e.printStackTrace (); CommonResponse commonresponse = new CommonResponse(); commonresponse.setSuccFlag(false); commonresponse.setMsg(result); return commonresponse; } // 成功 CommonResponse commonresponse = new CommonResponse(); commonresponse.setSuccFlag(true); response.setContentType("text/plain; charset=UTF-8"); return commonresponse; }
service
@Override public String excelUploadItemList(ArrayList<String> al, String[][] content, Map<String, Integer> locationMap) { Map<String, String[]> itemMap = new HashMap<>(); for (int i = 1; i < content.length; i++) { String itemId = content[i][locationMap.get("商品ID")]; itemMap.put(itemId, content[i]); } // 生成时间 String date = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()); // sql String sql = "insert into tb_item (id, title, sell_point, price, num , cid, status, created, updated) values(?, ?, ?, ?, 500, 560, 1, '" + date +"','" + date + "')"; // 参数 List<Object[]> batchArgs = new ArrayList<Object[]>(); // 封装参数 for (int i = 1; i < content.length; i++) { String[] rowData = content[i]; Object[] batchArg = new Object[]{rowData[0], rowData[1], rowData[2], rowData[3]}; batchArgs.add(batchArg); } String result = ""; try { jdbcTemplate.batchUpdate(sql, batchArgs); result = "success"; } catch (DataAccessException e) { e.printStackTrace (); } return result; }
CommonResponse.java
/** * CommonResponse.java * Created at 2015-04-14 * Created by zhaozhong * / package com.alphajuns.ssm.util; /** * <p>ClassName: BaseObject</p> * <p> Description: General response object, mainly provided to the return value of rest api </ p> * <p>Date: Apr 14, 2015</p> */ public class CommonResponse extends BaseObject { /** * */ private static final long serialVersionUID = 5382262170831616150L; /** * 0: SUCCESS; 1: FAILED */ private int succFlag = 0; /** * */ private String msg = ""; /** * General data */ private Object data; /** * Extended data */ private Object dataExt; /** * */ private Object rows = ""; /** * */ private long total = 0; /** * success */ public static final int SUCCESS = 0; /** * fail */ public static final int FAIL = 1; public String getMsg() { return this.msg; } public void setMsg(String msg) { this.msg = msg; } public int getSuccFlag() { return this.succFlag; } public void setSuccFlag(int succFlag) { this.succFlag = succFlag; } public void setSuccFlag(boolean succFlag){ if(succFlag){ this.succFlag = 0; }else{ this.succFlag = 1; } } public Object getData() { return this.data; } public void setData(Object data) { this.data = data; } /** * <p>Description: getRows</p> * @return the rows */ public Object getRows() { return this.rows; } /** * <p>Description: setRows</p> * @param rows the rows to set */ public void setRows(Object rows) { this.rows = rows; } /** * <p>Description: getTotal</p> * @return the total */ public long getTotal() { return this.total; } /** * <p>Description: setTotal</p> * @param total the total to set */ public void setTotal(long total) { this.total = total; } /** * <p>Description: getDataExt</p> * @param data ext to get */ public Object getDataExt() { return dataExt; } /** * <p>Description: setDataExt</p> * @param data ext to set */ public void setDataExt(Object dataExt) { this.dataExt = dataExt; } }