WebUploader实现图片上传功能

  //上传图片(阿里云) 控制层

  @ResponseBody

  @RequestMapping(value = "/upload", method = RequestMethod.POST)

  public String upload(@RequestParam(value = "f", required = false, defaultValue = "") String f,

  String folder,

  MultipartHttpServletRequest request, HttpServletResponse response) {

  return commonService.upload(folder, f, request, response);

  }

  //业务处理

  public String upload(String folder, String f, MultipartHttpServletRequest request, HttpServletResponse response) {

  log.info("CommonService上传图片(腾讯云):newFilename11:" + f);

  JSONObject jsonObject = new JSONObject();

  String result = "";

  try {

  Iterator itr = request.getFileNames();

  MultipartFile file = null;

  String name = "";

  while (itr.hasNext()) {

  file = request.getFile(itr.next());

  name = file.getOriginalFilename();

  String newFilenameBase = UUID.randomUUID().toString().replace("-", "");

  String originalFileExtension = file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf("."));

  String newFilename = newFilenameBase + originalFileExtension;

  if (!StringUtils.isBlank(f)) {

  if (f.startsWith(","))

  f.substring(1);

  newFilename = (f.contains(".") ? f.substring(0, f.lastIndexOf(".")) : f) + ".jpg";

  }

  newFilename = "img/" + newFilename;

  AliyunOssUtils.upLoadFile(newFilename,PropertyUtil.getValue("folder"),file);

  //TentunOssUtils.uploadImage(file, newFilename);

  /*if (PropertyUtil.getValue("pic_url").endsWith("/")) {

  jsonObject.put("src", PropertyUtil.getValue("pic_url") + newFilename);

  } else {

  jsonObject.put("src", PropertyUtil.getValue("pic_url") + "/" + newFilename);

  }*/

  jsonObject.put("src",CommonUtils.setImageVideoUrlSign(newFilename));

  jsonObject.put("oname", name);

  jsonObject.put("nname", newFilename);

  //删除暂存在root里的图片

  File tempFile = new File(name);

  if (tempFile != null && tempFile.exists()) {

  tempFile.delete();

  }

  }

  } catch (Exception e) {

  log.error("上传图片异常", e);

  jsonObject.put("e", e.getMessage());

  }

  result = jsonObject.toString();

  return result;

  }