xuhy
2024-02-29 384bd896d3cefd65fb0bdd756ba94fd992dc6c9c
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
package com.ruoyi.common.utils.file;
 
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
 
import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.util.Arrays;
 
/**
 * 图片处理工具类
 *
 * @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 url 地址
     * @return 字节数据
     */
    public static byte[] readFile(String url)
    {
//        InputStream in = 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(in);
//        }
        return null;
    }
}