上傳檔案檢測有錯誤返回

來源:互聯網
上載者:User

標籤:bool   商品資料   span   long   []   filename   color   date()   tty   

    @RequestMapping("/import")    @ResponseBody    public Map<String, Object> importItem(@RequestParam("fileName") MultipartFile uploadFile,            HttpServletRequest request, HttpServletResponse response,Long warehouseId){        if(warehouseId==null||warehouseId.longValue()<=0){            return APIUtil.toMap("0200", false,"倉庫id不可為空!");        }        if (uploadFile.isEmpty()) {            return APIUtil.toMap("0200", false,"上傳檔案不可為空!");        }        String fileName = uploadFile.getOriginalFilename();        if (!fileName.endsWith("xlsx") && !fileName.endsWith("xls")) {            return APIUtil.toMap("0200", false,"上傳檔案錯誤");        }                InputStream inputStream = null;        try {            inputStream = uploadFile.getInputStream();            boolean isExcel2003 = true;            if (fileName.endsWith("xlsx")) {                isExcel2003 = false;            }            List<Map<String, Object>> list=ReadExcelUtils.readLocationItem(inputStream, isExcel2003);            if(list==null||list.size()==0){                return APIUtil.toMap("0200", false,"上傳檔案資料為空白");            }            int res=0;                        List<StorageStock> stocks=new ArrayList<>();                        for (int i = 0; i < list.size(); i++) {                Map<String, Object> mapObj=list.get(i);                String barCode=mapObj.get("barCode")==null?"0":mapObj.get("barCode").toString();                String locationName=mapObj.get("locationName")==null?"0":mapObj.get("locationName").toString();                String msg="";                                StorageStock stock=new StorageStock();                                WarehouseLocation location=warehouseLocationService.get(warehouseId, locationName);                if(location==null){                    res+=1;                    msg="庫位:["+mapObj.get("locationName")+"]不存在"+",";                }else{                    stock.setWarehouseLocationId(location.getId());                    if(location.getType().intValue()!=WarehouseLoaclEnum.FIXED.getType()){                        msg="庫位:["+locationName+"]不是固定庫位"+",";                    }                }                StorageStock item=storageService.findByBarCode(warehouseId, barCode);                if(item==null){                    res+=1;                    msg+="條碼:["+mapObj.get("barCode")+"]不存在";                }else{                    stock.setItemId(item.getItemId());                    StorageStock stock1=storageService.getStorageStock(warehouseId, item.getItemId());                    if(stock1!=null){                        msg+="條碼:["+barCode+"]已綁定庫位";                    }                }                mapObj.put("msg", msg);                                stocks.add(stock);            }            if(res>0){                String[] titles={"商品條碼(必填)","固定庫位標識(必填)","返回結果"};                HSSFWorkbook wb = new HSSFWorkbook();                HSSFSheet sheet = wb.createSheet("sheet0");                HSSFRow row = sheet.createRow(0);                HSSFCellStyle cellStyle = wb.createCellStyle();                HSSFFont font = wb.createFont();font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);                cellStyle.setFont(font);                row.setRowStyle(cellStyle);                sheet.setColumnWidth(0, 20 * 256);                sheet.setColumnWidth(1, 20 * 256);                sheet.setColumnWidth(2, 20 * 256);                for (int i = 0; i< titles.length ;i++) {                    HSSFCell cell = row.createCell(i);                    cell.setCellValue(titles[i]);                    cell.setCellStyle(cellStyle);                }                for (int i = 0; i < list.size(); i++) {                    row = sheet.createRow((int) i + 1);                      Map<String, Object> map = list.get(i);                    row.createCell(0).setCellValue(map.get("barCode")+"");                    row.createCell(1).setCellValue(map.get("locationName")+"");                    row.createCell(2).setCellValue(map.get("msg")+"");                }                 FileOutputStream out = null;                    String url = "";                    try {                        String xlsxName = "匯入失敗商品資料";                        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmm");                        String dateTime = sdf.format(new Date());                        xlsxName = xlsxName +dateTime + ".xlsx";                        String codedFileName = new String( xlsxName.getBytes(), "UTF-8");                        url = "/admin_excel/"+codedFileName;                        out = new FileOutputStream(ScpEnum.SCP_EXCEL_PATH.getValue()+codedFileName);                        wb.write(out);                    } catch (IOException e) {                        e.printStackTrace();                    } finally {                        try {                            out.close();                        } catch (IOException e) {                            e.printStackTrace();                        }                    }                    Map<String, Object> eMap = new HashMap<>();                    eMap.put("url",url);                    return APIUtil.toMap("0200", false, eMap);            }else{                int result=storageService.saveLocationItem(stocks);                if(result>0){                    return APIUtil.toMap("0000", false,"綁定成功");                }            }        } catch (Exception e) {            return APIUtil.toMap("0200", false,"系統異常");        }        return APIUtil.toMap("0200", false,"操作失敗");    }

 

上傳檔案檢測有錯誤返回

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.