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));
	}
}

转载于:https://my.oschina.net/u/1052192/blog/896518

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值