Request tool class hutool package HttpRequest sends post request with request header and request body
Save MultipartFile file to local
package org.jeecg.modules.quota.utils; import cn.hutool.core.bean.BeanUtil; import cn.hutool.http.HttpRequest; import cn.hutool.json.JSONArray; import cn.hutool.json.JSONObject; import cn.hutool.json.JSONUtil; import org.jeecg.common.constant.CommonConstant; import org.jeecg.common.util.MinioUtil; import org.jeecg.common.util.ZOSUtil; import org.jeecg.modules.quota.controller.DataSynchronismController; import org.jeecg.modules.quota.entity.SysCertPdf; import org.jeecg.modules.quota.service.ISysCertPdfService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.client.RestTemplate; import org.springframework.web.multipart.MultipartFile; import java.io.*; import java.util.*; import java.util.stream.Collectors; import java.util.stream.Stream; /** * @Author leitao * @Date 2023/9/21 15:28 * @description: Request tool class * @Title: RequestYxeUtil * @Package org.jeecg.modules.quota.utils */ public class RequestYxeUtil { // Private Static Final String token_url = \ "http: // 23: 89/xxxx/token? Clientid = 2C9642BD21C0000 & amp; ClientSecret = 6ABC4457BC60E0D2E 0B08A8B9828C6E9D86250458B4DE51BF82D9604ABEB987D3 \ "; //token (online) private static final String token_url = "http://23:89/xxxx/xxxx/token?clientId=2c9fb2d21c0000 & amp;clientSecret=6ab3e8291b9af828c6e9d86250458b4de51bf82d9604abeb987d3"; //Get fishing boat photo evidence private static final String XeblCertificate_url = "http://23:89/xxx/xxxx/xxxxx/XeblCertificate"; //token() private static final String XeblCyzp_token_url = "http://28:87/xxx//xxxx/token"; //Get crew photos private static final String XeblCyzp_url = "http://28:87/xxx//xxxx/CyService/getCyPhoto"; /** *Limit xxxx certificate * * @param cm * @param ycid * @throwsIOException */ public static ArrayList<SysCertPdf> xeblCertificate(String cm, String ycid, String uploadType) throws Exception { ArrayList<SysCertPdf> sysCertPdfs = new ArrayList<>(); String requestResult = HttpRequest.post(token_url).execute().body(); JSONObject jsonResult = JSONUtil.parseObj(requestResult); String code = jsonResult.getStr("code"); if ("0".equals(code)) { String result = jsonResult.getStr("result"); JSONObject resultJson = JSONUtil.parseObj(result); String accessToken = resultJson.getStr("accessToken"); //json format HashMap<String, Object> paramMap = new HashMap<>(); paramMap.put("shipName", cm); paramMap.put("shipInfoId", ycid); String xeblCertificateResult = HttpRequest.post(XeblCertificate_url) .header("X-Access-Token", accessToken) .body(JSONUtil.toJsonPrettyStr(paramMap)).execute().body(); JSONObject jsonObject = JSONUtil.parseObj(xeblCertificateResult); String code1 = jsonObject.getStr("code"); if ("200".equals(code1)) { //Convert result to JSONObject JSONObject json_result = jsonObject.getJSONObject("result"); Map<String, Object> map = JSONUtil.parseObj(json_result); Map<String, SysCertPdf> sysCertPdfMap = new HashMap<>(); Object pngCert = map.get("pngCert"); if (pngCert instanceof JSONArray) { JSONArray jsonArray = (JSONArray) pngCert; for (Object o : jsonArray) { Map<String, Object> pngCertMap = (Map<String, Object>) o; for (String value : pngCertMap.keySet()) { SysCertPdf sysCertPdf = new SysCertPdf(); String base64 = (String) pngCertMap.get(value); MultipartFile pngMultipartFile = Base64ToMultipartFileConverter.convert(base64, ycid + ".png"); uploadFile(pngMultipartFile, value, "png", ycid, uploadType, sysCertPdf); sysCertPdfMap.put(value, sysCertPdf); } } } Object pdfCert = map.get("pdfCert"); if (pdfCert instanceof JSONArray) { JSONArray jsonArray = (JSONArray) pdfCert; for (Object o : jsonArray) { Map<String, Object> pngCertMap = (Map<String, Object>) o; for (String value : pngCertMap.keySet()) { String base64 = (String) pngCertMap.get(value); MultipartFile pdfMultipartFile = Base64ToMultipartFileConverter.convert(base64, ycid + ".pdf"); uploadFile(pdfMultipartFile, value, "pdf", ycid, uploadType, sysCertPdfMap.get(value)); } } } for (String s : sysCertPdfMap.keySet()) { sysCertPdfs.add(sysCertPdfMap.get(s)); } } } return sysCertPdfs; } /** * Upload files to server * * @param multipartFile * @param value * @param type * @param ycid * @param uploadType * @param sysCertPdf * @return * @throwsException */ public static SysCertPdf uploadFile(MultipartFile multipartFile, String value, String type, String ycid, String uploadType, SysCertPdf sysCertPdf) throws Exception { String bizPath = "pdfCert" + "/" + value; String fileUrl = " "; if ("png".equals(type)) { if (CommonConstant.UPLOAD_TYPE_MINIO.equals(uploadType)) { fileUrl = MinioUtil.upload(multipartFile, bizPath); fileUrl = fileUrl.substring(1, fileUrl.length()); //Save file information // Get file name sysCertPdf.setModelName(value); sysCertPdf.setPngPath(fileUrl); sysCertPdf.setCertId(ycid); sysCertPdf.setCertType(1); sysCertPdf.setDeleted(0); return sysCertPdf; } else if (CommonConstant.UPLOAD_TYPE_ZOS.equals(uploadType)) { fileUrl = ZOSUtil.upload(multipartFile, bizPath); sysCertPdf.setModelName(value); sysCertPdf.setPngPath(fileUrl); sysCertPdf.setCertId(ycid); sysCertPdf.setCertType(1); sysCertPdf.setDeleted(0); return sysCertPdf; } } else if ("pdf".equals(type)) { if (BeanUtil.isNotEmpty(sysCertPdf)) { if (CommonConstant.UPLOAD_TYPE_MINIO.equals(uploadType)) { fileUrl = MinioUtil.upload(multipartFile, bizPath); //Save file information // Get file name sysCertPdf.setPdfPath(fileUrl); return sysCertPdf; } else if (CommonConstant.UPLOAD_TYPE_ZOS.equals(uploadType)) { fileUrl = ZOSUtil.upload(multipartFile, bizPath); sysCertPdf.setPdfPath(fileUrl); return sysCertPdf; } } } return null; } /** * Save the file locally * * @param file * @param type * @param value * @param ycid * @throwsIOException */ public static void saveFileToLocal(MultipartFile file, String type, String value, String ycid) throws IOException { String fileName = file.getOriginalFilename(); String filePath = "E:\\workspace\" + fileName + "." + type; String filePath1 = "E:\\workspace\" + "pdfCert" + "/" + value + "/" + ycid + "." + type; File localFile = new File(filePath1); // file name if (!localFile.getParentFile().exists()) { localFile.getParentFile().mkdirs(); // Create a new directory } file.transferTo(localFile); } /** * Search photos based on vessel ID card * * @param sfzhm * @param uploadType * @return */ public static SysCertPdf xeblCyzp(String sfzhm, String uploadType) throws Exception { // HashMap<String, Object> paramMap = new HashMap<>(); paramMap.put("clientId", "402881f167e00001"); paramMap.put("clientSecret", "482978cf5bcb08bc51dcec5b680ccff5ff54ceef53cfde562"); //form-data String requestResult = HttpRequest.post(XeblCyzp_token_url).form(paramMap) .execute().body(); JSONObject jsonResult = JSONUtil.parseObj(requestResult); String code = jsonResult.getStr("code"); if ("0".equals(code)) { String result = jsonResult.getStr("result"); JSONObject resultJson = JSONUtil.parseObj(result); String accessToken = resultJson.getStr("accessToken"); String[] str = new String[]{sfzhm}; String xeblCyzpUrlResult = HttpRequest.post(XeblCyzp_url) .header("X-Access-Token", accessToken) .body(Arrays.toString(str)).execute().body(); JSONObject jsonObject = JSONUtil.parseObj(xeblCyzpUrlResult); String code1 = jsonObject.getStr("code"); if ("200".equals(code1)) { String str_result = jsonObject.getStr("result"); Map<String, String> collect = Arrays.stream(str_result.split(",")).collect(Collectors.toMap((e -> e.split(":")[0]), ( e -> e.split(":")[1]))); StringBuilder stringBuilder = new StringBuilder(); StringBuilder append = stringBuilder.append("[{"").append(sfzhm).append("""); String sfzhmBase64 = collect.get(append.toString()); String substring = sfzhmBase64.substring(1,sfzhmBase64.length()-3); MultipartFile jpgMultipartFile = Base64ToMultipartFileConverter.convert(substring, sfzhm + ".jpg"); return uploadCyPhoto(jpgMultipartFile, sfzhm, uploadType); } } return null; } /** * Upload photos of crew members * @param jpgMultipartFile * @param sfzhm * @param uploadType * @return * @throwsException */ private static SysCertPdf uploadCyPhoto(MultipartFile jpgMultipartFile, String sfzhm, String uploadType) throws Exception { String bizPath = "cyPhoto"; String fileUrl = " "; SysCertPdf sysCertPdf = new SysCertPdf(); if (CommonConstant.UPLOAD_TYPE_MINIO.equals(uploadType)) { fileUrl = MinioUtil.upload(jpgMultipartFile, bizPath); fileUrl = fileUrl.substring(1); //Save file information // Get file name sysCertPdf.setModelName("cyPhoto"); sysCertPdf.setPngPath(fileUrl); sysCertPdf.setCertId(sfzhm); sysCertPdf.setCertType(2); sysCertPdf.setDeleted(0); return sysCertPdf; }else if (CommonConstant.UPLOAD_TYPE_ZOS.equals(uploadType)) { fileUrl = ZOSUtil.upload(jpgMultipartFile, bizPath); sysCertPdf.setModelName("cyPhoto"); sysCertPdf.setPngPath(fileUrl); sysCertPdf.setCertId(sfzhm); sysCertPdf.setCertType(2); sysCertPdf.setDeleted(0); return sysCertPdf; } return null; } }
Convert base64 to MultipartFile
package org.jeecg.modules.quota.utils; import org.apache.commons.fileupload.FileItem; import org.apache.commons.io.FileUtils; import org.springframework.mock.web.MockMultipartFile; import org.springframework.util.Base64Utils; import org.springframework.core.io.ByteArrayResource; import org.springframework.web.multipart.MultipartFile; import org.springframework.web.multipart.commons.CommonsMultipartFile; import java.io.ByteArrayInputStream; import java.io.File; import java.io.IOException; import java.nio.charset.StandardCharsets; import java.util.Base64; public class Base64ToMultipartFileConverter { public static MultipartFile convert(String base64String, String fileName) { // Decode Base64 string into byte array byte[] bytes = Base64.getDecoder().decode(base64String); //Create a MockMultipartFile object and set the file content and file name MockMultipartFile multipartFile = new MockMultipartFile("file", fileName, "application/octet-stream", bytes); return multipartFile; } }
hutool sends + parameter get request
//Address String checkur = downloa.getLoadConfigUrl(); //devise serial number String deviceId = downloa.getDeviceId(); HashMap<String, Object> paramMap = new HashMap<String, Object>(); paramMap.put("deviceId", deviceId); //!!!!!!!!!!!!!!! + + + + = //Send post request String jsonResult = HttpRequest.post(checkur) .body(JSONUtil.toJsonPrettyStr(paramMap)) .execute().body(); JSONObject jsonObject = JSONUtil.parseObj(jsonResult); String responseCode = jsonObject.getStr("responseCode"); if (Constants.SUCCESS.equals(responseCode)) { BasicDownloadRes basicDownloadRes = new BasicDownloadRes(); basicDownloadRes.setResponseMessage(jsonObject.getStr("responseMessage")); basicDownloadRes.setResponseCode(responseCode); this.insertBasicDownloadRes(basicDownloadRes); /** * @Description: TODO * @Author LeiTao * @Date 2022/4/23 * @Version V1.0 **/ String data = jsonObject.getStr("data"); Map map = JSONUtil.parseObj(data); //Administrative division code List<Map<String,Object>> region = (List<Map<String,Object>>) map.get("region"); this.basicDataDownloadRegionService.deleteRegionAll(); for (Map<String,Object> regionMap : region) { BasicDataDownloadRegion basicDataDownloadRegionService = new BasicDataDownloadRegion(); basicDataDownloadRegionService.setCode((String) regionMap.get("code")); basicDataDownloadRegionService.setFullName((String) regionMap.get("fullName")); basicDataDownloadRegionService.setName((String) regionMap.get("name")); basicDataDownloadRegionService.setType((Integer) regionMap.get("type")); this.basicDataDownloadRegionService.insertBasicDataDownloadRegion(basicDataDownloadRegionService); } //Industry code List<Map<String,Object>> industry = (List<Map<String,Object>>) map.get("industry"); basicDataDownloadIndustryService.deleteIndustryAll(); for (Map<String, Object> industryMap : industry) { BasicDataDownloadIndustry basicDataDownloadIndustry = new BasicDataDownloadIndustry(); basicDataDownloadIndustry.setCode((String) industryMap.get("code")); basicDataDownloadIndustry.setFullName((String) industryMap.get("fullName")); basicDataDownloadIndustry.setJgzh(Boolean.toString((Boolean) industryMap.get("jgzh"))); basicDataDownloadIndustry.setName((String) industryMap.get("name")); basicDataDownloadIndustry.setPcode((String) industryMap.get("pcode")); basicDataDownloadIndustryService.insertBasicDataDownloadIndustry(basicDataDownloadIndustry); }
The knowledge points of the article match the official knowledge files, and you can further learn related knowledge. Java Skill TreeHomepageOverview 133323 people are learning the system