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 "成功";
|
}
|
}
|
|
}
|