图片转换为base64 前台预览

业务需求 word转换成在线预览 结合这里是word转图片 也可以直接转换为在线预览

public static String parseFileToBase64_PNG1(InputStream inputStream, int pageNum) throws Exception {
		// String png_base64 = "";
		List<BufferedImage> bufferedImages = new ArrayList<BufferedImage>();
		BufferedImage image = null;
		ByteArrayOutputStream baos = new ByteArrayOutputStream();// io流
		bufferedImages = wordToImg1(inputStream, pageNum);
		image = mergeImage(false, bufferedImages);
		ImageIO.write(image, "png", baos);// 写入流中

		byte[] bytes = baos.toByteArray();// 转换成字节
		BASE64Encoder encoder = new BASE64Encoder();
		String png_base64 = encoder.encodeBuffer(bytes).trim();// 转换成base64串
		png_base64 = png_base64.replaceAll("\n", "").replaceAll("\r", "");// 删除
		
		return png_base64;
	}

评论

Your browser is out-of-date!

Update your browser to view this website correctly. Update my browser now

×