600字范文,内容丰富有趣,生活中的好帮手!
600字范文 > GET data:image/jpg;base64 35 37 ....... net::ERR_INVALID_URL

GET data:image/jpg;base64 35 37 ....... net::ERR_INVALID_URL

时间:2019-03-23 10:17:07

相关推荐

GET data:image/jpg;base64 35 37 ....... net::ERR_INVALID_URL

在接收服务器端传过来的图片将其转换成base64格式赋值给img标签时,出现了上面这个错误,通过查找,参考下面这篇文章,找到了答案,表示感谢。

参考文章链接

原因

服务器端没有将要传递过来的图片数据进行base64编码

解决办法

在服务器端进行编码,后台使用的是java,提供以下代码:

1、base64编码工具类,该类我也是参考网上大佬的,然后添加了一些自己的东西,时间太久了,记不得具体出处了,有点不好意思,嘻嘻。

package com.java1234.utils;import java.io.ByteArrayOutputStream;import java.io.File;import java.io.FileInputStream;import java.io.FileOutputStream;import java.io.IOException;import java.io.InputStream;import java.io.OutputStream;import .HttpURLConnection;import .URL;import sun.misc.BASE64Decoder;import sun.misc.BASE64Encoder;@SuppressWarnings("restriction")public class Base64Util {public static void main(String[] args) throws Exception {//本地图片地址String path = "D:"+File.separator+"login.jpg" ;String url = path;//在线图片地址String string = "//element_origin_min_pic/17/03/03/7bf4480888f35addcf2ce942701c728a.jpg";String str = Base64Util.ImageToBase64ByLocal(url);String ste = Base64Util.ImageToBase64ByOnline(string);System.out.println(str);////Base64Util.Base64ToImage(str,"C:/Users/Administrator/Desktop/test1.jpg");////Base64Util.Base64ToImage(ste, "C:/Users/Administrator/Desktop/test2.jpg");}/*** 本地图片转换成base64字符串* @param imgFile图片本地路径* @return** @author ZHANGJL* @dateTime -02-23 14:40:46*/public static String ImageToBase64ByLocal(String imgFile) {// 将图片文件转化为字节数组字符串,并对其进行Base64编码处理InputStream in = null;byte[] data = null;// 读取图片字节数组try {in = new FileInputStream(imgFile);data = new byte[in.available()];in.read(data);in.close();} catch (IOException e) {e.printStackTrace();}// 对字节数组Base64编码BASE64Encoder encoder = new BASE64Encoder();return encoder.encode(data);// 返回Base64编码过的字节数组字符串}/*** 本地图片byte数组转换成base64字符串* @param imgFile图片本地路径* @return** @author ZHANGJL* @dateTime -02-23 14:40:46*/public static String ImageByteArrayToBase64ByLocal(byte[] imgArray) {// 将图片文件转化为字节数组字符串,并对其进行Base64编码处理// 对字节数组Base64编码BASE64Encoder encoder = new BASE64Encoder();return encoder.encode(imgArray);// 返回Base64编码过的字节数组字符串}/*** 在线图片转换成base64字符串* * @param imgURL图片线上路径* @return** @author ZHANGJL* @dateTime -02-23 14:43:18*/public static String ImageToBase64ByOnline(String imgURL) {ByteArrayOutputStream data = new ByteArrayOutputStream();try {// 创建URLURL url = new URL(imgURL);byte[] by = new byte[1024];// 创建链接HttpURLConnection conn = (HttpURLConnection) url.openConnection();conn.setRequestMethod("GET");conn.setConnectTimeout(5000);InputStream is = conn.getInputStream();// 将内容读取内存中int len = -1;while ((len = is.read(by)) != -1) {data.write(by, 0, len);}// 关闭流is.close();} catch (IOException e) {e.printStackTrace();}// 对字节数组Base64编码BASE64Encoder encoder = new BASE64Encoder();return encoder.encode(data.toByteArray());}/*** base64字符串转换成图片* @param imgStrbase64字符串* @param imgFilePath图片存放路径* @return** @author ZHANGJL* @dateTime -02-23 14:42:17*/public static boolean Base64ToImage(String imgStr,String imgFilePath) {// 对字节数组字符串进行Base64解码并生成图片if (StringUtil.isEmpty(imgStr)) // 图像数据为空return false;BASE64Decoder decoder = new BASE64Decoder();try {// Base64解码byte[] b = decoder.decodeBuffer(imgStr);for (int i = 0; i < b.length; ++i) {if (b[i] < 0) {// 调整异常数据b[i] += 256;}}OutputStream out = new FileOutputStream(imgFilePath);out.write(b);out.flush();out.close();return true;} catch (Exception e) {return false;}}}

2、控制层代码

/*** 将图片转换为base64格式*/byte[] img = user.getHeadIcon();if(img!=null) {String headIcon = Base64Util.ImageByteArrayToBase64ByLocal(img);jsonObject.put("headIcon",headIcon);}

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。