protected RespBody check(MultipartFile media, int type) { if (!ResourceType.check(type)) { return RespBody.fail(200); } // 文件大小小于1KB或才大于1G if (media.getSize() < 1 << 10 || media.getSize() > 1 << 30) { return RespBody.fail(201); } return RespBody.success(); }
/** * 文件上传 * * @param type 上传类型 1:头像, 2:相册, 9:语音 * @return */ @RequestMapping(method = RequestMethod.POST) @ResponseBody public RespBody upload(@RequestParam(value = "type") int type, @RequestBody MultipartFile media) { RespBody respBody = check(media, type); if (respBody.hasError()) { return respBody; } Resource resource = null; try { resource = service.upload(media, type, getUid()); } catch (IOException e) { logger.error("上传文件错误." + e); } return respBody.setBody(resource); }
/** * 按类型获取文件 * * @param type 1:头像, 2:相册, 9:语音 * @return */ @RequestMapping(value = "/byType") @ResponseBody public RespBody getByType(@RequestParam(value = "type", required = true) Integer type) { if (type == 0) { // type = ResourceType.HEAD; throw new ServiceException(201); } return RespBody.success().setBody(service.getByType(type)); }
@Override protected RespBody check(Resource t) { return RespBody.success(); }
/** * 获取文件 * * @param uid * @return */ @RequestMapping @ResponseBody public RespBody get(@RequestParam(value = "uid", required = true) Integer uid) { return RespBody.success().setBody(service.get(uid)); }