lyh
2025-01-13 137d008bd9b7d932160436a3a560b24512f6d1db
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
package org.jeecg.modules.dnc.utils;
 
import cn.hutool.crypto.SmUtil;
import com.alibaba.fastjson.JSONObject;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.HttpEntity;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.FileEntity;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;
import org.jeecg.modules.dnc.utils.file.SM3Util;
import org.jeecg.modules.dnc.dto.SysLogTypeObjectDto;
 
import java.io.File;
import java.net.URLEncoder;
 
/**
 * @author clown
 * * @date 2023/11/14
 */
@Slf4j
public class FileClient {
 
    public static String getToken(String host, String port, String userName, String pwd,String addressToken) throws Exception, Throwable {
        CloseableHttpClient client = null;
        CloseableHttpResponse response = null;
        try {
            ObjectMapper objectMapper = new ObjectMapper();
            JSONObject jsonObject = new JSONObject();
            //构造请求参数appId和password
            jsonObject.put("appId", userName);
            //将密码通过SM3加密
            jsonObject.put("password", SM3Util.encrypt(pwd));
            String str = objectMapper.writeValueAsString(jsonObject);
            StringEntity stringEntity = new StringEntity(str);
            //构造http请求
            String url = "http" + "://" + host + ":" + port + "/" + addressToken;
            HttpPost httpPost = new HttpPost(url);
            //设置Content-Type
            httpPost.setHeader(HTTP.CONTENT_TYPE, "application/json");
            //Content-length会在请求自动自动加上
            //httpPost.setHeader(HTTP.CONTENT_LEN, stringEntity.getContentLength()+"");
            //将构造好的参数放入请求中
            httpPost.setEntity(stringEntity);
            //设置请求超时时间
            RequestConfig requestConfig = RequestConfig.custom()
                    .setConnectTimeout(5000).setConnectionRequestTimeout(5000)
                    .setSocketTimeout(5000).build();
            httpPost.setConfig(requestConfig);
            //发起请求
            client = HttpClients.createDefault();
            response = client.execute(httpPost);
            //解析请求的response
            if (response.getStatusLine().getStatusCode() == 200) {
                HttpEntity entity = response.getEntity();
                String result = EntityUtils.toString(entity, "UTF-8");
                JSONObject jobject = JSONObject.parseObject(result);
                int code = jobject.getIntValue("code");
                String token = jobject.getString("token");
                String message = jobject.getString("msg");
                if (200 != code) {
                    throw new Exception("appAuth Error,code[" + code + "],message[" + message + "]");
                } else {
                    //若请求成功,返回token
                    return token;
                }
            } else {
                throw new Exception("appAuth Error:" + response.getStatusLine().toString());
            }
        } catch (Throwable e) {
            throw e;
        } finally {
            if (response != null) {
                try {
                    response.close();
                } catch (Throwable t) {
                }
            }
            if (client != null) {
                try {
                    client.close();
                } catch (Throwable t) {
                }
            }
        }
    }
 
 
   /* public static void main(String[] args) {
        try {
            String token = getToken("127.0.0.1","8299","admin","123");
        } catch (Throwable throwable) {
            throwable.printStackTrace();
        }
    }*/
 
