欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

java类型转换

程序员文章站 2022-03-14 15:55:38
...

(1)MultipartFile->BufferedImage

BufferedImage bufferedImage=ImageIO.read(imageFile.getInputStream());

(2)BufferedImage–>byte[]

/**
	 * 类型转换,BufferedImage-->byte[]
	 * @param bufferedImage BufferedImage 原始image
	 * @param Suffix   图片类型,如jpg,png
	 * @return byte[]  字节数组
	 */
	private static byte[] BufferedImageToBytes(BufferedImage bufferedImage,String Suffix) throws IOException {
		ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
		ImageIO.write(bufferedImage, Suffix, byteArrayOutputStream);
		byteArrayOutputStream.flush();
		byte[] bytes = byteArrayOutputStream.toByteArray();
		byteArrayOutputStream.close();
		return bytes;
	}

(3)byte[]–>List

List<Byte> list = new ArrayList<>();
		for (byte b : bytes) {
			list.add(b);
		}