CZT
2023-08-21 998770ccd5fac7417c5b680b03b28c439cc31561
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
package com.fzzy.api.utils;
 
 
import org.apache.commons.lang.time.DateFormatUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
 
import com.fzzy.data.ConfigData;
 
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Date;
 
/**
 * 文件工具类
 *
 * @author chen
 * @date 2022-09-26 16:06
 */
@Service(FileUtil.BEAN_ID)
public class FileUtil {
 
    public static final String BEAN_ID = "base.fileUtil";
 
    @Autowired
    private ConfigData configData;
 
    public String getInoutFilePath(Date date) {
        if (null == date) {
            date = new Date();
        }
 
        String basePath = configData.getImgPath() + "INOUT/"
                + DateFormatUtils.format(date, "yyyyMM") + "/";
 
        File file = new File(basePath);
        if (!file.exists()) {
            file.mkdirs();
        }
        return basePath;
    }
 
    /**
     * 获取图片的二进制流
     * @param imgPath
     * @return
     */
    @SuppressWarnings("resource")
    public String imgToIo(String imgPath){
        //图片转化为二进制
        byte[] imageBytes = null;
        try {
 
            FileInputStream fileInputStream = new FileInputStream(new File(imgPath));
            imageBytes = new byte[fileInputStream.available()];
            fileInputStream.read(imageBytes);
        } catch (IOException e) {
 
            System.out.println(e);
            return null;
        }
        return UnicodeByteToStr(imageBytes);
    }
 
    private static String UnicodeByteToStr(byte[] b){
        StringBuilder sb = new StringBuilder();
        for(int i=0;i<b.length;i++) {
            sb.append(String.format("%02x", b[i]));
        }
        return sb.toString();
    }
}