Pu Zhibing
5 天以前 4c4dc127cdc9c41f2bfb3c138108529856e031b8
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
package com.supersavedriving.user.modular.system.util.bank.parmUtil;
 
import org.apache.commons.lang.StringUtils;
import org.dom4j.Element;
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;
 
import java.io.*;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
 
/**
 * @author wu
 *
 * */
public class Utils {
    /**
     * 将文件转成base64 字符串
     * @return
     * @throws Exception
     */
    public static String encodeBase64File(String path) throws Exception {
        File file = new File(path);
        FileInputStream inputFile = new FileInputStream(file);
        byte[] buffer = new byte[(int) file.length()];
        inputFile.read(buffer);
        inputFile.close();
        return new BASE64Encoder().encode(buffer);
 
    }
 
    /**
     * 将base64字符解码保存文件
     * @param base64Code
     * @param targetPath
     * @throws Exception
     */
    public static void decoderBase64File(String base64Code, String targetPath) {
        try {
            if (StringUtils.isNotBlank(base64Code)) {
                byte[] buffer = new BASE64Decoder().decodeBuffer(base64Code);
                FileOutputStream out = new FileOutputStream(targetPath);
                out.write(buffer);
                out.close();
                System.out.println("文件转换完成!  " + targetPath);
                return;
            }
            System.out.println("文件为空! " + targetPath);
        } catch (Exception e) {
            System.err.println("文件转换错误!"+ e.getMessage());
            e.printStackTrace();
        }
 
    }
 
    /**
     * 压缩文件
     *
     * @param filePath
     * @param zipName
     */
    public static boolean fileZip(String filePath, String zipName) {
 
        int BUFFER = 2048;
 
        FileOutputStream dest = null;
        ZipOutputStream out = null;
        BufferedInputStream origin = null;
        try {
 
            File zipFile = new File(zipName);
 
            if (zipFile.exists()) {
                zipFile.delete();
            }
 
            dest = new FileOutputStream(zipName);
 
            out = new ZipOutputStream(new BufferedOutputStream(dest));
 
            // out.setMethod(ZipOutputStream.DEFLATED);
 
            byte data[] = new byte[BUFFER];
 
            // get a list of files from current directory
 
            File f = new File(filePath);
 
            String files[] = f.list();
 
            for (int i = 0; i < files.length; i++) {
 
                FileInputStream fi = new FileInputStream(filePath + File.separator + files[i]);
 
                origin = new BufferedInputStream(fi, BUFFER);
 
                ZipEntry entry = new ZipEntry(files[i]);
 
                out.putNextEntry(entry);
 
                int count;
 
                while ((count = origin.read(data, 0, BUFFER)) != -1) {
                    out.write(data, 0, count);
                }
            }
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("生成压缩包失败");
        } finally {
            try {
                if (out != null) {
                    out.close();
                }
                if (dest != null) {
                    dest.close();
                }
                if (origin != null) {
                    origin.close();
                }
 
            } catch (IOException e) {
            }
 
        }
        return false;
    }
 
    /**
     * 请求报文参与签名元素排序
     *
     * @param root
     */
    public static String sortSignInfo(Element root)
    {
        List<Element> list = root.elements();
        List<String> signList = new ArrayList<String>();
 
        for (Element e : list)
        {
            if ("SIGN_INFO".equals(e.getName()) || "FILE_CONTENT".equals(e.getName()) || "LIST".equals(e.getName()) || "FILE_CONTENT_F".equals(e.getName()) || "FILE_CONTENT_B".equals(e.getName())) {
                continue;
            }
            if (e.getText() != null)
            {
                signList.add(e.getText());
            }
        }
        //排序
        Collections.sort(signList);
 
        StringBuffer signInfo = new StringBuffer();
 
        for (String sign  : signList)
        {
            signInfo = signInfo.append(sign);
        }
        //若遇到银行返回验证签名未通过,可与银行侧对比该串内容是否一致
        System.out.println("请求字段排序串:"+signInfo);
        return signInfo.toString();
    }
 
    public static String getNowTime26() {
        SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd-HH.mm.ss.SSSSSS");
        return sf.format(new Date());
    }
}