Blame view

ruoyi-common/src/main/java/com/ruoyi/common/utils/file/ImageUtils.java 2.7 KB
01873447   RuoYi   Excel注解支持Image图片导出
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
  package com.ruoyi.common.utils.file;

  

  import java.io.ByteArrayInputStream;

  import java.io.ByteArrayOutputStream;

  import java.io.FileInputStream;

  import java.io.InputStream;

  import java.net.URL;

  import java.net.URLConnection;

  import java.util.Arrays;

  import org.apache.poi.util.IOUtils;

  import org.slf4j.Logger;

  import org.slf4j.LoggerFactory;

  import com.ruoyi.common.config.RuoYiConfig;

  import com.ruoyi.common.constant.Constants;

  import com.ruoyi.common.utils.StringUtils;

  

  /**

   * 图片处理工具类

   *

   * @author ruoyi

   */

  public class ImageUtils

  {

      private static final Logger log = LoggerFactory.getLogger(ImageUtils.class);

  

      public static byte[] getImage(String imagePath)

      {

          InputStream is = getFile(imagePath);

          try

          {

              return IOUtils.toByteArray(is);

          }

          catch (Exception e)

          {

              log.error("图片加载异常 {}", e);

              return null;

          }

          finally

          {

              IOUtils.closeQuietly(is);

          }

      }

  

      public static InputStream getFile(String imagePath)

      {

          try

          {

              byte[] result = readFile(imagePath);

              result = Arrays.copyOf(result, result.length);

              return new ByteArrayInputStream(result);

          }

          catch (Exception e)

          {

              log.error("获取图片异常 {}", e);

          }

          return null;

      }

  

      /**

       * 读取文件为字节数据

       * 

       * @param key 地址

       * @return 字节数据

       */

      public static byte[] readFile(String url)

      {

          InputStream in = null;

          ByteArrayOutputStream baos = null;

          try

          {

              if (url.startsWith("http"))

              {

                  // 网络地址

                  URL urlObj = new URL(url);

                  URLConnection urlConnection = urlObj.openConnection();

                  urlConnection.setConnectTimeout(30 * 1000);

                  urlConnection.setReadTimeout(60 * 1000);

                  urlConnection.setDoInput(true);

                  in = urlConnection.getInputStream();

              }

              else

              {

                  // 本机地址

                  String localPath = RuoYiConfig.getProfile();

                  String downloadPath = localPath + StringUtils.substringAfter(url, Constants.RESOURCE_PREFIX);

                  in = new FileInputStream(downloadPath);

              }

              return IOUtils.toByteArray(in);

          }

          catch (Exception e)

          {

              log.error("获取文件路径异常 {}", e);

              return null;

          }

          finally

          {

              IOUtils.closeQuietly(baos);

          }

      }

  }