将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
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)