mitao
2025-04-04 d89a42213b4a32535e93185dedf41fe7a7fc1940
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
package com.sinata.system.utils;
 
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URL;
 
public class ImgUtils {
    /**
     * 从URL中获取图片输入流
     * 并创建本地文件
     *
     * @param imageUrl
     * @param savePath
     * @return
     * @throws Exception
     */
    public static File getImageFileFromUrl(String imageUrl, String savePath) throws Exception {
        // 从URL中获取图片输入流
        URL url = new URL(imageUrl);
        InputStream in = url.openStream();
 
 
        // 构建保存路径
        String[] split = imageUrl.split("/");
        String fileName = split[split.length - 1];
        File file = new File(savePath + fileName);
        if (!file.exists()) {
            file.createNewFile();
        }
 
        // 写入文件
        FileOutputStream out = new FileOutputStream(file);
        byte[] buffer = new byte[4096];
        int n = 0;
        while ((n = in.read(buffer)) != -1) {
            out.write(buffer, 0, n);
        }
        out.close();
        in.close();
 
        return file;
    }
 
    /**
     * 根据URL地址获取文件
     * 得到file对象
     *
     * @param path URL网络地址
     * @return File
     */
    public static File getFileByHttpURL(String path) {
        String newUrl = path.split("[?]")[0];
        String[] suffix = newUrl.split("/");
        //得到最后一个分隔符后的名字
        String fileName = suffix[suffix.length - 1];
        File file = null;
        InputStream inputStream = null;
        OutputStream outputStream = null;
        try {
            file = File.createTempFile("report", fileName);//创建临时文件
            URL urlFile = new URL(newUrl);
            inputStream = urlFile.openStream();
            outputStream = new FileOutputStream(file);
 
            int bytesRead = 0;
            byte[] buffer = new byte[8192];
            while ((bytesRead = inputStream.read(buffer, 0, 8192)) != -1) {
                outputStream.write(buffer, 0, bytesRead);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (null != outputStream) {
                    outputStream.close();
                }
                if (null != inputStream) {
                    inputStream.close();
                }
 
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return file;
    }
 
    //顺时针旋转90度(通过交换图像的整数像素RGB 值)
    public static BufferedImage rotateClockwise90(BufferedImage bi) {
        int width = bi.getWidth();
        int height = bi.getHeight();
        BufferedImage bufferedImage = new BufferedImage(height, width, bi.getType());
        for (int i = 0; i < width; i++)
            for (int j = 0; j < height; j++)
                bufferedImage.setRGB(height - 1 - j, width - 1 - i, bi.getRGB(i, j));
        return bufferedImage;
    }
 
    //逆时针旋转90度(通过交换图像的整数像素RGB 值)
    public static BufferedImage rotateCounterclockwise90(BufferedImage bi) {
        int width = bi.getWidth();
        int height = bi.getHeight();
        BufferedImage bufferedImage = new BufferedImage(height, width, bi.getType());
        for (int i = 0; i < width; i++)
            for (int j = 0; j < height; j++)
                bufferedImage.setRGB(j, i, bi.getRGB(i, j));
        return bufferedImage;
    }
 
    //旋转180度(通过交换图像的整数像素RGB 值)
    public static BufferedImage rotate180(BufferedImage bi) {
        int width = bi.getWidth();
        int height = bi.getHeight();
        BufferedImage bufferedImage = new BufferedImage(width, height, bi.getType());
        for (int i = 0; i < width; i++)
            for (int j = 0; j < height; j++)
                bufferedImage.setRGB(width - i - 1, height - j - 1, bi.getRGB(i, j));
        return bufferedImage;
    }
 
    //水平翻转
    public static BufferedImage rotateHorizon(BufferedImage bi) {
        int width = bi.getWidth();
        int height = bi.getHeight();
        BufferedImage bufferedImage = new BufferedImage(width, height, bi.getType());
        for (int i = 0; i < width; i++)
            for (int j = 0; j < height; j++)
                bufferedImage.setRGB(width - i - 1, j, bi.getRGB(i, j));
        return bufferedImage;
    }
 
    //垂直翻转
    public static BufferedImage rotateVertical(BufferedImage bi) {
        int width = bi.getWidth();
        int height = bi.getHeight();
        BufferedImage bufferedImage = new BufferedImage(width, height, bi.getType());
        for (int i = 0; i < width; i++)
            for (int j = 0; j < height; j++)
                bufferedImage.setRGB(i, height - 1 - j, bi.getRGB(i, j));
        return bufferedImage;
    }
 
    public static void main(String[] args) throws Exception {
        //File file = ImgUtils.getImageFileFromUrl("【你的图片网络url】", "【你要保存的本地文件位置】");
        File file = ImgUtils.getFileByHttpURL("【你的图片网络url】");
        System.out.println("file.length() = " + file.length());
    }
}