提交multipart/form-data类型数据

提交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 ResponseEntity create(@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());
                    } else if (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("成功添加工行支付配置");
                } else if (company.equals(IConstant.MOBAO)) {
                    logger.info("将要添加的是魔宝支付配置");
                    payModeService.createMobaoPayMode(modeDto);
                    logger.info("成功添加魔宝支付配置");
                }
                return new ResponseEntity(new Bingo(), HttpStatus.CREATED);
            } else {
                logger.info("校验支付公司简称路径变量{}不通过", company);
                JSONObject error = new JSONObject();
                error.put("ErrorCode", 400);
                error.put("ErrorMsg", "公司简称填写错误");
                return new 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());
            return new ResponseEntity(error, HttpStatus.BAD_REQUEST);
        } catch (CoreException e) {
            e.printStackTrace();
            logger.info(JSONObject.toJSONString(e));
            return new 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());
            return new ResponseEntity(error, HttpStatus.BAD_REQUEST);
        }
    }

我的联系方式

  • Q Q:1250052380
  • 邮箱:1250052380@qq.com
  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

羽轩GM

您的鼓励是我创作的动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值