提交multipart/form-data类型数据

2022-07-27 12:29:42

提交multipart/form-data类型数据

@(spring mvc)[文件上传|附加信息]

举例:工行签名证书上传保存

说明:框架使用的是spring mvc 4,接口测试工具使用postman,本文要实现的功能有:

  • 文件上传 :文件上传信息填写在form-data区域,字段名即文件名,文件内容通过postman控件选择,存储在mysql数据库中类型为BLOB,对应的java类属性为byte[]
  • 文件存储 :直接将文件存储到数据库,而非存储文件的URL;
  • 其他信息 :同时上传文件的其他附加信息,也一并保存到数据库。

postman测试请求

postman接口地址:
https://www.getpostman.com/collections/3ff9e71924f3facec051

postman接口


controller源码

@RequestMapping(value ="business/{businessId}/{company}/mode", method = RequestMethod.POST,
            produces = MediaType.APPLICATION_JSON_VALUE)public ResponseEntitycreate(@PathVariable("businessId") Long businessId,
                                 @PathVariable("company") String company,
                                 @RequestParam("name") String name,
                                 @RequestParam("password") String privatePassword,
                                 @RequestParam("account") String account,
                                 HttpServletRequest request) {try {boolean passed = CompanyUtil.checkCompany(company);if (passed) {
                PayModeDto modeDto =new PayModeDto();
                modeDto.setName(name);
                modeDto.setBusinessId(businessId);
                modeDto.setPrivatePassword(privatePassword);
                modeDto.setAccount(account);

                DiskFileItemFactory factory =new DiskFileItemFactory();
                ServletFileUpload upload =new ServletFileUpload(factory);
                List items = upload.parseRequest(request);for (Object object : items) {
                    FileItem fileItem = (FileItem) object;
                    System.out.println(fileItem.toString());if (fileItem.getFieldName().equals("publicKey")) {
                        modeDto.setPublicKey(fileItem.get());
                    }elseif (fileItem.getFieldName().equals("privateKey")) {
                        modeDto.setPrivateKey(fileItem.get());
                    }
                }
                logger.info("客户端将创建的支付配置:{}", JSONObject.toJSONString(modeDto));if (company.equals(IConstant.ICBC)) {
                    logger.info("将要添加的是工商银行支付配置");
                    payModeService.createICBCPayMode(modeDto);
                    logger.info("成功添加工行支付配置");
                }elseif (company.equals(IConstant.MOBAO)) {
                    logger.info("将要添加的是魔宝支付配置");
                    payModeService.createMobaoPayMode(modeDto);
                    logger.info("成功添加魔宝支付配置");
                }returnnew ResponseEntity(new Bingo(), HttpStatus.CREATED);
            }else {
                logger.info("校验支付公司简称路径变量{}不通过", company);
                JSONObject error =new JSONObject();
                error.put("ErrorCode",400);
                error.put("ErrorMsg","公司简称填写错误");returnnew ResponseEntity(error, HttpStatus.BAD_REQUEST);
            }
        }catch (FileUploadException e) {
            e.printStackTrace();
            logger.info(e.getMessage());
            JSONObject error =new JSONObject();
            error.put("ErrorCode",400);
            error.put("ErrorMsg", e.getMessage());returnnew ResponseEntity(error, HttpStatus.BAD_REQUEST);
        }catch (CoreException e) {
            e.printStackTrace();
            logger.info(JSONObject.toJSONString(e));returnnew ResponseEntity(e, HttpStatus.BAD_REQUEST);
        }catch (Exception e) {
            e.printStackTrace();
            logger.info(e.getMessage());
            JSONObject error =new JSONObject();
            error.put("ErrorCode",400);
            error.put("ErrorMsg", e.getMessage());returnnew ResponseEntity(error, HttpStatus.BAD_REQUEST);
        }
    }


  • 作者:羽轩GM
  • 原文链接:https://blog.csdn.net/musuny/article/details/46315143
    更新时间:2022-07-27 12:29:42