Commit 6228a7a594df992f4562fbc45c646b7c66dbc2ce
1 parent
03d2bc11
SNManage commit
Showing
12 changed files
with
65 additions
and
63 deletions
pom.xml
@@ -4,8 +4,8 @@ | @@ -4,8 +4,8 @@ | ||
4 | xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | 4 | xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> |
5 | <modelVersion>4.0.0</modelVersion> | 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 | <version>1.0</version> | 9 | <version>1.0</version> |
10 | 10 | ||
11 | <properties> | 11 | <properties> |
@@ -33,10 +33,6 @@ | @@ -33,10 +33,6 @@ | ||
33 | <scope>test</scope> | 33 | <scope>test</scope> |
34 | </dependency> | 34 | </dependency> |
35 | <dependency> | 35 | <dependency> |
36 | - <groupId>org.springframework.boot</groupId> | ||
37 | - <artifactId>spring-boot-starter-thymeleaf</artifactId> | ||
38 | - </dependency> | ||
39 | - <dependency> | ||
40 | <groupId>io.springfox</groupId> | 36 | <groupId>io.springfox</groupId> |
41 | <artifactId>springfox-swagger2</artifactId> | 37 | <artifactId>springfox-swagger2</artifactId> |
42 | <version>2.7.0</version> | 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 | import org.springframework.boot.SpringApplication; | 3 | import org.springframework.boot.SpringApplication; |
5 | import org.springframework.boot.autoconfigure.SpringBootApplication; | 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; |
6 | import springfox.documentation.swagger2.annotations.EnableSwagger2; | 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 | import io.swagger.annotations.Api; | 8 | import io.swagger.annotations.Api; |
9 | import io.swagger.annotations.ApiOperation; | 9 | import io.swagger.annotations.ApiOperation; |
10 | import org.apache.logging.log4j.LogManager; | 10 | import org.apache.logging.log4j.LogManager; |
11 | import org.apache.logging.log4j.Logger; | 11 | import org.apache.logging.log4j.Logger; |
12 | import org.springframework.beans.factory.annotation.Autowired; | 12 | import org.springframework.beans.factory.annotation.Autowired; |
13 | -import org.springframework.stereotype.Controller; | ||
14 | import org.springframework.web.bind.annotation.*; | 13 | import org.springframework.web.bind.annotation.*; |
15 | 14 | ||
16 | -import javax.servlet.http.HttpServletRequest; | ||
17 | -import javax.servlet.http.HttpServletResponse; | ||
18 | import javax.validation.Valid; | 15 | import javax.validation.Valid; |
19 | -import java.io.IOException; | ||
20 | 16 | ||
21 | @Api(tags = "SN操作") | 17 | @Api(tags = "SN操作") |
22 | @RestController | 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 | import javax.servlet.http.HttpServletRequest; | 7 | import javax.servlet.http.HttpServletRequest; |
8 | import javax.servlet.http.HttpServletResponse; | 8 | import javax.servlet.http.HttpServletResponse; |
9 | -import java.io.FileNotFoundException; | ||
10 | import java.io.IOException; | 9 | import java.io.IOException; |
11 | 10 | ||
12 | public interface SNService { | 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 | import cn.hutool.core.codec.Base64; | 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 | import org.apache.logging.log4j.LogManager; | 8 | import org.apache.logging.log4j.LogManager; |
10 | import org.apache.logging.log4j.Logger; | 9 | import org.apache.logging.log4j.Logger; |
11 | import org.springframework.beans.factory.annotation.Value; | 10 | import org.springframework.beans.factory.annotation.Value; |
@@ -14,6 +13,9 @@ import org.springframework.stereotype.Service; | @@ -14,6 +13,9 @@ import org.springframework.stereotype.Service; | ||
14 | import javax.servlet.http.HttpServletRequest; | 13 | import javax.servlet.http.HttpServletRequest; |
15 | import javax.servlet.http.HttpServletResponse; | 14 | import javax.servlet.http.HttpServletResponse; |
16 | import java.io.*; | 15 | import java.io.*; |
16 | +import java.nio.charset.StandardCharsets; | ||
17 | +import java.nio.file.Files; | ||
18 | +import java.nio.file.Paths; | ||
17 | import java.text.SimpleDateFormat; | 19 | import java.text.SimpleDateFormat; |
18 | import java.util.Date; | 20 | import java.util.Date; |
19 | import java.util.HashSet; | 21 | import java.util.HashSet; |
@@ -49,11 +51,12 @@ public class SNServiceImpl implements SNService { | @@ -49,11 +51,12 @@ public class SNServiceImpl implements SNService { | ||
49 | String currTime = sdf.format(new Date()); | 51 | String currTime = sdf.format(new Date()); |
50 | //读取文件,判断SN是否重复 | 52 | //读取文件,判断SN是否重复 |
51 | Set<String> set = new HashSet<>(); | 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 | String br; | 56 | String br; |
54 | while ((br=bufferedReader.readLine()) != null) { | 57 | while ((br=bufferedReader.readLine()) != null) { |
55 | //截取SN | 58 | //截取SN |
56 | - int index = br.indexOf('#'); | 59 | + int index = br.indexOf("##"); |
57 | String SN = br.substring(0,index == -1? 0 : index); | 60 | String SN = br.substring(0,index == -1? 0 : index); |
58 | set.add(SN); | 61 | set.add(SN); |
59 | } | 62 | } |
@@ -67,11 +70,11 @@ public class SNServiceImpl implements SNService { | @@ -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 | } finally { //释放锁 | 78 | } finally { //释放锁 |
76 | lock.unlock(); | 79 | lock.unlock(); |
77 | } | 80 | } |
@@ -79,15 +82,18 @@ public class SNServiceImpl implements SNService { | @@ -79,15 +82,18 @@ public class SNServiceImpl implements SNService { | ||
79 | 82 | ||
80 | @Override | 83 | @Override |
81 | public String getEncryptSNs() throws IOException { | 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 | @Override | 99 | @Override |
@@ -99,7 +105,8 @@ public class SNServiceImpl implements SNService { | @@ -99,7 +105,8 @@ public class SNServiceImpl implements SNService { | ||
99 | HashSet<String> deleteSNsSet = new HashSet<>(deleteSNsDTO.getSns()); | 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 | String br; | 110 | String br; |
104 | StringBuilder stringBuilder = new StringBuilder(); | 111 | StringBuilder stringBuilder = new StringBuilder(); |
105 | StringBuilder existSNs = new StringBuilder(); | 112 | StringBuilder existSNs = new StringBuilder(); |
@@ -107,33 +114,33 @@ public class SNServiceImpl implements SNService { | @@ -107,33 +114,33 @@ public class SNServiceImpl implements SNService { | ||
107 | boolean SNExist = false; | 114 | boolean SNExist = false; |
108 | while ((br=bufferedReader.readLine()) != null) { | 115 | while ((br=bufferedReader.readLine()) != null) { |
109 | //截取SN | 116 | //截取SN |
110 | - int index = br.indexOf('#'); | 117 | + int index = br.indexOf("##"); |
111 | String str = br.substring(0,index == -1? 0 : index); | 118 | String str = br.substring(0,index == -1? 0 : index); |
112 | if (!deleteSNsSet.contains(str)) { | 119 | if (!deleteSNsSet.contains(str)) { |
113 | stringBuilder.append(br); | 120 | stringBuilder.append(br); |
114 | stringBuilder.append(System.lineSeparator()); | 121 | stringBuilder.append(System.lineSeparator()); |
115 | } else { | 122 | } else { |
116 | - existSNs.append(str).append("、"); | 123 | + existSNs.append(br).append(System.lineSeparator()); |
117 | SNExist = true; | 124 | SNExist = true; |
118 | } | 125 | } |
119 | } | 126 | } |
120 | bufferedReader.close(); | 127 | bufferedReader.close(); |
121 | 128 | ||
122 | - //如果存在要删除的SN,先将数据备份,再将留存数据覆盖掉原SN文件,避免数据写一半断电丢失。 | 129 | + //如果存在要删除的SN,先将数据备份,再将留存数据覆盖掉原SN文件。 |
123 | if (SNExist) { | 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 | //覆盖掉原来的数据到SN文件中 | 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 | logger.info("-----------------------------结束删除SNs-----------------------------"); | 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,7 +56,7 @@ | ||
56 | <logger name="org.mybatis" level="INFO"></logger> | 56 | <logger name="org.mybatis" level="INFO"></logger> |
57 | <root level="all"> | 57 | <root level="all"> |
58 | <appender-ref ref="Console"/> | 58 | <appender-ref ref="Console"/> |
59 | -<!-- <appender-ref ref="RollingFileInfo"/>--> | 59 | + <appender-ref ref="RollingFileInfo"/> |
60 | <appender-ref ref="RollingFileWarn"/> | 60 | <appender-ref ref="RollingFileWarn"/> |
61 | <appender-ref ref="RollingFileError"/> | 61 | <appender-ref ref="RollingFileError"/> |
62 | </root> | 62 | </root> |
src/main/resources/sn/SN.txt
src/main/resources/sn/SNTemp.txt