1.获取远程网路的图片

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

     /**

 * 根据地址获得数据的字节流

 *

 * @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;

}

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

   /**

 * 根据地址获得数据的字节流

 *

 * @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;

}

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

   /**

 * 从输入流中获取数据

 *

 * @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.将网络读取的文件流转成本地文件

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

        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("没有从该连接获得内容");

}

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

/**

 * 将图片写入到磁盘

 *

 * @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、压缩本地图片

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

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, 00, 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(); 

    }

1

2

3

4

5

6

7

@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(285380); 

    System.out.println("结束:" new Date().toLocaleString()); 

}

1

}

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

  1. java从远程url文件流读取文件并下载到本地

    java从远程url文件流读取文件,且下载到本地,写个循环可实现批量 import java.io.*; import java.net.HttpURLConnection; import java. ...

  2. php如何接收ap端上传的图片_用php获取远程图片并把它保存到本地的代码

    用php获取远程图片并把它保存到本地的代码 更新时间:2008年04月07日 20:43:26   作者: Function: 获取远程图片并把它保存到本地 确定您有把文件写入本地服务器的权限变量说明 ...

  3. php 无法获取远程图片,本地连接受限制或无法连接 用php获取远程图片并把它保存到本地的代码...

    function GrabImage($url,$filename="") { if($url==""):return false;endif; if($fil ...

  4. Java 获取url地址文件流

    /*** 根据url下载文件流* @param urlStr* @return*/ public static InputStream getInputStreamFromUrl(String url ...

  5. java获取下载链接文件流并上传至OSS

    InputStream inputStream = new URL("下载链接地址").openStream(); MultipartFile file = new MockMul ...

  6. java怎么获取服务器文件夹,java获取远程服务器的文件夹

    java获取远程服务器的文件夹 内容精选 换一换 工具中所有涉及上传文件功能的,如果需要上传的文件大于1GB或者解压后超过剩余磁盘空间的一半,则需要释放磁盘空间或手动将文件上传至服务器,其他情况可通过 ...

  7. java解压服务器文件夹,java获取远程服务器上的文件夹

    java获取远程服务器上的文件夹 内容精选 换一换 安装X722板载网卡驱动软件包,使裸金属服务器支持在v5服务器上下发.其他类型服务器可跳过此步骤.本文以Windows Server 2016为例, ...

  8. java 获取服务器上文件,java获取远程服务器上的文件

    java获取远程服务器上的文件 内容精选 换一换 已成功登录Java性能分析.待安装Guardian的服务器已开启sshd.待安装Guardian的服务器已安装JRE,JRE版本要求为Huawei J ...

  9. java 获取classpath下文件多种方式

    java 获取classpath下文件多种方式 一:properties下配置 在resources下定义server.properties register.jks.path=classpath\: ...

最新文章

  1. 基于keras的深度学习基本概念讲解
  2. Web API Test Client 1.2.0
  3. 继承和多态的区别[发现记混了,区别下]
  4. python文件无法关闭_Python脚本无法正常终止
  5. C# 9 record 并非简单属性 POCO 的语法糖
  6. android刷新时的圆形动画_Android动画篇(一):圆形进度条CircleProgressBar
  7. PHP过滤HTML标签的三种方法
  8. linux程序设计第四版中文pdf下载地址
  9. Trying to create too many scroll contexts. Must be less than or equal to: [500]
  10. TiDB Data Migration (DM)介绍
  11. java爬取中央气象台天气预报
  12. Sensor感应器介绍
  13. matlab filter zf,什么是MATLAB函数过滤器中’zf’的内容
  14. 汉诺塔游戏(Python)
  15. Mqtt精髓系列之精简之道
  16. JavaScript---BOM基础
  17. 基于LiDAR里程计和先验地图的定位方法
  18. ESP32-C3入门教程 环境篇⑤——Flash Download Tools 固件烧录工具的使用
  19. Ubuntu系列:Ubuntu安装deepin wine QQ, 微信...
  20. 告诉你C盘里的每个文件夹都是干嘛的,哪个可以删除,哪些不能碰

热门文章

  1. java struts1_struts1.x
  2. wps表格里面计算机在哪里,WPS的Word居然还有计算神器?在哪里能找到又是怎么进行计算呢?...
  3. vlookup两个条件匹配_vlookup,你还是只会基础的单条件查找?
  4. 数据结构-队列2-链式存储
  5. mysql zrm_mysql数据库备份—ZRM
  6. 【Linux】查看文件内容的相关命令总结
  7. HGOI20190707 题解
  8. PyCherm的常用快捷键总结
  9. HTML5笔记1——HTML5的发展史及标签的改变
  10. YUV格式转换RGB(基于opencv)