将线上图片读取尺寸信息,用于本地化存储 获取图片信息 try { HttpURLConnection con = null; BufferedImage image = null; URL imgUrl = new URL(url); try { // 支持https trustAllHosts(); HttpsURLConnection https = (HttpsURLConnection)imgUrl.openConnection(); if (imgUrl.getProtocol().toLowerCase().equals("https")) { https.setHostnameVerifier(DO_NOT_VERIFY); con = https; } else { con = (HttpURLConnection)imgUrl.openConnection(); } con.setDoOutput(true); image = ImageIO.read(con.getInputStream()); } catch (IOException e) { e.printStackTrace(); }
获取图片尺寸
int srcWidth = image .getWidth(); // 源图宽度 int srcHeight = image .getHeight(); // 源图高度
/** * 线上图片转为byte数组 * * @param path * @return */ public static byte[] onlineImage2byte(String path) throws IOException { byte[] data = null; URL url = null; InputStream input = null; try{ url = new URL(path); HttpsURLConnection httpUrl = (HttpsURLConnection) url.openConnection(); httpUrl.connect(); httpUrl.getInputStream(); input = httpUrl.getInputStream(); }catch (Exception e) { e.printStackTrace(); return null; } ByteArrayOutputStream output = new ByteArrayOutputStream(); byte[] buf = new byte[1024]; int numBytesRead = 0; while ((numBytesRead = input.read(buf)) != -1) { output.write(buf, 0, numBytesRead); } data = output.toByteArray(); output.close(); input.close(); return data; }