spring base64轉圖片

/** * app 更新頭像 * * @param base64Code * @param origName * @param req * @param res * @param indexImgSize * @throws Exception * @author linwk 2016年6月13日 */ @RequestMapping(value = "/uploadImg.htm", method = RequestMethod.POST) public void uploadFrontByBase64(String base64Code, HttpServletRequest req, HttpServletResponse res, Integer indexImgSize) throws Exception { try { if (StringUtils.isBlank(base64Code)) { throw new BusinessException("圖片不能爲空!"); } Long userId = RequestUtils.getCurrentUserId(req); UserVO user = userService.getUserVOById(userId);數據庫

// boolean a = GenerateImage(base64Code);
		// 商品圖片存放路徑
		String storageFilePath;
		String storageFileName;
		String base64Img = null;
		// 獲取圖片後綴
		String imgSuffix = "jpg";
		String pattern = "data:image/(.*?);base64";
		Pattern p = Pattern.compile(pattern);
		Matcher m = p.matcher(base64Code);
		ArrayList<String> strs = Lists.newArrayList();
		if (m.find()) {
			strs.add(m.group(1));
		}
		for (String s : strs) {
			imgSuffix = s;
		}
		if (base64Code.indexOf("base64,") > 0) {
			String[] base64LiStrings = base64Code.split("base64,");
			// 獲取圖片內容
			base64Img = base64LiStrings[1];
		} else {
			base64Img = base64Code;
		}

		storageFilePath = "user/" + user.getId() + "/" + DateUtil.getNowDateToString("yyyy") + "/" + DateUtil.getNowDateToString("MM") + "/"
				+ DateUtil.getNowDateToString("dd");
		storageFileName = UUID.randomUUID().toString();

		storageFileName += "." + imgSuffix;
		// 原圖id
		Long oldImgId = user.getPhotoId();

		// 更新我的頭像(只容許傳一張)
		List<Accessory> fileList = new ArrayList<Accessory>(0);

		FileUploadVO fileUpload = new FileUploadVO();
		BASE64Decoder decoder = new BASE64Decoder();

		final byte[] decoderBytes = decoder.decodeBuffer(base64Img);

		MultipartFile file = new MultipartFile() {
			@Override
			public void transferTo(File dest) throws IOException, IllegalStateException {

			}

			@Override
			public boolean isEmpty() {
				return false;
			}

			@Override
			public long getSize() {
				return 0;
			}

			@Override
			public String getOriginalFilename() {
				return "";
			}

			@Override
			public String getName() {
				return "";
			}

			@Override
			public InputStream getInputStream() throws IOException {
				return null;
			}

			@Override
			public String getContentType() {
				return null;
			}

			@Override
			public byte[] getBytes() throws IOException {
				return decoderBytes;
			}
		};

		fileUpload.setFile(file);
		fileUpload.setStorageFilePath(storageFilePath);
		fileUpload.setStorageFileName(storageFileName);
		// fileUpload.setUserId(user.getId());

		// 上傳文件到硬盤
		Accessory acc = UploadFileUtil.uploadFile(fileUpload);
		// 保存文件到數據庫
		acc = fileService.insertFile(acc);
		fileList.add(acc);
		// 更新用戶
		user.setPhotoId(acc.getId());
		userService.updateUser(user);
		// 從數據庫刪除原文件
		fileService.deleteFile(oldImgId);
		// 從硬盤刪除原文件
		UploadFileUtil.deleteFile(fileService.getById(oldImgId));
		ResponseOutputUtils.renderJson(res, MessageDTO.newInstance(true,"上傳成功", null));

	} catch (BusinessException ex) {
		ResponseOutputUtils.renderJson(res, MessageDTO.newInstance(false, ex.getMessage(), null));
	} catch (Exception ex) {
		logger.error("上傳頭像異常", ex);
		ResponseOutputUtils.renderJson(res, MessageDTO.newInstance(false, "上傳頭像異常", null));
	}
}
相關文章
相關標籤/搜索