@RequestMapping(value = "/image", method = {RequestMethod.POST, RequestMethod.GET}) public QsMap uploadPicture(HttpServletRequest request, HttpServletResponse response) throws IOException { QsMap qsMap = new QsMap();//自定义Map //获取上传文件的主文件名与扩展名 MultipartHttpServletRequest req = (MultipartHttpServletRequest) request; MultipartFile multipartFile = req.getFile("file"); //获取上传时的名称 String fileName = multipartFile.getOriginalFilename(); //根据文件扩展名得到文件类型 String type = getFileType(fileName); //存放图片的绝对路径 String realPath = "J:/apache-tomcat-7.0.52/webapps/uploadimage/aftersale"; //格式化时间戳 SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss"); String nowTime = sdf.format(new Date().getTime()); //裁剪图片名称 // String originalFirstName = multipartFile.getOriginalFilename(); // String picFirstName = originalFirstName.substring(0, originalFirstName.indexOf(".")); //取得图片的格式后缀 String originalLastName = multipartFile.getOriginalFilename(); String picLastName = originalLastName.substring(originalLastName.lastIndexOf(".")); try { File dir = new File(realPath); //如果文件目录不存在,创建文件目录 if (!dir.exists()) { dir.mkdir(); } //拼接 名字+时间戳+后缀 String picName = nowTime + UUID.randomUUID() + picLastName; File file = new File(realPath,picName); multipartFile.transferTo(file); // qsMap.put("file",file); //全路径 qsMap.put("file","uploadimage/aftersale/" + picName); } catch (IOException e) { e.printStackTrace(); } catch (IllegalStateException e) { e.printStackTrace(); } return qsMap; }
优化版
@ApiOperation(value = "图片上传",notes = "UploadPic") @RequestMapping(value = "/image", method = {RequestMethod.POST, RequestMethod.GET}) public QsMap uploadPicture(HttpServletRequest request, HttpServletResponse response) throws IOException { QsMap qsMap = new QsMap(); //获取上传文件的主文件名与扩展名 MultipartHttpServletRequest req = (MultipartHttpServletRequest) request; //对应前端的upload的name参数"file" MultipartFile multipartFile = req.getFile("file"); //获取上传时的名称 String fileName = multipartFile.getOriginalFilename(); //存放图片的绝对路径 String realPath = "J:\\apache-tomcat-7.0.52\\webapps\\uploadimage\\aftersale"; //格式化时间戳 SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss"); //System.currentTimeMillis() 是直接调用本地方法,而 new Date().getTime() 确还要创建一个Date对象,降低了效率和占用了内存(虽然损耗很小)。 String nowTime = sdf.format(System.currentTimeMillis()); //取得图片的格式后缀 String originalLastName = multipartFile.getOriginalFilename(); String picLastName = originalLastName.substring(originalLastName.lastIndexOf(".")); //判断上传格式 if(picLastName.equals(".jpg") || picLastName.equals(".gif") || picLastName.equals(".png") || picLastName.equals(".jpeg")){ try { File dir = new File(realPath); //如果文件目录不存在,创建文件目录 if (!dir.exists()) { dir.mkdirs(); //目录的操作与删除 mkdir():创建一级目录 mkdirs():创建多级目录 delete():删除目录或文件 } //拼接 名字+时间戳+后缀 String picName = nowTime + UUID.randomUUID() + picLastName; File file = new File(realPath,picName); multipartFile.transferTo(file); //全路径 qsMap.put("file","http://60.30.201.10:8093/PhotoService/storeroomUploadImage/aftersale/" + picName); } catch (IOException e) { e.printStackTrace(); } catch (IllegalStateException e) { e.printStackTrace(); } }else{ qsMap.put("上传了不合法的图片后缀!",picLastName); } return qsMap; }