java将hdfs上的图片转为base64返回给前端

 将hdfs上面的图片转化为base64传给前端

public static void main(String[] args) throws IOException {
	String hdfsPath = "hdfs://192.168.0.0:9200/tmp/test/tupian.jpg";
	Path path = new Path(hdfsPath);
	Configuration configuration = new Configuration();
	FSDataInputStream fsDataInputStream = null;
	FileSystem fileSystem = null;
	// 定义一个字符串用来存储文件内容
	fileSystem = path.getFileSystem(configuration);
	fsDataInputStream = fileSystem.open(path);

	int oop = (int) ((HdfsDataInputStream)fsDataInputStream).getVisibleLength();
	byte[] b = new byte[oop];

	fsDataInputStream.read(b);
	String res = getImageBase64(b,"jpg")
}

//type:jpg
private static String getImageBase64(byte[] context,String type) {
	String imageData = "";
	BASE64Encoder encoder = new BASE64Encoder();
	// 通过base64来转化图片
	imageData = encoder.encode(context);
	imageData = imageData.replaceAll("\n", "").replaceAll("\r", "");
	imageData = "data:image/"+type+";base64,"+imageData;
	return imageData;
}

https://blog.csdn.net/qq_41212530/article/details/104294956

你可能感兴趣的:(java基础,java)