快速导航

Base64与图片之间的转换

java 2018/02/27 00:02

要使用base64需要配置sun.misc.BASE64Decoder包,Eclipse自动配置的JDK是不存在jar包的,需要自己配置引进

右击工程>properties>Java Build Path>将配置的JRE System Library移除,并Add Library..添加JRE System Library

Base64与图片之间的转换

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;
public class Base64ToImage {
	public void base64ToImage(String base64Str,String imgFilePath) {
		BASE64Decoder decoder=new BASE64Decoder();
		try {
			byte[] b=decoder.decodeBuffer(base64Str);
			for(int i=0;i<b.length;i++) {
				if(b[i]<0) {
					//调整异常数据
					b[i]+=256;
				}
			}
			OutputStream outputStream=new FileOutputStream(imgFilePath);
			outputStream.write(b);
			outputStream.flush();
			outputStream.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

	public void ImageToBase64(String imgFilePath) {
		InputStream inputStream=null;
		byte[] data=null;
		try {
			inputStream = new FileInputStream(imgFilePath);
			data=new byte[inputStream.available()];
			inputStream.read(data);
			inputStream.close();
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		BASE64Encoder encoder=new BASE64Encoder();
		String base64Str=encoder.encode(data);
		System.out.println(base64Str);
	}
}

测试类

public class Test {
	public static void main(String[] args) throws IOException {
		QueryPetsOnSale queryPetsOnSale=new QueryPetsOnSale();
		try {
				String base64Str="/9j/4AAQSkZJRgABAgAAAQABAAD/2wBDAAgGBgcGBQgHBwcJCQgKDBQNDAsLDBkSEw8UHRofHh0aHBwgJC4nICIsIxwcKDcpLDAxNDQ0Hyc5PTgyPC4zNDL/2wBDAQkJCQwLDBgNDRgyIRwhMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMj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Base64ToImage();
				base64ToImage.base64ToImage(base64Str,"E:\\new.img");
				base64ToImage.ImageToBase64("E:\\new.img");
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}
如果觉得我的文章对您有用,请随意打赏。您的支持将鼓励我继续创作!

Powered by codetiler.com