Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
D
device-new-platform
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
赵啸非
device-new-platform
Commits
895bd40a
Commit
895bd40a
authored
Feb 01, 2023
by
赵啸非
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
添加应用发布
parent
cb162782
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
223 additions
and
5 deletions
+223
-5
common-lib/src/main/java/com/mortals/xhx/utils/EncodeUtil.java
...n-lib/src/main/java/com/mortals/xhx/utils/EncodeUtil.java
+192
-0
device-manager/src/main/java/com/mortals/xhx/busiz/web/DeviceSendMsgController.java
...va/com/mortals/xhx/busiz/web/DeviceSendMsgController.java
+21
-1
device-manager/src/main/java/com/mortals/xhx/module/device/service/impl/DeviceModuleDistributeServiceImpl.java
...evice/service/impl/DeviceModuleDistributeServiceImpl.java
+10
-4
No files found.
common-lib/src/main/java/com/mortals/xhx/utils/EncodeUtil.java
0 → 100644
View file @
895bd40a
package
com.mortals.xhx.utils
;
import
org.slf4j.Logger
;
import
org.slf4j.LoggerFactory
;
import
java.io.*
;
import
java.util.BitSet
;
/**
* @author 自动识别文件编码格式
*/
public
class
EncodeUtil
{
private
static
Logger
logger
=
LoggerFactory
.
getLogger
(
EncodeUtil
.
class
);
private
static
int
BYTE_SIZE
=
8
;
public
static
String
CODE_UTF8
=
"UTF-8"
;
public
static
String
CODE_UTF8_BOM
=
"UTF-8_BOM"
;
public
static
String
CODE_GBK
=
"GBK"
;
/**
* 通过文件全名称获取编码集名称
*
* @param fullFileName
* @param ignoreBom
* @return
* @throws Exception
*/
public
static
String
getEncode
(
String
fullFileName
,
boolean
ignoreBom
)
throws
Exception
{
logger
.
debug
(
"fullFileName ; {}"
,
fullFileName
);
BufferedInputStream
bis
=
new
BufferedInputStream
(
new
FileInputStream
(
fullFileName
));
return
getEncode
(
bis
,
ignoreBom
);
}
/**
* 通过文件缓存流获取编码集名称,文件流必须为未曾
*
* @param bis
* @param ignoreBom 是否忽略utf-8 bom
* @return
* @throws Exception
*/
public
static
String
getEncode
(
BufferedInputStream
bis
,
boolean
ignoreBom
)
throws
Exception
{
bis
.
mark
(
0
);
String
encodeType
=
"未识别"
;
byte
[]
head
=
new
byte
[
3
];
bis
.
read
(
head
);
if
(
head
[
0
]
==
-
1
&&
head
[
1
]
==
-
2
)
{
encodeType
=
"UTF-16"
;
}
else
if
(
head
[
0
]
==
-
2
&&
head
[
1
]
==
-
1
)
{
encodeType
=
"Unicode"
;
}
else
if
(
head
[
0
]
==
-
17
&&
head
[
1
]
==
-
69
&&
head
[
2
]
==
-
65
)
{
//带BOM
if
(
ignoreBom
)
{
encodeType
=
CODE_UTF8
;
}
else
{
encodeType
=
CODE_UTF8_BOM
;
}
}
else
if
(
"Unicode"
.
equals
(
encodeType
))
{
encodeType
=
"UTF-16"
;
}
else
if
(
isUTF8
(
bis
))
{
encodeType
=
CODE_UTF8
;
}
else
{
encodeType
=
CODE_GBK
;
}
logger
.
info
(
"result encode type : "
+
encodeType
);
return
encodeType
;
}
/**
* 是否是无BOM的UTF8格式,不判断常规场景,只区分无BOM UTF8和GBK
*
* @param bis
* @return
*/
private
static
boolean
isUTF8
(
BufferedInputStream
bis
)
throws
Exception
{
bis
.
reset
();
//读取第一个字节
int
code
=
bis
.
read
();
do
{
BitSet
bitSet
=
convert2BitSet
(
code
);
//判断是否为单字节
if
(
bitSet
.
get
(
0
))
{
//多字节时,再读取N个字节
if
(!
checkMultiByte
(
bis
,
bitSet
))
{
//未检测通过,直接返回
return
false
;
}
}
else
{
//单字节时什么都不用做,再次读取字节
}
code
=
bis
.
read
();
}
while
(
code
!=
-
1
);
return
true
;
}
/**
* 检测多字节,判断是否为utf8,已经读取了一个字节
*
* @param bis
* @param bitSet
* @return
*/
private
static
boolean
checkMultiByte
(
BufferedInputStream
bis
,
BitSet
bitSet
)
throws
Exception
{
int
count
=
getCountOfSequential
(
bitSet
);
byte
[]
bytes
=
new
byte
[
count
-
1
];
//已经读取了一个字节,不能再读取
bis
.
read
(
bytes
);
for
(
byte
b
:
bytes
)
{
if
(!
checkUtf8Byte
(
b
))
{
return
false
;
}
}
return
true
;
}
/**
* 检测单字节,判断是否为utf8
*
* @param b
* @return
*/
private
static
boolean
checkUtf8Byte
(
byte
b
)
throws
Exception
{
BitSet
bitSet
=
convert2BitSet
(
b
);
return
bitSet
.
get
(
0
)
&&
!
bitSet
.
get
(
1
);
}
/**
* 检测bitSet中从开始有多少个连续的1
*
* @param bitSet
* @return
*/
private
static
int
getCountOfSequential
(
BitSet
bitSet
)
{
int
count
=
0
;
for
(
int
i
=
0
;
i
<
BYTE_SIZE
;
i
++)
{
if
(
bitSet
.
get
(
i
))
{
count
++;
}
else
{
break
;
}
}
return
count
;
}
/**
* 将整形转为BitSet
*
* @param code
* @return
*/
private
static
BitSet
convert2BitSet
(
int
code
)
{
BitSet
bitSet
=
new
BitSet
(
BYTE_SIZE
);
for
(
int
i
=
0
;
i
<
BYTE_SIZE
;
i
++)
{
int
tmp3
=
code
>>
(
BYTE_SIZE
-
i
-
1
);
int
tmp2
=
0x1
&
tmp3
;
if
(
tmp2
==
1
)
{
bitSet
.
set
(
i
);
}
}
return
bitSet
;
}
/**
* 将一指定编码的文件转换为另一编码的文件
*
* @param oldFullFileName
* @param oldCharsetName
* @param newFullFileName
* @param newCharsetName
*/
public
static
void
convert
(
String
oldFullFileName
,
String
oldCharsetName
,
String
newFullFileName
,
String
newCharsetName
)
throws
Exception
{
logger
.
info
(
"the old file name is : {}, The oldCharsetName is : {}"
,
oldFullFileName
,
oldCharsetName
);
logger
.
info
(
"the new file name is : {}, The newCharsetName is : {}"
,
newFullFileName
,
newCharsetName
);
StringBuffer
content
=
new
StringBuffer
();
BufferedReader
bin
=
new
BufferedReader
(
new
InputStreamReader
(
new
FileInputStream
(
oldFullFileName
),
oldCharsetName
));
String
line
;
while
((
line
=
bin
.
readLine
())
!=
null
)
{
content
.
append
(
line
);
content
.
append
(
System
.
getProperty
(
"line.separator"
));
}
newFullFileName
=
newFullFileName
.
replace
(
"\\"
,
"/"
);
File
dir
=
new
File
(
newFullFileName
.
substring
(
0
,
newFullFileName
.
lastIndexOf
(
"/"
)));
if
(!
dir
.
exists
())
{
dir
.
mkdirs
();
}
Writer
out
=
new
OutputStreamWriter
(
new
FileOutputStream
(
newFullFileName
),
newCharsetName
);
out
.
write
(
content
.
toString
());
}
}
device-manager/src/main/java/com/mortals/xhx/busiz/web/DeviceSendMsgController.java
View file @
895bd40a
...
...
@@ -9,12 +9,15 @@ import com.mortals.xhx.base.system.message.impl.MessageProducer;
import
com.mortals.xhx.busiz.req.UploadDeviceReq
;
import
com.mortals.xhx.busiz.rsp.ApiResp
;
import
com.mortals.xhx.common.code.ApiRespCodeEnum
;
import
com.mortals.xhx.common.code.LogTypeEnum
;
import
com.mortals.xhx.common.key.Constant
;
import
com.mortals.xhx.common.key.QueueKey
;
import
com.mortals.xhx.common.model.DefaultTbQueueMsgHeaders
;
import
com.mortals.xhx.common.model.MessageHeader
;
import
com.mortals.xhx.module.device.model.DeviceEntity
;
import
com.mortals.xhx.module.device.model.DeviceLogEntity
;
import
com.mortals.xhx.module.device.model.DeviceQuery
;
import
com.mortals.xhx.module.device.service.DeviceLogService
;
import
com.mortals.xhx.module.device.service.DeviceService
;
import
com.mortals.xhx.queue.DefaultTbQueueMsg
;
import
com.mortals.xhx.queue.TbQueueMsg
;
...
...
@@ -26,6 +29,7 @@ import org.springframework.web.bind.annotation.RequestBody;
import
org.springframework.web.bind.annotation.RequestMapping
;
import
org.springframework.web.bind.annotation.RestController
;
import
java.util.Date
;
import
java.util.List
;
import
static
com
.
mortals
.
xhx
.
common
.
key
.
Constant
.
MESSAGETYPE_NOTIFY_REFRESH
;
...
...
@@ -38,6 +42,8 @@ public class DeviceSendMsgController {
private
DeviceService
deviceService
;
@Autowired
private
MessageProducer
messageProducer
;
@Autowired
private
DeviceLogService
deviceLogService
;
...
...
@@ -56,9 +62,23 @@ public class DeviceSendMsgController {
header
.
put
(
MessageHeader
.
MESSAGETYPE
,
MESSAGETYPE_NOTIFY_REFRESH
);
header
.
put
(
MessageHeader
.
DEVICECODE
,
deviceEntity
.
getDeviceCode
());
header
.
put
(
MessageHeader
.
TIMESTAMP
,
DateUtils
.
getCurrStrDateTime
());
TbQueueMsg
queueMsg
=
new
DefaultTbQueueMsg
(
IdUtil
.
fastUUID
(),
"W10="
,
header
);
messageProducer
.
sendMsg
(
QueueKey
.
DEFAULT_EXCHANGE
,
Constant
.
DOWN_TOPIC
+
deviceEntity
.
getDeviceCode
(),
JSON
.
toJSONString
(
queueMsg
));
DeviceLogEntity
deviceLogEntity
=
new
DeviceLogEntity
();
deviceLogEntity
.
initAttrValue
();
deviceLogEntity
.
setTraceID
(
IdUtil
.
fastSimpleUUID
());
deviceLogEntity
.
setSiteId
(
deviceEntity
.
getSiteId
());
deviceLogEntity
.
setDeviceId
(
deviceEntity
.
getId
());
deviceLogEntity
.
setDeviceName
(
deviceEntity
.
getDeviceName
());
deviceLogEntity
.
setDeviceCode
(
deviceEntity
.
getDeviceCode
());
deviceLogEntity
.
setMessageHead
(
MESSAGETYPE_NOTIFY_REFRESH
);
deviceLogEntity
.
setContent
(
"W10="
);
deviceLogEntity
.
setLogType
(
LogTypeEnum
.
下发服务
.
getValue
());
deviceLogEntity
.
setCreateUserId
(
1L
);
deviceLogEntity
.
setCreateTime
(
new
Date
());
deviceLogService
.
save
(
deviceLogEntity
,
null
);
}
}
catch
(
Exception
e
)
{
log
.
error
(
"接收数据失败"
,
e
);
...
...
device-manager/src/main/java/com/mortals/xhx/module/device/service/impl/DeviceModuleDistributeServiceImpl.java
View file @
895bd40a
...
...
@@ -16,6 +16,7 @@ import com.mortals.xhx.module.device.model.DeviceModuleDistributeEntity;
import
com.mortals.xhx.module.device.service.DeviceModuleDistributeService
;
import
com.mortals.xhx.module.product.model.ProductEntity
;
import
com.mortals.xhx.module.product.service.ProductService
;
import
com.mortals.xhx.utils.EncodeUtil
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.stereotype.Service
;
...
...
@@ -97,15 +98,20 @@ public class DeviceModuleDistributeServiceImpl extends AbstractCRUDServiceImpl<D
File
file
=
new
File
(
"F:\\交警窗口证件照.zip"
);
String
disPath
=
"F:\\pics\\"
;
ZipUtil
.
unzip
(
"F:\\交警窗口证件照.zip"
,
disPath
,
Charset
.
forName
(
"GBK"
));
String
fileEncode
=
"UTF-8"
;
try
{
fileEncode
=
EncodeUtil
.
getEncode
(
"F:\\1672973316144.zip"
,
true
);
}
catch
(
Exception
e
)
{
File
[]
files
=
FileUtil
.
ls
(
disPath
);
for
(
File
file1
:
files
)
{
}
ZipUtil
.
unzip
(
"F:\\1672973316144.zip"
,
disPath
,
Charset
.
forName
(
fileEncode
));
/* File[] files = FileUtil.ls(disPath);
for (File file1 : files) {
System.out.println(file1.getName());
}
}
*/
/* System.out.println(file.getName());
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment