图片URl转为输入流和图片base64转为输入流 InputStream

直接上代码 :

/**
	 * url 转 InputStream
	 *
	 * @param fileUrl
	 * @return
	 * @throws IOException
	 */
	public static InputStream url2InputStream(String fileUrl) throws IOException {
		URL url = new URL(fileUrl);
		URLConnection conn = url.openConnection();
		conn.setConnectTimeout(30 * 1000);  // 连接超时:30s
		conn.setReadTimeout(1 * 1000 * 1000); // IO超时:1min
		conn.connect();

		// 创建输入流读取文件
		InputStream in = conn.getInputStream();
		return in;
	}


	/**
	 * bast64 转 输入流
	 * @return
	 */
	public static InputStream base64ToInputstream(String bast64) throws IOException {
		//将字符串转换为byte数组
		byte[] bytes = new BASE64Decoder().decodeBuffer(bast64.trim());
		//转化为输入流
		return new ByteArrayInputStream(bytes);
	}

 

你可能感兴趣的:(图片URl转为输入流和图片base64转为输入流 InputStream)