• 请求工具类和base64转成MultipartFile工具类


    请求工具类 hutool包HttpRequest带请求头和请求体发送post请求

    将MultipartFile文件保存到本地

    1. package org.jeecg.modules.quota.utils;
    2. import cn.hutool.core.bean.BeanUtil;
    3. import cn.hutool.http.HttpRequest;
    4. import cn.hutool.json.JSONArray;
    5. import cn.hutool.json.JSONObject;
    6. import cn.hutool.json.JSONUtil;
    7. import org.jeecg.common.constant.CommonConstant;
    8. import org.jeecg.common.util.MinioUtil;
    9. import org.jeecg.common.util.ZOSUtil;
    10. import org.jeecg.modules.quota.controller.DataSynchronismController;
    11. import org.jeecg.modules.quota.entity.SysCertPdf;
    12. import org.jeecg.modules.quota.service.ISysCertPdfService;
    13. import org.springframework.beans.factory.annotation.Autowired;
    14. import org.springframework.web.client.RestTemplate;
    15. import org.springframework.web.multipart.MultipartFile;
    16. import java.io.*;
    17. import java.util.*;
    18. import java.util.stream.Collectors;
    19. import java.util.stream.Stream;
    20. /**
    21. * @Author leitao
    22. * @Date 2023/9/21 15:28
    23. * @description: 请求工具类
    24. * @Title: RequestYxeUtil
    25. * @Package org.jeecg.modules.quota.utils
    26. */
    27. public class RequestYxeUtil {
    28. // private static final String token_url = "http://23:89/xxxx/xxxx/token?clientId=2c9642bd21c0000&clientSecret=6abc4457bc60e0d2e0b08a8b9828c6e9d86250458b4de51bf82d9604abeb987d3";
    29. //token(线上)
    30. private static final String token_url = "http://23:89/xxxx/xxxx/token?clientId=2c9fb2d21c0000&clientSecret=6ab3e8291b9af828c6e9d86250458b4de51bf82d9604abeb987d3";
    31. //获取渔船照片证据
    32. private static final String XeblCertificate_url = "http://23:89/xxx/xxxx/xxxxx/XeblCertificate";
    33. //token()
    34. private static final String XeblCyzp_token_url = "http://28:87/xxx//xxxx/token";
    35. //获取船员照片
    36. private static final String XeblCyzp_url = "http://28:87/xxx//xxxx/CyService/getCyPhoto";
    37. /**
    38. * 限额xxxx证书
    39. *
    40. * @param cm
    41. * @param ycid
    42. * @throws IOException
    43. */
    44. public static ArrayList xeblCertificate(String cm, String ycid, String uploadType) throws Exception {
    45. ArrayList sysCertPdfs = new ArrayList<>();
    46. String requestResult = HttpRequest.post(token_url).execute().body();
    47. JSONObject jsonResult = JSONUtil.parseObj(requestResult);
    48. String code = jsonResult.getStr("code");
    49. if ("0".equals(code)) {
    50. String result = jsonResult.getStr("result");
    51. JSONObject resultJson = JSONUtil.parseObj(result);
    52. String accessToken = resultJson.getStr("accessToken");
    53. //json格式
    54. HashMap paramMap = new HashMap<>();
    55. paramMap.put("shipName", cm);
    56. paramMap.put("shipInfoId", ycid);
    57. String xeblCertificateResult = HttpRequest.post(XeblCertificate_url)
    58. .header("X-Access-Token", accessToken)
    59. .body(JSONUtil.toJsonPrettyStr(paramMap)).execute().body();
    60. JSONObject jsonObject = JSONUtil.parseObj(xeblCertificateResult);
    61. String code1 = jsonObject.getStr("code");
    62. if ("200".equals(code1)) {
    63. //将result转成JSONObject
    64. JSONObject json_result = jsonObject.getJSONObject("result");
    65. Map map = JSONUtil.parseObj(json_result);
    66. Map sysCertPdfMap = new HashMap<>();
    67. Object pngCert = map.get("pngCert");
    68. if (pngCert instanceof JSONArray) {
    69. JSONArray jsonArray = (JSONArray) pngCert;
    70. for (Object o : jsonArray) {
    71. Map pngCertMap = (Map) o;
    72. for (String value : pngCertMap.keySet()) {
    73. SysCertPdf sysCertPdf = new SysCertPdf();
    74. String base64 = (String) pngCertMap.get(value);
    75. MultipartFile pngMultipartFile = Base64ToMultipartFileConverter.convert(base64, ycid + ".png");
    76. uploadFile(pngMultipartFile, value, "png", ycid, uploadType, sysCertPdf);
    77. sysCertPdfMap.put(value, sysCertPdf);
    78. }
    79. }
    80. }
    81. Object pdfCert = map.get("pdfCert");
    82. if (pdfCert instanceof JSONArray) {
    83. JSONArray jsonArray = (JSONArray) pdfCert;
    84. for (Object o : jsonArray) {
    85. Map pngCertMap = (Map) o;
    86. for (String value : pngCertMap.keySet()) {
    87. String base64 = (String) pngCertMap.get(value);
    88. MultipartFile pdfMultipartFile = Base64ToMultipartFileConverter.convert(base64, ycid + ".pdf");
    89. uploadFile(pdfMultipartFile, value, "pdf", ycid, uploadType, sysCertPdfMap.get(value));
    90. }
    91. }
    92. }
    93. for (String s : sysCertPdfMap.keySet()) {
    94. sysCertPdfs.add(sysCertPdfMap.get(s));
    95. }
    96. }
    97. }
    98. return sysCertPdfs;
    99. }
    100. /**
    101. * 将文件上传到服务器
    102. *
    103. * @param multipartFile
    104. * @param value
    105. * @param type
    106. * @param ycid
    107. * @param uploadType
    108. * @param sysCertPdf
    109. * @return
    110. * @throws Exception
    111. */
    112. public static SysCertPdf uploadFile(MultipartFile multipartFile, String value, String type, String ycid, String uploadType, SysCertPdf sysCertPdf) throws Exception {
    113. String bizPath = "pdfCert" + "/" + value;
    114. String fileUrl = " ";
    115. if ("png".equals(type)) {
    116. if (CommonConstant.UPLOAD_TYPE_MINIO.equals(uploadType)) {
    117. fileUrl = MinioUtil.upload(multipartFile, bizPath);
    118. fileUrl = fileUrl.substring(1, fileUrl.length());
    119. //保存文件信息
    120. // 获取文件名
    121. sysCertPdf.setModelName(value);
    122. sysCertPdf.setPngPath(fileUrl);
    123. sysCertPdf.setCertId(ycid);
    124. sysCertPdf.setCertType(1);
    125. sysCertPdf.setDeleted(0);
    126. return sysCertPdf;
    127. } else if (CommonConstant.UPLOAD_TYPE_ZOS.equals(uploadType)) {
    128. fileUrl = ZOSUtil.upload(multipartFile, bizPath);
    129. sysCertPdf.setModelName(value);
    130. sysCertPdf.setPngPath(fileUrl);
    131. sysCertPdf.setCertId(ycid);
    132. sysCertPdf.setCertType(1);
    133. sysCertPdf.setDeleted(0);
    134. return sysCertPdf;
    135. }
    136. } else if ("pdf".equals(type)) {
    137. if (BeanUtil.isNotEmpty(sysCertPdf)) {
    138. if (CommonConstant.UPLOAD_TYPE_MINIO.equals(uploadType)) {
    139. fileUrl = MinioUtil.upload(multipartFile, bizPath);
    140. //保存文件信息
    141. // 获取文件名
    142. sysCertPdf.setPdfPath(fileUrl);
    143. return sysCertPdf;
    144. } else if (CommonConstant.UPLOAD_TYPE_ZOS.equals(uploadType)) {
    145. fileUrl = ZOSUtil.upload(multipartFile, bizPath);
    146. sysCertPdf.setPdfPath(fileUrl);
    147. return sysCertPdf;
    148. }
    149. }
    150. }
    151. return null;
    152. }
    153. /**
    154. * 将文件保存到本地
    155. *
    156. * @param file
    157. * @param type
    158. * @param value
    159. * @param ycid
    160. * @throws IOException
    161. */
    162. public static void saveFileToLocal(MultipartFile file, String type, String value, String ycid) throws IOException {
    163. String fileName = file.getOriginalFilename();
    164. String filePath = "E:\\workspace\\" + fileName + "." + type;
    165. String filePath1 = "E:\\workspace\\" + "pdfCert" + "/" + value + "/" + ycid + "." + type;
    166. File localFile = new File(filePath1);
    167. // 文件名
    168. if (!localFile.getParentFile().exists()) {
    169. localFile.getParentFile().mkdirs();// 新建目录
    170. }
    171. file.transferTo(localFile);
    172. }
    173. /**
    174. * 根据船只身份证查询照片
    175. *
    176. * @param sfzhm
    177. * @param uploadType
    178. * @return
    179. */
    180. public static SysCertPdf xeblCyzp(String sfzhm, String uploadType) throws Exception {
    181. //
    182. HashMap paramMap = new HashMap<>();
    183. paramMap.put("clientId", "402881f167e00001");
    184. paramMap.put("clientSecret", "482978cf5bcb08bc51dcec5b680ccff5ff54ceef53cfde562");
    185. //form-data
    186. String requestResult = HttpRequest.post(XeblCyzp_token_url).form(paramMap)
    187. .execute().body();
    188. JSONObject jsonResult = JSONUtil.parseObj(requestResult);
    189. String code = jsonResult.getStr("code");
    190. if ("0".equals(code)) {
    191. String result = jsonResult.getStr("result");
    192. JSONObject resultJson = JSONUtil.parseObj(result);
    193. String accessToken = resultJson.getStr("accessToken");
    194. String[] str = new String[]{sfzhm};
    195. String xeblCyzpUrlResult = HttpRequest.post(XeblCyzp_url)
    196. .header("X-Access-Token", accessToken)
    197. .body(Arrays.toString(str)).execute().body();
    198. JSONObject jsonObject = JSONUtil.parseObj(xeblCyzpUrlResult);
    199. String code1 = jsonObject.getStr("code");
    200. if ("200".equals(code1)) {
    201. String str_result = jsonObject.getStr("result");
    202. Map collect = Arrays.stream(str_result.split(",")).collect(Collectors.toMap((e -> e.split(":")[0]), (e -> e.split(":")[1])));
    203. StringBuilder stringBuilder = new StringBuilder();
    204. StringBuilder append = stringBuilder.append("[{\"").append(sfzhm).append("\"");
    205. String sfzhmBase64 = collect.get(append.toString());
    206. String substring = sfzhmBase64.substring(1,sfzhmBase64.length()-3);
    207. MultipartFile jpgMultipartFile = Base64ToMultipartFileConverter.convert(substring, sfzhm + ".jpg");
    208. return uploadCyPhoto(jpgMultipartFile, sfzhm, uploadType);
    209. }
    210. }
    211. return null;
    212. }
    213. /**
    214. * 上传船员照片
    215. * @param jpgMultipartFile
    216. * @param sfzhm
    217. * @param uploadType
    218. * @return
    219. * @throws Exception
    220. */
    221. private static SysCertPdf uploadCyPhoto(MultipartFile jpgMultipartFile, String sfzhm, String uploadType) throws Exception {
    222. String bizPath = "cyPhoto";
    223. String fileUrl = " ";
    224. SysCertPdf sysCertPdf = new SysCertPdf();
    225. if (CommonConstant.UPLOAD_TYPE_MINIO.equals(uploadType)) {
    226. fileUrl = MinioUtil.upload(jpgMultipartFile, bizPath);
    227. fileUrl = fileUrl.substring(1);
    228. //保存文件信息
    229. // 获取文件名
    230. sysCertPdf.setModelName("cyPhoto");
    231. sysCertPdf.setPngPath(fileUrl);
    232. sysCertPdf.setCertId(sfzhm);
    233. sysCertPdf.setCertType(2);
    234. sysCertPdf.setDeleted(0);
    235. return sysCertPdf;
    236. }else if (CommonConstant.UPLOAD_TYPE_ZOS.equals(uploadType)) {
    237. fileUrl = ZOSUtil.upload(jpgMultipartFile, bizPath);
    238. sysCertPdf.setModelName("cyPhoto");
    239. sysCertPdf.setPngPath(fileUrl);
    240. sysCertPdf.setCertId(sfzhm);
    241. sysCertPdf.setCertType(2);
    242. sysCertPdf.setDeleted(0);
    243. return sysCertPdf;
    244. }
    245. return null;
    246. }
    247. }

    将 base64转成MultipartFile 

    1. package org.jeecg.modules.quota.utils;
    2. import org.apache.commons.fileupload.FileItem;
    3. import org.apache.commons.io.FileUtils;
    4. import org.springframework.mock.web.MockMultipartFile;
    5. import org.springframework.util.Base64Utils;
    6. import org.springframework.core.io.ByteArrayResource;
    7. import org.springframework.web.multipart.MultipartFile;
    8. import org.springframework.web.multipart.commons.CommonsMultipartFile;
    9. import java.io.ByteArrayInputStream;
    10. import java.io.File;
    11. import java.io.IOException;
    12. import java.nio.charset.StandardCharsets;
    13. import java.util.Base64;
    14. public class Base64ToMultipartFileConverter {
    15. public static MultipartFile convert(String base64String, String fileName) {
    16. // 解码Base64字符串为字节数组
    17. byte[] bytes = Base64.getDecoder().decode(base64String);
    18. // 创建MockMultipartFile对象并设置文件内容和文件名
    19. MockMultipartFile multipartFile = new MockMultipartFile("file", fileName, "application/octet-stream", bytes);
    20. return multipartFile;
    21. }
    22. }

    hutool发送+参数get请求

    1. //地址
    2. String checkur = downloa.getLoadConfigUrl();
    3. //设备序列号
    4. String deviceId = downloa.getDeviceId();
    5. HashMap paramMap = new HashMap();
    6. paramMap.put("deviceId", deviceId);
    7. //!!!!!!!!!!!!!!++++=
    8. //发送post请求
    9. String jsonResult = HttpRequest.post(checkur)
    10. .body(JSONUtil.toJsonPrettyStr(paramMap))
    11. .execute().body();
    12. JSONObject jsonObject = JSONUtil.parseObj(jsonResult);
    13. String responseCode = jsonObject.getStr("responseCode");
    14. if (Constants.SUCCESS.equals(responseCode)) {
    15. BasicDownloadRes basicDownloadRes = new BasicDownloadRes();
    16. basicDownloadRes.setResponseMessage(jsonObject.getStr("responseMessage"));
    17. basicDownloadRes.setResponseCode(responseCode);
    18. this.insertBasicDownloadRes(basicDownloadRes);
    19. /**
    20. * @Description: TODO
    21. * @Author LeiTao
    22. * @Date 2022/4/23
    23. * @Version V1.0
    24. **/
    25. String data = jsonObject.getStr("data");
    26. Map map = JSONUtil.parseObj(data);
    27. //行政区划代码
    28. List> region = (List>) map.get("region");
    29. this.basicDataDownloadRegionService.deleteRegionAll();
    30. for (Map regionMap : region) {
    31. BasicDataDownloadRegion basicDataDownloadRegionService = new BasicDataDownloadRegion();
    32. basicDataDownloadRegionService.setCode((String) regionMap.get("code"));
    33. basicDataDownloadRegionService.setFullName((String) regionMap.get("fullName"));
    34. basicDataDownloadRegionService.setName((String) regionMap.get("name"));
    35. basicDataDownloadRegionService.setType((Integer) regionMap.get("type"));
    36. this.basicDataDownloadRegionService.insertBasicDataDownloadRegion(basicDataDownloadRegionService);
    37. }
    38. //行业代码
    39. List> industry = (List>) map.get("industry");
    40. basicDataDownloadIndustryService.deleteIndustryAll();
    41. for (Map industryMap : industry) {
    42. BasicDataDownloadIndustry basicDataDownloadIndustry = new BasicDataDownloadIndustry();
    43. basicDataDownloadIndustry.setCode((String) industryMap.get("code"));
    44. basicDataDownloadIndustry.setFullName((String) industryMap.get("fullName"));
    45. basicDataDownloadIndustry.setJgzh(Boolean.toString((Boolean) industryMap.get("jgzh")));
    46. basicDataDownloadIndustry.setName((String) industryMap.get("name"));
    47. basicDataDownloadIndustry.setPcode((String) industryMap.get("pcode"));
    48. basicDataDownloadIndustryService.insertBasicDataDownloadIndustry(basicDataDownloadIndustry);
    49. }

  • 相关阅读:
    基于java的CRM客户关系管理系统的设计与实现
    QT实现TCP通信(服务器与客户端搭建)
    如何设计一条大型PLC生产线系统
    Nacos安装讲解教程
    面试题汇总
    2024级199管理类联考之写作
    【编译原理】词法分析
    JEECG BOOT 前端记录
    Kubernetes:kube-apiserver 之 scheme(二)
    Java开发 - Redis常见问题场景及解决办法一览
  • 原文地址:https://blog.csdn.net/weixin_60948956/article/details/133276941