页面代码:
<html>
</html>
java代码 :
@RequestMapping(method=RequestMethod.POST, value="/imgupload")
public @ResponseBody String uploadImg(HttpServletRequest request, @RequestParam("imgFile") MultipartFile imgFile){
String uploadDir = request.getRealPath("/upload");
String oriName = imgFile.getOriginalFilename();
//文件名后缀处理---start---
String _suffix = oriName.substring(oriName.lastIndexOf(".")+1, oriName.length());
//-----end---
//---重新处理文件名start---
String suffix = oriName.substring(oriName.lastIndexOf("."), oriName.length());
String newFileName = uuid + suffix;
imgFile.transferTo(new File(uploadDir + "/" + newFileName));
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return JSONObject.fromObject(object).toString();
}