CommonService.java 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. package com.its.web.service.common;
  2. import com.its.web.dto.common.CodeDto;
  3. import com.its.web.dto.common.ConnStatisticsDto;
  4. import com.its.web.dto.common.TbWwwOrgDto;
  5. import com.its.web.mapper.its.common.CommonMapper;
  6. import lombok.RequiredArgsConstructor;
  7. import lombok.extern.slf4j.Slf4j;
  8. import org.apache.commons.io.FileUtils;
  9. import org.springframework.beans.factory.annotation.Value;
  10. import org.springframework.stereotype.Service;
  11. import javax.servlet.http.HttpServletRequest;
  12. import java.io.*;
  13. import java.net.URLDecoder;
  14. import java.util.List;
  15. import java.util.Map;
  16. import java.util.UUID;
  17. @Slf4j
  18. @RequiredArgsConstructor
  19. @Service
  20. public class CommonService {
  21. private final CommonMapper mapper;
  22. @Value("${image-location}")
  23. String imageLocation;
  24. @Value("${video-location}")
  25. String videoLocation;
  26. /**
  27. * 공공기관 사이트 URL
  28. * @return List<TbWwwOrgDto>
  29. */
  30. public List<TbWwwOrgDto> findAllOrganization() {
  31. return this.mapper.findAllOrganization();
  32. }
  33. /**
  34. * Day Code 리스트
  35. * @return List<CodeDto>
  36. */
  37. public List<CodeDto> findDayList() {
  38. return this.mapper.findDayList();
  39. }
  40. /**
  41. * 접속자 조회 통계
  42. * @return List<ConnStatisticsDto>
  43. */
  44. public List<ConnStatisticsDto> getConnStatistics(Map<String, String> paramMap) {
  45. String type = paramMap.get("type");
  46. if ("week".equals(type)) {
  47. return this.mapper.getConnWeekStatistics(paramMap);
  48. }
  49. else if ("month".equals(type)) {
  50. return this.mapper.getConnMonthStatistics(paramMap);
  51. }
  52. return this.mapper.getConnStatistics(paramMap);
  53. }
  54. /**
  55. * 접속자 이력 등록
  56. * @return DB 등록 수
  57. */
  58. public int insertConnHs() { return this.mapper.insertConnHs(); }
  59. public String imageUpload(HttpServletRequest req) {
  60. String uploadImageInfo = "";
  61. String fileName = req.getHeader("file-name");
  62. try {
  63. fileName = URLDecoder.decode(fileName, "UTF-8");
  64. }
  65. catch (UnsupportedEncodingException exception) {
  66. log.error("Can not Decoding UTF-8");
  67. }
  68. String filePath = imageLocation;
  69. File file = new File(filePath);
  70. boolean isMkdirs = true;
  71. if (!file.exists()) {
  72. isMkdirs = file.mkdirs();
  73. }
  74. if (isMkdirs) {
  75. String realFileNm;
  76. realFileNm = UUID.randomUUID().toString().replace("-", "") + fileName.substring(fileName.lastIndexOf("."));
  77. String rlFileNm = filePath + File.separator + realFileNm;
  78. try (
  79. InputStream is = req.getInputStream();
  80. OutputStream os = new FileOutputStream(rlFileNm);
  81. ) {
  82. int numRead;
  83. byte[] byteArray = new byte[Integer.parseInt(req.getHeader("file-size"))];
  84. if (is != null) {
  85. while ((numRead = is.read(byteArray, 0, byteArray.length)) != -1) {
  86. os.write(byteArray, 0, numRead);
  87. }
  88. }
  89. os.flush();
  90. } catch (IOException e) {
  91. log.error("noticeImgupload() IO Exception");
  92. } catch (Exception e1) {
  93. log.error("noticeImgupload() Exception");
  94. }
  95. uploadImageInfo += "&bNewLine=true";
  96. uploadImageInfo += "&sFileName=" + fileName;
  97. uploadImageInfo += "&sFileURL=" + "/api/common/upload/" + realFileNm;
  98. }
  99. return uploadImageInfo;
  100. }
  101. // public byte[] upload(String imageName, String type) {
  102. public byte[] upload(String imageName) {
  103. byte[] image = null;
  104. File file = new File(imageLocation, imageName);
  105. // if (type.equals("video")) {
  106. // file = new File(videoLocation, imageName);
  107. // }
  108. int fileSize = (int) file.length();
  109. if (fileSize > 0) {
  110. try {
  111. byte[] byteArray = FileUtils.readFileToByteArray(file);
  112. if (byteArray.length > 0) {
  113. int ii = 0;
  114. image = new byte[byteArray.length];
  115. for (byte b : byteArray) {
  116. image[ii++] = b;
  117. }
  118. }
  119. } catch (FileNotFoundException e) {
  120. log.error("Not Found File - {}", imageLocation + imageName);
  121. } catch (IOException e) {
  122. log.error("Can Not Send Image Cause IOException - {}", imageLocation + imageName);
  123. }
  124. }
  125. return image;
  126. }
  127. }