/** * 上传图片文件 * * @param request http 请求 * @param file 照片文件 * @throws IOException */ @ResponseBody @RequestMapping(value = "/upload", method = RequestMethod.POST) public ResponseMessage upload( HttpServletRequest request, @RequestParam(value = "img") MultipartFile file, String title) throws IOException { Pair<String, String> pair = uploadImg(request, file); picRepository.save( Pic.newOne().setPath(pair.getValue()).setTitle(getTitle(pair.getKey(), title))); return new ResponseMessage() .set("filename", pair.getValue()) .set("url", WebMvcConfig.getUrl(pair.getValue())); }
/** * 上传图片文件,一次可以上传多张 * * @param request 请求 * @param files 照片文件 * @throws IOException */ @ResponseBody @RequestMapping(value = "/uploadMultiple", method = RequestMethod.POST) public ResponseMessage upload( HttpServletRequest request, @RequestParam(value = "img") MultipartFile[] files) throws IOException { List<String> filenames = new ArrayList<>(); List<Pic> pics = new ArrayList<>(); for (MultipartFile file : files) { Pair<String, String> pair = uploadImg(request, file); filenames.add(pair.getValue()); Pic pic = Pic.newOne().setTitle(getTitle(pair.getKey(), "")).setPath(pair.getValue()); pics.add(pic); } picRepository.save(pics); return new ResponseMessage() .set("count", String.valueOf(filenames.size())) .set("filenames", JsonHelper.toJson(filenames)); }