图片下载
图片下载工具类
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()); // 源图高度 }
故乡明
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 按钮权限的设计及实现
· 25岁的心里话