base64图片转图片保存操作

public String handlerbase64Img(String base64Data, HttpServletRequest request) {
		try {
			String dataPrix = "";
			String data = "";
			if (base64Data == null || "".equals(base64Data)) {
				throw new Exception("上传失败,上传图片数据为空");
			} else {
				String[] d = base64Data.split("base64,");
				if (d != null && d.length == 2) {
					dataPrix = d[0];
					data = d[1];
				} else {
					throw new Exception("上传失败,数据不合法");
				}
			}
			String suffix = "";
			if ("data:image/jpeg;".equalsIgnoreCase(dataPrix)) {// data:image/jpeg;base64,base64编码的jpeg图片数据
				suffix = ".jpg";
			} else if ("data:image/x-icon;".equalsIgnoreCase(dataPrix)) {// data:image/x-icon;base64,base64编码的icon图片数据
				suffix = ".ico";
			} else if ("data:image/gif;".equalsIgnoreCase(dataPrix)) {// data:image/gif;base64,base64编码的gif图片数据
				suffix = ".gif";
			} else if ("data:image/png;".equalsIgnoreCase(dataPrix)) {// data:image/png;base64,base64编码的png图片数据
				suffix = ".png";
			} else {
				throw new Exception("上传图片格式不合法");
			}
			String tempFileName = UUID.randomUUID().toString() + suffix;

			// 因为BASE64Decoder的jar问题,此处使用spring框架提供的工具包
			byte[] bs = Base64Utils.decodeFromString(data);
			try {
				// 使用apache提供的工具类操作流
				System.out.println(request.getServletContext().getRealPath("/upload"));
				FileUtils.writeByteArrayToFile(
						new File(request.getServletContext().getRealPath("/upload"), tempFileName), bs);
			} catch (Exception ee) {
				throw new Exception("上传失败,写入文件失败," + ee.getMessage());
			}
			return "success";
		} catch (Exception e) {
			return "error";
		}
	}

猜你喜欢

转载自blog.csdn.net/zhangkang65/article/details/80805164