    /**
     *
     * @param host
     * @param port
     * @param token
     * @param remoteFilePath
     * @param localFilePath
     * @param addressUploadFile
     * @return
     * @throws Exception
     * @throws Throwable
     */
    public static String uploadFile(String host, String port, String token, String fileName,String remoteFilePath, String localFilePath,String addressUploadFile) throws Exception, Throwable {
        CloseableHttpClient client = null;
        CloseableHttpResponse response = null;
        SysLogTypeObjectDto objectName = new SysLogTypeObjectDto();
        objectName.setResult("失败");
        try {
            //拼接请求的url
            String url = "http" + "://" + host + ":" + port + "/" + addressUploadFile;
            //构造请求
            HttpPost httpPost = new HttpPost(url);
            //设置Content-Type为文件流格式
            httpPost.setHeader(HTTP.CONTENT_TYPE, "application/octet-stream");
            //设置header的请求参数
            httpPost.addHeader("Accept", "*/*");
            httpPost.addHeader("Accept-Encoding", "UTF-8");
            httpPost.setHeader("Token", token);
            //FileName需要经过URLEncoder加码,防止中文乱码
            httpPost.setHeader("FileName", URLEncoder.encode(remoteFilePath, "UTF-8"));
            /*文件大小*/
            httpPost.setHeader("WenjianqIcaoren", "徐业植");//文件起草人
            httpPost.setHeader("Shenpiren", "张世贵");//审批人
            httpPost.setHeader("Miji", URLEncoder.encode("内部", "UTF-8"));
            httpPost.addHeader("Baomiqixian","10,12,30");
            httpPost.addHeader("Qianfaren", URLEncoder.encode("张世贵", "UTF-8"));//签发人
            File file = new File(localFilePath);
            if (file == null || !file.exists()){
                httpPost.addHeader("Wenjiansanliezhi", URLEncoder.encode("sm3", "UTF-8"));
            } else {
                String sm3 = SmUtil.sm3(file);
                httpPost.addHeader("Wenjiansanliezhi", URLEncoder.encode(sm3, "UTF-8"));
            }
 
            httpPost.addHeader("Beizhu", URLEncoder.encode("无", "UTF-8"));
            //将对应上传的本地文件解析成文件流放入body
            httpPost.setEntity(new FileEntity(new File(localFilePath)));
            //根据文件大小设置超时时间
            int timeout = (int) ((new File(localFilePath).length() / (1000)) * 2 + 2000);
            //设置超时时间
            RequestConfig requestConfig = RequestConfig.custom()
                    .setConnectTimeout(5000).setConnectionRequestTimeout(5000)
                    .setSocketTimeout(timeout).build();
            httpPost.setConfig(requestConfig);
            //请求构造完成,发起请求
            client = HttpClients.createDefault();
            response = client.execute(httpPost);
            //解析response
            if (response.getStatusLine().getStatusCode() == 200) {
                HttpEntity entity = response.getEntity();
                System.out.println(entity);
                String result = EntityUtils.toString(entity, "UTF-8");
                JSONObject jobject = JSONObject.parseObject(result);
                int code = jobject.getIntValue("code");
                String message = jobject.getString("msg");
                if (200 != code) {
                    //throw new Exception("uploadFile Error,code[" + code + "],message[" + message + "]");
                    log.error( "code: "+ code +"  message: " + message);
                    if (response != null) {
                        try {
                            response.close();
                        } catch (Throwable t) {
                        }
                    }
                    if (client != null) {
                        try {
                            client.close();
                        } catch (Throwable t) {
                        }
                    }
                    return "失败";
                }
                return "成功";
            } else {
                //throw new Exception("uploadFile Error:" + response.getStatusLine().toString());
                log.error( "uploadFile Error:" + response.getStatusLine().toString());
                objectName.setResult(response.getStatusLine().toString());
                if (response != null) {
                    try {
                        response.close();
                    } catch (Throwable t) {
                    }
                }
                if (client != null) {
                    try {
                        client.close();
                    } catch (Throwable t) {
                    }
                }
                return "失败";
            }
        } catch (Throwable e) {
            log.error( e.getMessage());
            if (response != null) {
                try {
                    response.close();
                } catch (Throwable t) {
                }
            }
            if (client != null) {
                try {
                    client.close();
                } catch (Throwable t) {
                }
            }
            return "失败";
        } finally {
 
            if (response != null) {
                try {
                    response.close();
                } catch (Throwable t) {
                }
            }
            if (client != null) {
                try {
                    client.close();
                } catch (Throwable t) {
                }
            }
            return "成功";
        }
    }
 
}