@(spring mvc)[文件上传|附加信息]
工行签名证书上传保存
说明:框架使用的是spring mvc 4,接口测试工具使用postman,本文要实现的功能有:
- 文件上传 :文件上传信息填写在form-data区域,字段名即文件名,文件内容通过
postman
控件选择,存储在mysql
数据库中类型为BLOB,对应的java类属性为byte[]- 文件存储 :直接将文件存储到数据库,而非存储文件的URL;
- 其他信息 :同时上传文件的其他附加信息,也一并保存到数据库。
postman接口地址:
https://www.getpostman.com/collections/3ff9e71924f3facec051
@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
原文:http://blog.csdn.net/musuny/article/details/46315143