Java

您所在的位置:网站首页 java中image Java

Java

2023-06-06 14:30| 来源: 网络整理| 查看: 265

项目场景: 该功能是向第三方站点接口拉取的资源,拉取内容包括图片、文章内容、标题等。 1 问题描述 由于我们项目含有ssl证书,用户访问门户网站时,·浪潮云· 将默认http访问地址默认转为https(包括请求地址),第三方接口没有ssl证书 访问只能通过http访问。 目前后端访问接口是完全没问题的,但是拉取下来的图片为 http😕/www.my.xxx.cn/xx/xxx.jpg 但存入数据库后返回前端无法显示 1 2 方案1 - 图片转换base64: 首先通过后端访问获取到的图片地址进行base64转换 一般处理图片跨域类问题都可使用base64方式,但是还需文件大小来判断是否会影响到性能

@Test public void test() throws Exception { String url = "http://www.my.xxx.cn/xx/xxx.jpg"; String str1 = url.substring(0, url.indexOf(".")); String str2 = url.substring(str1.length()+1, url.length()); System.out.println(str2); String s1 = convertImageToBase64(url, null); System.out.println(s1); //返回base64 } ---- /** * 将url压缩为指定大小 * @param imageUrl * @param sizeLimit * @return * @throws IOException */ public static String convertImageToBase64(String imageUrl, Integer sizeLimit) throws IOException { String imgType=imageUrl.substring(imageUrl.lastIndexOf(".")+1); //默认上限为500k if (sizeLimit == null) { sizeLimit = 500; } sizeLimit = sizeLimit * 1024; String base64Image; DataInputStream dataInputStream = null; ByteArrayOutputStream outputStream = null; ByteArrayInputStream inputStream = null; try { //从远程读取图片 URL url = new URL(imageUrl); dataInputStream = new DataInputStream(url.openStream()); outputStream = new ByteArrayOutputStream(); byte[] buffer = new byte[2048]; int length; while ((length = dataInputStream.read(buffer)) > 0) { outputStream.write(buffer, 0, length); } byte[] context = outputStream.toByteArray(); //将图片数据还原为图片 inputStream = new ByteArrayInputStream(context); //注意:使用ImageIO.read()方法可能会导致图片数据丢失,导致图片变色 BufferedImage image = ImageIO.read(inputStream); int imageSize = context.length; int type = image.getType(); int height = image.getHeight(); int width = image.getWidth(); BufferedImage tempImage; //判断文件大小是否大于size,循环压缩,直到大小小于给定的值 while (imageSize > sizeLimit) { //将图片长宽压缩到原来的90% height = new Double(height * 0.9).intValue(); width = new Double(width * 0.9).intValue(); tempImage = new BufferedImage(width, height, type); // 绘制缩小后的图 tempImage.getGraphics().drawImage(image, 0, 0, width, height, null); //重新计算图片大小 outputStream.reset(); ImageIO.write(tempImage, imgType, outputStream); imageSize = outputStream.toByteArray().length; } //将图片转化为base64并返回 byte[] data = outputStream.toByteArray(); //此处一定要使用org.apache.tomcat.util.codec.binary.Base64,防止再linux上出现换行等特殊符号 base64Image = Base64.encodeBase64String(data); } catch (Exception e) { //抛出异常 throw e; } finally { if (dataInputStream != null) { try { dataInputStream.close(); } catch (IOException e) { e.printStackTrace(); } } if (outputStream != null) { try { outputStream.close(); } catch (IOException e) { e.printStackTrace(); } } if (inputStream != null) { try { inputStream.close(); } catch (IOException e) { e.printStackTrace(); } } } return base64Image; }

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 方案2 - todo: 思路:通过保存本地重新生成图片url地址返回

问题二 - 解析富文本图片转base64替换原图片地址: @Test public void test34() throws IOException { String str = “



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3