文件转base64

1.根据URL下载文件(pdf/图片)并转换为Base64

package com.jiayou.peis.official.account.biz.utils;

import org.apache.commons.io.IOUtils;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.util.Base64;

public class FileUtil {
	/**
	 * 根据URL下载文件(pdf/图片)并转换为Base64
	 * @param fileUrl
	 * @return
	 */
	public static String transUrlToBase64(String fileUrl){
	    try {
	        URL url = new URL(fileUrl);
	        InputStream inputStream = url.openStream();
	        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
	        IOUtils.copy(inputStream,outputStream);
	        byte[] byteArray = outputStream.toByteArray();
	        return Base64.getEncoder().encodeToString(byteArray);
	    } catch (IOException e) {
	        throw new RuntimeException(e);
	    }
	}
	
	public static void main(String[] args) {
	    String s = transUrlToBase64("http://11122/22/22/1.pdf");
	    System.out.println("dddddd=========:"+s);
	}
}

你可能感兴趣的:(java,java,笔记)