图片下载
图片下载工具类
package com.zxwa.ntmss.img2text.utils; import cn.hutool.core.util.StrUtil; import com.zxwa.ntmss.process.config.OcrConfig; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import java.io.*; import java.net.HttpURLConnection; import java.net.URL; public class FileUtils { private static Logger LOG = LoggerFactory.getLogger(FileUtils.class); public static void main(String[] args) throws Exception { } public static byte[] netImage2Bytes(String netUrl) { LOG.info("开始下载:" + netUrl); byte[] bytes = null; try { URL url = new URL(netUrl); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setRequestMethod("GET"); conn.setConnectTimeout(5 * 1000); InputStream inStream = conn.getInputStream(); bytes = readInputStream(inStream); } catch (Exception e) { e.printStackTrace(); } return bytes; } public static String netImage2File(String netUrl, String fileLocal) throws Exception { LOG.info("开始下载:" + netUrl); URL url = new URL(netUrl); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setRequestMethod("GET"); conn.setConnectTimeout(5 * 1000); InputStream inStream = conn.getInputStream(); byte[] data = readInputStream(inStream); File imageFile = new File(fileLocal); FileOutputStream outStream = new FileOutputStream(imageFile); outStream.write(data); outStream.close(); return fileLocal; } public static byte[] readInputStream(InputStream inStream) throws Exception {
ByteArrayOutputStream outStream = new ByteArrayOutputStream();
//创建一个Buffer字符串
byte[] buffer = new byte[1024];
//每次读取的字符串长度,如果为-1,代表全部读取完毕
int len = 0;
//使用一个输入流从buffer里把数据读取出来
while ((len = inStream.read(buffer)) != -1) {
//用输出流往buffer里写入数据,中间参数代表从哪个位置开始读,len代表读取的长度
outStream.write(buffer, 0, len);
}
//关闭输入流
inStream.close();
//把outStream里的数据写入内存
return outStream.toByteArray();
}
public static byte[] readInputStream(String filePath) throws Exception {
File file = new File(filePath);
FileInputStream fis = new FileInputStream(file);
return readInputStream(fis);
}
}
imgio
public static BufferedImage read(File input) throws IOException public static BufferedImage read(InputStream input) throws IOException public static BufferedImage read(URL input) throws IOException public static BufferedImage read(ImageInputStream stream) throws IOException
案例
/** * 网络图片下载 * * @param imageUrl 图片url * @param formatName 文件格式名称 * @param localFile 下载到本地文件 * @return 下载是否成功 */ public static boolean downloadImage(String imageUrl, String formatName, File localFile) { boolean isSuccess = false; try { URL url = new URL(imageUrl); isSuccess = ImageIO.write(ImageIO.read(url), formatName, localFile); } catch (Exception e) { e.printStackTrace(); } return isSuccess; }
实际应用:获取图片的大小和尺寸
java获取图片的大小和尺寸,有两种获取的源:
- 一种是读取本地的图片获取大小和尺寸
- 一种是通过服务器上图片的地址获取图片的尺寸
/** * 本地获取 * */ @Test public void testImg2() throws IOException{ File picture = new File("C:/Users/aflyun/Pictures/Camera Roll/1.jpg"); BufferedImage sourceImg =ImageIO.read(new FileInputStream(picture)); System.out.println(String.format("%.1f",picture.length()/1024.0));// 源图大小 System.out.println(sourceImg.getWidth()); // 源图宽度 System.out.println(sourceImg.getHeight()); // 源图高度 }
/** * 获取服务器上的 */ @Test public void getImg() throws FileNotFoundException, IOException{ URL url = new URL("http://img.mall.tcl.com/dev1/0/000/148/0000148235.fid"); URLConnection connection = url.openConnection(); connection.setDoOutput(true); BufferedImage image = ImageIO.read(connection.getInputStream()); int srcWidth = image .getWidth(); // 源图宽度 int srcHeight = image .getHeight(); // 源图高度 System.out.println("srcWidth = " + srcWidth); System.out.println("srcHeight = " + srcHeight); } /** * 获取服务器上的 */ @Test public void testImg1() throws IOException{ InputStream murl = new URL("http://img.mall.tcl.com/dev1/0/000/148/0000148235.fid").openStream(); BufferedImage sourceImg =ImageIO.read(murl); System.out.println(sourceImg.getWidth()); // 源图宽度 System.out.println(sourceImg.getHeight()); // 源图高度 }
故乡明