上传文件检测有错误返回

Posted lanliying

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了上传文件检测有错误返回相关的知识,希望对你有一定的参考价值。

    @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,"操作失败");
    }

 

以上是关于上传文件检测有错误返回的主要内容,如果未能解决你的问题,请参考以下文章

封装一个文件上传并检测安全性重命名的脚本

将存储在内存中的文件上传到s3

php表单上传图片到七牛云存储并返回地址……求具体流程~有代码更好

Flask 文件上传返回错误 405 方法不允许

java Ftp上传创建多层文件的代码片段

第一次检测到错误后返回错误代码