spring上传
?/**
? * @作者:朱湘鄂
? * @说明:该方法主要:保存图片到指定目录,获取图片存储路径
? * @param request
? *??????????? 传入当前请求对象
? * @param names
? *??????????? 图片存储的二级目录
? * @param sid
? *??????????? 图片存储的三级目录
???? * @param attrName
? *??????????? 图片存储的三级目录
? *???????????
? * @返回值:返回图片存储的存储路径
? */
?@SuppressWarnings("deprecation")
?public static String uploadImage(HttpServletRequest request, String names, String sid ,String attrName) {
??try {
???MultipartHttpServletRequest multipartRequest? =? (MultipartHttpServletRequest) request;
????? CommonsMultipartFile file = (CommonsMultipartFile) multipartRequest.getFile(attrName);
????? String imagePath=null;
????? if(file.getOriginalFilename()!=null && !("".equals(file.getOriginalFilename()))){
????//调用上传图片工具类,传入参数,获得图片保存路径
????String path = "/uploads/" + names + "/" + sid + "/";
????String root = request.getRealPath(path);
????File dir = new File(root);
????if (dir.exists() == false) {
?????dir.mkdirs();
????}
????String fileFileName = file.getOriginalFilename();
????String name = fileFileName.toString();// 得到上传文件的原名称
????// 得到图片的后缀名
????name = name.substring(name.indexOf('.'));
????// 图片重命名
????String rename = sid + name;
????File uploadedFile = new File(dir + "/" + rename);
????byte[] bytes = file.getBytes();
????FileCopyUtils.copy(bytes, uploadedFile);
????imagePath = path + rename;
????? }
????? return imagePath;
??} catch (Exception e) {
???return "系统异常,图片上传异常!" + e;
??}
?}