搜索
首页类库下载java类库java获取远程网络图片文件流、压缩保存到本地

java获取远程网络图片文件流、压缩保存到本地

1.获取远程网路的图片

     /**
 * 根据地址获得数据的字节流
 *
 * @param strUrl
 *            网络连接地址
 * @return
 */
public static byte[] getImageFromNetByUrl(String strUrl) {
    try {
        URL url = new URL(strUrl);
        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
        conn.setRequestMethod("GET");
        conn.setConnectTimeout(5 * 1000);
        InputStream inStream = conn.getInputStream();// 通过输入流获取图片数据
        byte[] btImg = readInputStream(inStream);// 得到图片的二进制数据
        return btImg;
    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}
 /**
 * 根据地址获得数据的字节流
 *
 * @param strUrl
 *            本地连接地址
 * @return
 */
public static byte[] getImageFromLocalByUrl(String strUrl) {
    try {
        File imageFile = new File(strUrl);
        InputStream inStream = new FileInputStream(imageFile);
        byte[] btImg = readInputStream(inStream);// 得到图片的二进制数据
        return btImg;
    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}
   /**
 * 从输入流中获取数据
 *
 * @param inStream
 *            输入流
 * @return
 * @throws Exception
 */
public static byte[] readInputStream(InputStream inStream) throws Exception {
    ByteArrayOutputStream outStream = new ByteArrayOutputStream();
    byte[] buffer = new byte[10240];
    int len = 0;
    while ((len = inStream.read(buffer)) != -1) {
        outStream.write(buffer, 0, len);
    }
    inStream.close();
    return outStream.toByteArray();
}  

2.将网络读取的文件流转成本地文件

       byte[] btImg1 = ImageUtil.getImageFromNetByUrl(fileUrl1);
if (null != btImg1 && btImg1.length > 0) {
    logger.debug("读取到:" + btImg1.length + " 字节");
    ImageUtil.writeImageToDisk(btImg1, fileZipUrl1);
} else {
    logger.debug("没有从该连接获得内容");
}
 
byte[] btImg2 = ImageUtil.getImageFromNetByUrl(fileUrl2);
if (null != btImg2 && btImg2.length > 0) {
    logger.debug("读取到:" + btImg2.length + " 字节");
    ImageUtil.writeImageToDisk(btImg2, fileZipUrl2);
} else {
    logger.debug("没有从该连接获得内容");
}
/**
 * 将图片写入到磁盘
 *
 * @param img
 *            图片数据流
 * @param fileName
 *            文件保存时的名称
 */
public static void writeImageToDisk(byte[] img, String zipImageUrl) {
    try {
        File file = new File(zipImageUrl);
        FileOutputStream fops = new FileOutputStream(file);
        fops.write(img);
        fops.flush();
        fops.close();
        System.out.println("图片已经写入"+zipImageUrl);
    } catch (Exception e) {
        e.printStackTrace();
    }
}

3、压缩本地图片

import java.io.*; 
import java.util.Date; 
import java.awt.*; 
import java.awt.image.*; 
import javax.imageio.ImageIO; 
import com.sun.image.codec.jpeg.*; 
/**
 * 图片压缩处理
 */ 
public class ImgCompress { 
    private Image img; 
    private int width; 
    private int height;  
    /**
     * 构造函数
     */ 
    public ImgCompress(String fileName) throws IOException { 
        File file = new File(fileName);// 读入文件 
        img = ImageIO.read(file);      // 构造Image对象 
        width = img.getWidth(null);    // 得到源图宽 
        height = img.getHeight(null);  // 得到源图长 
    } 
    /**
     * 按照宽度还是高度进行压缩
     * @param w int 最大宽度
     * @param h int 最大高度
     */ 
    public void resizeFix(int w, int h) throws IOException { 
        if (width / height > w / h) { 
            resizeByWidth(w); 
        } else { 
            resizeByHeight(h); 
        } 
    } 
    /**
     * 以宽度为基准,等比例放缩图片
     * @param w int 新宽度
     */ 
    public void resizeByWidth(int w) throws IOException { 
        int h = (int) (height * w / width); 
        resize(w, h); 
    } 
    /**
     * 以高度为基准,等比例缩放图片
     * @param h int 新高度
     */ 
    public void resizeByHeight(int h) throws IOException { 
        int w = (int) (width * h / height); 
        resize(w, h); 
    } 
    /**
     * 强制压缩/放大图片到固定的大小
     * @param w int 新宽度
     * @param h int 新高度
     */ 
    public void resize(int w, int h) throws IOException { 
        // SCALE_SMOOTH 的缩略算法 生成缩略图片的平滑度的 优先级比速度高 生成的图片质量比较好 但速度慢 
        BufferedImage image = new BufferedImage(w, h,BufferedImage.TYPE_INT_RGB );  
        image.getGraphics().drawImage(img, 0, 0, w, h, null); // 绘制缩小后的图 
        File destFile = new File("C:/Users/Administrator/Desktop/147.jpg"); 
        FileOutputStream out = new FileOutputStream(destFile); // 输出到文件流 
        // 可以正常实现bmp、png、gif转jpg 
        JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out); 
        encoder.encode(image); // JPEG编码 
        out.close(); 
    }
@SuppressWarnings("deprecation") 
public static void main(String[] args) throws Exception { 
    System.out.println("开始:" + new Date().toLocaleString()); 
    ImgCompress imgCom = new  ImgCompress("C:/Users/Administrator/Desktop/1479209533362.jpg"); 
    imgCom.resizeFix(285, 380); 
    System.out.println("结束:" + new Date().toLocaleString()); 
}
}

完~

声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn

热AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover

AI Clothes Remover

用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool

Undress AI Tool

免费脱衣服图片

Clothoff.io

Clothoff.io

AI脱衣机

AI Hentai Generator

AI Hentai Generator

免费生成ai无尽的。

热门文章

R.E.P.O.能量晶体解释及其做什么(黄色晶体)
3 周前By尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.最佳图形设置
3 周前By尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.如果您听不到任何人,如何修复音频
3 周前By尊渡假赌尊渡假赌尊渡假赌
WWE 2K25:如何解锁Myrise中的所有内容
3 周前By尊渡假赌尊渡假赌尊渡假赌

热工具

Atom编辑器mac版下载

Atom编辑器mac版下载

最流行的的开源编辑器

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

功能强大的PHP集成开发环境

Dreamweaver Mac版

Dreamweaver Mac版

视觉化网页开发工具

SecLists

SecLists

SecLists是最终安全测试人员的伙伴。它是一个包含各种类型列表的集合,这些列表在安全评估过程中经常使用,都在一个地方。SecLists通过方便地提供安全测试人员可能需要的所有列表,帮助提高安全测试的效率和生产力。列表类型包括用户名、密码、URL、模糊测试有效载荷、敏感数据模式、Web shell等等。测试人员只需将此存储库拉到新的测试机上,他就可以访问到所需的每种类型的列表。

WebStorm Mac版

WebStorm Mac版

好用的JavaScript开发工具