From 8ea0e7d76194175adc8fcac808e263b28ff2ba9f Mon Sep 17 00:00:00 2001 From: leebj Date: Mon, 20 Oct 2025 13:33:52 +0900 Subject: [PATCH] no message --- .../cokr/xit/fims/nxrp/web/MultipartTest.java | 129 +++++++++++++ .../xit/fims/nxrp/web/NxrpController.java | 174 ++++++++++++++++++ 2 files changed, 303 insertions(+) create mode 100644 src/main/java/cokr/xit/fims/nxrp/web/MultipartTest.java diff --git a/src/main/java/cokr/xit/fims/nxrp/web/MultipartTest.java b/src/main/java/cokr/xit/fims/nxrp/web/MultipartTest.java new file mode 100644 index 00000000..00a284ed --- /dev/null +++ b/src/main/java/cokr/xit/fims/nxrp/web/MultipartTest.java @@ -0,0 +1,129 @@ +package cokr.xit.fims.nxrp.web; + +import java.io.IOException; +import java.math.BigInteger; +import java.net.URI; +import java.net.http.HttpClient; +import java.net.http.HttpRequest; +import java.net.http.HttpResponse; +import java.nio.charset.StandardCharsets; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.Random; + +public class MultipartTest { + + HttpClient httpClient; + + HttpRequest httpRequest; + HttpResponse httpResponse; + + protected final Map map = new HashMap<>(); + private static final String LINEFEED = "\r\n"; + private static final String DOUBLE_HYPHEN = "--"; + private static final String QUTATE = "\""; + + public void fileDown(String url, String downloadPath) throws IOException { + + httpClient = HttpClient.newHttpClient(); + httpRequest = HttpRequest.newBuilder() + .uri(URI.create(url)) + .setHeader("Accept-Language", "ko") + .setHeader("Content-Type", "application/json") + .GET() + .build(); + + try { + + HttpResponse getResponse = httpClient.send(httpRequest, HttpResponse.BodyHandlers.ofFile(Paths.get(downloadPath))); + + } catch (InterruptedException e) { + e.printStackTrace(); + } + + } + + public void multipartPost(String sendFilePath, String fileSavePath, String sendUri) throws IOException { + Path path = Path.of(sendFilePath); + BigInteger randomNumber = new BigInteger(256, new Random()); + StringBuilder boundary = new StringBuilder().append(randomNumber); + + map.put("fileSavePath", fileSavePath); + map.put("file", path); + + httpClient = HttpClient.newHttpClient(); + multipartToByte(map, boundary.toString()); + httpRequest = HttpRequest.newBuilder() + .uri(URI.create(sendUri)) + .setHeader("Accept-Language", "ko") + .setHeader("Content-Type", "multipart/form-data; boundary=" + boundary) + .POST(multipartToByte(map, boundary.toString())) + .build(); + + try { + HttpResponse response = httpClient.send(httpRequest, HttpResponse.BodyHandlers.ofString()); + } catch (InterruptedException e) { + e.printStackTrace(); + } + } + + private HttpRequest.BodyPublisher multipartToByte (Map map, String boundary) throws IOException { + List byteArrays = new ArrayList<>(); + StringBuilder stringBuilder = new StringBuilder(); + + for (Map.Entry data : map.entrySet()) { + stringBuilder.setLength(0); + stringBuilder.append(DOUBLE_HYPHEN) + .append(boundary) + .append(LINEFEED); + + if (data.getValue() instanceof Path) { + Path filePath = (Path)data.getValue(); + String mimeType = Files.probeContentType(filePath); + byte[] fileByte = Files.readAllBytes(filePath); + + stringBuilder.append("Content-Disposition: form-data; name=") + .append(QUTATE) + .append(data.getKey()) + .append(QUTATE) + .append("; filename= ") + .append(QUTATE) + .append(data.getValue()) + .append(QUTATE) + .append(LINEFEED) + .append("Content-Type: ") + .append(mimeType) + .append(LINEFEED) + .append(LINEFEED); + + byteArrays.add(stringBuilder.toString().getBytes(StandardCharsets.UTF_8)); + byteArrays.add(fileByte); + byteArrays.add(LINEFEED.getBytes(StandardCharsets.UTF_8)); + } else { + stringBuilder.append("Content-Disposition: form-data; name=") + .append(QUTATE) + .append(data.getKey()) + .append(QUTATE) + .append(";") + .append(LINEFEED) + .append(LINEFEED) + .append(data.getValue()) + .append(LINEFEED); + byteArrays.add(stringBuilder.toString().getBytes(StandardCharsets.UTF_8)); + } + } + + stringBuilder.setLength(0); + stringBuilder.append(DOUBLE_HYPHEN) + .append(boundary) + .append(DOUBLE_HYPHEN); + byteArrays.add(stringBuilder.toString().getBytes(StandardCharsets.UTF_8)); + + return HttpRequest.BodyPublishers.ofByteArrays(byteArrays); + } +} diff --git a/src/main/java/cokr/xit/fims/nxrp/web/NxrpController.java b/src/main/java/cokr/xit/fims/nxrp/web/NxrpController.java index 277ff7c5..a83a7cbd 100644 --- a/src/main/java/cokr/xit/fims/nxrp/web/NxrpController.java +++ b/src/main/java/cokr/xit/fims/nxrp/web/NxrpController.java @@ -1,21 +1,29 @@ package cokr.xit.fims.nxrp.web; import java.io.File; +import java.io.FileInputStream; +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; import java.time.LocalDateTime; import java.time.format.DateTimeFormatter; import java.util.List; +import java.util.function.Consumer; import javax.annotation.Resource; import javax.servlet.http.HttpServletRequest; +import org.apache.commons.io.FilenameUtils; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.multipart.MultipartFile; import org.springframework.web.servlet.ModelAndView; import cokr.xit.fims.nxrp.Hirer; import cokr.xit.fims.nxrp.NisIndivQuery; import cokr.xit.fims.nxrp.service.NisIndivService; import cokr.xit.fims.nxrp.service.bean.NisIndivBean; +import cokr.xit.foundation.Downloadable; import cokr.xit.foundation.web.AbstractController; @Controller @@ -51,6 +59,72 @@ public class NxrpController extends AbstractController { return new ModelAndView("jsonView"); } + + + //테스트서버에서 압축파일만들고 제주도운영서버에 파일보내기 + @RequestMapping(name="테스트서버에서 압축파일만들고 제주도운영서버에 파일보내기", value="/jeju01_test_to_jejudo.do") + public ModelAndView jeju01_test_to_jejudo(HttpServletRequest hreq, NisIndivQuery req) { + + ModelAndView jsonView = new ModelAndView("jsonView"); + + if(!isLocal() && !isTestServer()) { + return jsonView; + } + + String url = ""; + String prodYn = hreq.getParameter("prodYn"); + if(prodYn == null) { + return jsonView; + } else if(prodYn.equals("Y")) { + url = "https://jeju.go.kr"; + } else if(prodYn.equals("N")) { + url = "http://dev.jejudo.go.kr"; + } else { + return jsonView; + } + String random = hreq.getParameter("random"); + boolean randomFlag = true; + if(random != null && random.equals("N")) { + randomFlag = false; + } + NisIndivQuery query = new NisIndivQuery(); + query.setSggCd("50110"); + query.setTaskSeCd("ECA"); + String zipPath = nisIndivBean.makeNisIndivJeju01(query, randomFlag); + String zipFolder = FilenameUtils.getPath(zipPath); + + if(zipPath != null && !zipPath.equals("")) { + MultipartTest mt = new MultipartTest(); + + try { + mt.multipartPost(zipPath, zipFolder, url+"/is/rentcar/intf/nxrp/jeju01_multipart_to_jejusi.do"); + } catch (IOException e) { + e.printStackTrace(); + } + (new File(zipPath)).delete(); + } + + jsonView.addObject("res", "ok"); + return jsonView; + } + + //테스트서버에서 보낸 압축파일 받아서 제주시로 보내기 + @RequestMapping(name="테스트서버에서 보낸 압축파일 받아서 제주시로 보내기", value="/jeju01_multipart_to_jejusi.do") + public ModelAndView jeju01_multipart_to_jejusi(MultipartFile[] file, HttpServletRequest req) throws IllegalStateException, IOException { + + + String zipFolder = req.getParameter("fileSavePath"); + + File file0 = new File(zipFolder+file[0].getOriginalFilename()); + file[0].transferTo(file0); + + + nisIndivBean.send_DO_to_SI(file0.getPath()); + + return new ModelAndView("jsonView"); + } + + //테스트 계약정보만들기 @RequestMapping(name="계약정보만들기", value="/makeJeju01.do") public ModelAndView makeJeju01(NisIndivQuery req) { @@ -80,6 +154,88 @@ public class NxrpController extends AbstractController { return new ModelAndView("jsonView"); } + //테스트 면허정보응답파일 가져오기 + @RequestMapping(name="getSiToTest0", value="/getSiToTest0.do") + public ModelAndView getSiToTest0(HttpServletRequest req) throws IOException { + ModelAndView jsonView = new ModelAndView("jsonView"); + + if(!isLocal() && !isTestServer()) { + return jsonView; + } + + String url = ""; + String prodYn = req.getParameter("prodYn"); + if(prodYn == null) { + return jsonView; + } else if(prodYn.equals("Y")) { + url = "https://jeju.go.kr"; + } else if(prodYn.equals("N")) { + url = "http://dev.jejudo.go.kr"; + } else { + return jsonView; + } + + MultipartTest mt = new MultipartTest(); + mt.fileDown(url+"/is/rentcar/intf/nxrp/getSiToTest.do", "files/temp/receiveDriver"); + + File folder = new File("files/temp/receiveDriver"); + File[] fl = folder.listFiles(); + if(fl == null || fl.length == 0) { + System.out.println("1!!!"); + return jsonView; + } + + if(fl[0].length() == 0) { + System.out.println("2!!!"); + fl[0].delete(); + return jsonView; + } + + //List results = nisIndivBean.getCarDriverInfoFromFile(); + //for(Hirer result : results) { + //nisIndivBean.updateDriverInfo(result); + //} + + return jsonView; + } + + //테스트 면허정보응답파일 가져오기 + @RequestMapping(name="getSiToTest", value="/getSiToTest.do") + public ModelAndView getSiToTest(NisIndivQuery req) throws IOException { + ModelAndView dv = new ModelAndView("downloadView"); + + nisIndivBean.get_SI_to_DO(); + + File folder = new File("files/temp/receiveDriver"); + File[] fl = folder.listFiles(); + if(fl != null && fl.length > 0) { + String name = fl[0].getName(); + + InputStream menualIS = new FileInputStream(fl[0]); + byte[] menualBytes = menualIS.readAllBytes(); + menualIS.close(); + + Consumer writer = new Consumer() { + @Override + public void accept(OutputStream os) { + try { + os.write(menualBytes); + } catch (IOException e) { + e.printStackTrace(); + } + } + }; + + dv.addObject("download", + new Downloadable() + .setContentType("application/zip") + .setWriter(writer) + .setFilename(name) + ); + } + + return dv; + } //테스트 면허정보응답파일 가져오기 @RequestMapping(name="getSiToDo", value="/getSiToDo.do") @@ -114,4 +270,22 @@ public class NxrpController extends AbstractController { return new ModelAndView("jsonView"); } + + + + + public boolean isLocal() { + if(new File("C:/Users/XIT_LBJ").exists()) { + return true; + } else { + return false; + } + } + public boolean isTestServer() { + if(new File("/home/xituser").exists()) { + return true; + } else { + return false; + } + } }