package org.jeecg.modules.mdc.util; 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.system.util.SM3Util; 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 address) 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 + "/" + address; HttpPost httpPost = new HttpPost(url); //设置Content-Type httpPost.setHeader(HTTP.CONTENT_TYPE, "application/json"); //Content-length会在请求自动自动加上 //将构造好的参数放入请求中 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) { } } } } //uploadFile() /** * * @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; 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", URLEncoder.encode("谭政", "UTF-8")); httpPost.setHeader("Shenpiren", URLEncoder.encode("陈海波", "UTF-8")); 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.exists()){ httpPost.addHeader("Wenjiansanliezhi", "1"); } else { String sm3 = SmUtil.sm3(file); httpPost.addHeader("Wenjiansanliezhi", sm3); } 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(); // log.info(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) { // log.info( "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 { // log.info( 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) { //throw e; System.out.println( e.toString()); 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) { } } } } }