Commit 6228a7a594df992f4562fbc45c646b7c66dbc2ce
1 parent
03d2bc11
SNManage commit
Showing
12 changed files
with
65 additions
and
63 deletions
pom.xml
| ... | ... | @@ -4,8 +4,8 @@ | 
| 4 | 4 | xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | 
| 5 | 5 | <modelVersion>4.0.0</modelVersion> | 
| 6 | 6 | |
| 7 | - <groupId>com.haoge</groupId> | |
| 8 | - <artifactId>SNManageDemo</artifactId> | |
| 7 | + <groupId>com.ectrip</groupId> | |
| 8 | + <artifactId>SNManage</artifactId> | |
| 9 | 9 | <version>1.0</version> | 
| 10 | 10 | |
| 11 | 11 | <properties> | 
| ... | ... | @@ -33,10 +33,6 @@ | 
| 33 | 33 | <scope>test</scope> | 
| 34 | 34 | </dependency> | 
| 35 | 35 | <dependency> | 
| 36 | - <groupId>org.springframework.boot</groupId> | |
| 37 | - <artifactId>spring-boot-starter-thymeleaf</artifactId> | |
| 38 | - </dependency> | |
| 39 | - <dependency> | |
| 40 | 36 | <groupId>io.springfox</groupId> | 
| 41 | 37 | <artifactId>springfox-swagger2</artifactId> | 
| 42 | 38 | <version>2.7.0</version> | ... | ... | 
src/main/java/com/example/demo/SNManageApplication.java renamed to src/main/java/com/ectrip/demo/SNManageApplication.java
| 1 | -package com.example.demo; | |
| 1 | +package com.ectrip.demo; | |
| 2 | 2 | |
| 3 | -import org.springframework.boot.CommandLineRunner; | |
| 4 | 3 | import org.springframework.boot.SpringApplication; | 
| 5 | 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; | 
| 6 | 5 | import springfox.documentation.swagger2.annotations.EnableSwagger2; | ... | ... | 
src/main/java/com/example/demo/controller/SNController.java renamed to src/main/java/com/ectrip/demo/controller/SNController.java
| 1 | -package com.example.demo.controller; | |
| 1 | +package com.ectrip.demo.controller; | |
| 2 | 2 | |
| 3 | -import com.example.demo.dto.AddSNsDTO; | |
| 4 | -import com.example.demo.dto.DeleteSNsDTO; | |
| 5 | -import com.example.demo.exception.SNRepetitiveException; | |
| 6 | -import com.example.demo.service.SNService; | |
| 7 | -import com.example.demo.util.HttpResult; | |
| 3 | +import com.ectrip.demo.util.HttpResult; | |
| 4 | +import com.ectrip.demo.dto.AddSNsDTO; | |
| 5 | +import com.ectrip.demo.dto.DeleteSNsDTO; | |
| 6 | +import com.ectrip.demo.exception.SNRepetitiveException; | |
| 7 | +import com.ectrip.demo.service.SNService; | |
| 8 | 8 | import io.swagger.annotations.Api; | 
| 9 | 9 | import io.swagger.annotations.ApiOperation; | 
| 10 | 10 | import org.apache.logging.log4j.LogManager; | 
| 11 | 11 | import org.apache.logging.log4j.Logger; | 
| 12 | 12 | import org.springframework.beans.factory.annotation.Autowired; | 
| 13 | -import org.springframework.stereotype.Controller; | |
| 14 | 13 | import org.springframework.web.bind.annotation.*; | 
| 15 | 14 | |
| 16 | -import javax.servlet.http.HttpServletRequest; | |
| 17 | -import javax.servlet.http.HttpServletResponse; | |
| 18 | 15 | import javax.validation.Valid; | 
| 19 | -import java.io.IOException; | |
| 20 | 16 | |
| 21 | 17 | @Api(tags = "SN操作") | 
| 22 | 18 | @RestController | ... | ... | 
src/main/java/com/example/demo/dto/AddSNsDTO.java renamed to src/main/java/com/ectrip/demo/dto/AddSNsDTO.java
src/main/java/com/example/demo/dto/DeleteSNsDTO.java renamed to src/main/java/com/ectrip/demo/dto/DeleteSNsDTO.java
src/main/java/com/example/demo/exception/SNRepetitiveException.java renamed to src/main/java/com/ectrip/demo/exception/SNRepetitiveException.java
src/main/java/com/example/demo/service/SNService.java renamed to src/main/java/com/ectrip/demo/service/SNService.java
| 1 | -package com.example.demo.service; | |
| 1 | +package com.ectrip.demo.service; | |
| 2 | 2 | |
| 3 | -import com.example.demo.dto.AddSNsDTO; | |
| 4 | -import com.example.demo.dto.DeleteSNsDTO; | |
| 5 | -import com.example.demo.exception.SNRepetitiveException; | |
| 3 | +import com.ectrip.demo.dto.AddSNsDTO; | |
| 4 | +import com.ectrip.demo.dto.DeleteSNsDTO; | |
| 5 | +import com.ectrip.demo.exception.SNRepetitiveException; | |
| 6 | 6 | |
| 7 | 7 | import javax.servlet.http.HttpServletRequest; | 
| 8 | 8 | import javax.servlet.http.HttpServletResponse; | 
| 9 | -import java.io.FileNotFoundException; | |
| 10 | 9 | import java.io.IOException; | 
| 11 | 10 | |
| 12 | 11 | public interface SNService { | ... | ... | 
src/main/java/com/example/demo/service/impl/SNServiceImpl.java renamed to src/main/java/com/ectrip/demo/service/impl/SNServiceImpl.java
| 1 | -package com.example.demo.service.impl; | |
| 1 | +package com.ectrip.demo.service.impl; | |
| 2 | 2 | |
| 3 | 3 | import cn.hutool.core.codec.Base64; | 
| 4 | -import com.example.demo.controller.SNController; | |
| 5 | -import com.example.demo.dto.AddSNsDTO; | |
| 6 | -import com.example.demo.dto.DeleteSNsDTO; | |
| 7 | -import com.example.demo.exception.SNRepetitiveException; | |
| 8 | -import com.example.demo.service.SNService; | |
| 4 | +import com.ectrip.demo.dto.DeleteSNsDTO; | |
| 5 | +import com.ectrip.demo.service.SNService; | |
| 6 | +import com.ectrip.demo.dto.AddSNsDTO; | |
| 7 | +import com.ectrip.demo.exception.SNRepetitiveException; | |
| 9 | 8 | import org.apache.logging.log4j.LogManager; | 
| 10 | 9 | import org.apache.logging.log4j.Logger; | 
| 11 | 10 | import org.springframework.beans.factory.annotation.Value; | 
| ... | ... | @@ -14,6 +13,9 @@ import org.springframework.stereotype.Service; | 
| 14 | 13 | import javax.servlet.http.HttpServletRequest; | 
| 15 | 14 | import javax.servlet.http.HttpServletResponse; | 
| 16 | 15 | import java.io.*; | 
| 16 | +import java.nio.charset.StandardCharsets; | |
| 17 | +import java.nio.file.Files; | |
| 18 | +import java.nio.file.Paths; | |
| 17 | 19 | import java.text.SimpleDateFormat; | 
| 18 | 20 | import java.util.Date; | 
| 19 | 21 | import java.util.HashSet; | 
| ... | ... | @@ -49,11 +51,12 @@ public class SNServiceImpl implements SNService { | 
| 49 | 51 | String currTime = sdf.format(new Date()); | 
| 50 | 52 | //读取文件,判断SN是否重复 | 
| 51 | 53 | Set<String> set = new HashSet<>(); | 
| 52 | - BufferedReader bufferedReader = new BufferedReader(new FileReader(SNFilePath)); | |
| 54 | + Reader reader = new InputStreamReader(Files.newInputStream(Paths.get(SNFilePath)),StandardCharsets.UTF_8); | |
| 55 | + BufferedReader bufferedReader = new BufferedReader(reader); | |
| 53 | 56 | String br; | 
| 54 | 57 | while ((br=bufferedReader.readLine()) != null) { | 
| 55 | 58 | //截取SN | 
| 56 | - int index = br.indexOf('#'); | |
| 59 | + int index = br.indexOf("##"); | |
| 57 | 60 | String SN = br.substring(0,index == -1? 0 : index); | 
| 58 | 61 | set.add(SN); | 
| 59 | 62 | } | 
| ... | ... | @@ -67,11 +70,11 @@ public class SNServiceImpl implements SNService { | 
| 67 | 70 | |
| 68 | 71 | |
| 69 | 72 | //写入 | 
| 70 | - FileWriter fileWriter = new FileWriter(SNFilePath,true); | |
| 71 | - fileWriter.write(SNs.toString()); | |
| 72 | - fileWriter.flush(); | |
| 73 | - fileWriter.close(); | |
| 74 | - logger.info("-----------------------------结束添加SNs-----------------------------"); | |
| 73 | + FileOutputStream fileOutputStream = new FileOutputStream(SNFilePath,true); | |
| 74 | + fileOutputStream.write(SNs.toString().getBytes(StandardCharsets.UTF_8)); | |
| 75 | + fileOutputStream.flush(); | |
| 76 | + fileOutputStream.close(); | |
| 77 | + logger.info("成功写入SNs:{}",SNs); | |
| 75 | 78 | } finally { //释放锁 | 
| 76 | 79 | lock.unlock(); | 
| 77 | 80 | } | 
| ... | ... | @@ -79,15 +82,18 @@ public class SNServiceImpl implements SNService { | 
| 79 | 82 | |
| 80 | 83 | @Override | 
| 81 | 84 | public String getEncryptSNs() throws IOException { | 
| 82 | - File file = new File(SNFilePath); | |
| 83 | 85 | //读取文件 | 
| 84 | - byte[] SNsByteArr = new byte[(int) file.length()]; | |
| 85 | - FileInputStream fileInputStream = new FileInputStream(file); | |
| 86 | - fileInputStream.read(SNsByteArr); | |
| 87 | - //加密 | |
| 88 | - String encodeSNs = Base64.encode(SNsByteArr); | |
| 86 | + Reader reader = new InputStreamReader(Files.newInputStream(Paths.get(SNFilePath)),StandardCharsets.UTF_8); | |
| 87 | + char[] chars = new char[1000]; | |
| 88 | + StringBuilder builder = new StringBuilder(); | |
| 89 | + int len = reader.read(chars); | |
| 90 | + while (len != -1) { | |
| 91 | + builder.append(chars,0,len); | |
| 92 | + len = reader.read(chars); | |
| 93 | + } | |
| 89 | 94 | |
| 90 | - return encodeSNs; | |
| 95 | + //加密 | |
| 96 | + return Base64.encode(builder.toString()); | |
| 91 | 97 | } | 
| 92 | 98 | |
| 93 | 99 | @Override | 
| ... | ... | @@ -99,7 +105,8 @@ public class SNServiceImpl implements SNService { | 
| 99 | 105 | HashSet<String> deleteSNsSet = new HashSet<>(deleteSNsDTO.getSns()); | 
| 100 | 106 | |
| 101 | 107 | //读取文件,记录不用删除的数据 | 
| 102 | - BufferedReader bufferedReader = new BufferedReader(new FileReader(SNFilePath)); | |
| 108 | + Reader reader = new InputStreamReader(Files.newInputStream(new File(SNFilePath).toPath()), StandardCharsets.UTF_8); | |
| 109 | + BufferedReader bufferedReader = new BufferedReader(reader); | |
| 103 | 110 | String br; | 
| 104 | 111 | StringBuilder stringBuilder = new StringBuilder(); | 
| 105 | 112 | StringBuilder existSNs = new StringBuilder(); | 
| ... | ... | @@ -107,33 +114,33 @@ public class SNServiceImpl implements SNService { | 
| 107 | 114 | boolean SNExist = false; | 
| 108 | 115 | while ((br=bufferedReader.readLine()) != null) { | 
| 109 | 116 | //截取SN | 
| 110 | - int index = br.indexOf('#'); | |
| 117 | + int index = br.indexOf("##"); | |
| 111 | 118 | String str = br.substring(0,index == -1? 0 : index); | 
| 112 | 119 | if (!deleteSNsSet.contains(str)) { | 
| 113 | 120 | stringBuilder.append(br); | 
| 114 | 121 | stringBuilder.append(System.lineSeparator()); | 
| 115 | 122 | } else { | 
| 116 | - existSNs.append(str).append("、"); | |
| 123 | + existSNs.append(br).append(System.lineSeparator()); | |
| 117 | 124 | SNExist = true; | 
| 118 | 125 | } | 
| 119 | 126 | } | 
| 120 | 127 | bufferedReader.close(); | 
| 121 | 128 | |
| 122 | - //如果存在要删除的SN,先将数据备份,再将留存数据覆盖掉原SN文件,避免数据写一半断电丢失。 | |
| 129 | + //如果存在要删除的SN,先将数据备份,再将留存数据覆盖掉原SN文件。 | |
| 123 | 130 | if (SNExist) { | 
| 124 | 131 | //备份数据 | 
| 125 | - FileWriter SNTempfileWriter = new FileWriter(SNTempFilePath); | |
| 126 | - SNTempfileWriter.write(stringBuilder.toString()); | |
| 127 | - SNTempfileWriter.flush(); | |
| 128 | - SNTempfileWriter.close(); | |
| 132 | + FileOutputStream tempFileOutputStream = new FileOutputStream(SNTempFilePath); | |
| 133 | + tempFileOutputStream.write(stringBuilder.toString().getBytes(StandardCharsets.UTF_8)); | |
| 134 | + tempFileOutputStream.flush(); | |
| 135 | + tempFileOutputStream.close(); | |
| 129 | 136 | //覆盖掉原来的数据到SN文件中 | 
| 130 | - FileWriter SNfileWriter = new FileWriter(SNFilePath); | |
| 131 | - SNfileWriter.write(stringBuilder.toString()); | |
| 132 | - SNfileWriter.flush(); | |
| 133 | - SNfileWriter.close(); | |
| 134 | - | |
| 137 | + FileOutputStream fileOutputStream = new FileOutputStream(SNFilePath); | |
| 138 | + fileOutputStream.write(stringBuilder.toString().getBytes(StandardCharsets.UTF_8)); | |
| 139 | + fileOutputStream.flush(); | |
| 140 | + fileOutputStream.close(); | |
| 141 | + logger.warn("成功删除SNs:{}",existSNs); | |
| 135 | 142 | } | 
| 136 | - logger.warn("成功删除SNs:{}",existSNs); | |
| 143 | + | |
| 137 | 144 | logger.info("-----------------------------结束删除SNs-----------------------------"); | 
| 138 | 145 | |
| 139 | 146 | ... | ... | 
src/main/java/com/example/demo/util/HttpResult.java renamed to src/main/java/com/ectrip/demo/util/HttpResult.java
src/main/resources/log4j2.xml
| ... | ... | @@ -56,7 +56,7 @@ | 
| 56 | 56 | <logger name="org.mybatis" level="INFO"></logger> | 
| 57 | 57 | <root level="all"> | 
| 58 | 58 | <appender-ref ref="Console"/> | 
| 59 | -<!-- <appender-ref ref="RollingFileInfo"/>--> | |
| 59 | + <appender-ref ref="RollingFileInfo"/> | |
| 60 | 60 | <appender-ref ref="RollingFileWarn"/> | 
| 61 | 61 | <appender-ref ref="RollingFileError"/> | 
| 62 | 62 | </root> | ... | ... | 
src/main/resources/sn/SN.txt
src/main/resources/sn/SNTemp.txt