Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
S
slm-fileview
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
梁杰芳
slm-fileview
Commits
0a1ff64d
Commit
0a1ff64d
authored
Apr 15, 2021
by
gaoxiong
Committed by
kl
Jun 17, 2021
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
修复某些情况下txt文本乱码问题
parent
176f3807
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
26 additions
and
7 deletions
+26
-7
EncodingDetects.java
...src/main/java/cn/keking/service/impl/EncodingDetects.java
+0
-0
SimTextFilePreviewImpl.java
...n/java/cn/keking/service/impl/SimTextFilePreviewImpl.java
+26
-7
No files found.
server/src/main/java/cn/keking/service/impl/EncodingDetects.java
0 → 100644
View file @
0a1ff64d
This source diff could not be displayed because it is too large. You can
view the blob
instead.
server/src/main/java/cn/keking/service/impl/SimTextFilePreviewImpl.java
View file @
0a1ff64d
package
cn
.
keking
.
service
.
impl
;
import
cn.keking.config.ConfigConstants
;
import
cn.keking.model.FileAttribute
;
import
cn.keking.model.ReturnResponse
;
import
cn.keking.service.FilePreview
;
...
...
@@ -11,8 +12,7 @@ import org.springframework.stereotype.Service;
import
org.springframework.ui.Model
;
import
org.springframework.web.util.HtmlUtils
;
import
java.io.File
;
import
java.io.IOException
;
import
java.io.*
;
import
java.nio.charset.StandardCharsets
;
/**
...
...
@@ -27,19 +27,19 @@ public class SimTextFilePreviewImpl implements FilePreview {
public
SimTextFilePreviewImpl
(
OtherFilePreviewImpl
otherFilePreview
)
{
this
.
otherFilePreview
=
otherFilePreview
;
}
private
static
final
String
FILE_DIR
=
ConfigConstants
.
getFileDir
();
@Override
public
String
filePreviewHandle
(
String
url
,
Model
model
,
FileAttribute
fileAttribute
)
{
String
fileName
=
fileAttribute
.
getName
();
String
baseUrll
=
FILE_DIR
+
fileName
;
// String suffix = fileAttribute.getSuffix();
ReturnResponse
<
String
>
response
=
DownloadUtils
.
downLoad
(
fileAttribute
,
fileName
);
if
(
response
.
isFailure
())
{
return
otherFilePreview
.
notSupportedFile
(
model
,
fileAttribute
,
response
.
getMsg
());
}
try
{
File
originFile
=
new
File
(
response
.
getContent
());
String
charset
=
KkFileUtils
.
getFileEncode
(
originFile
);
String
fileData
=
FileUtil
.
readString
(
originFile
,
charset
);
fileData
=
HtmlUtils
.
htmlEscape
(
fileData
,
StandardCharsets
.
UTF_8
.
name
());
String
fileData
=
HtmlUtils
.
htmlEscape
(
textData
(
baseUrll
));
model
.
addAttribute
(
"textData"
,
Base64
.
encodeBase64String
(
fileData
.
getBytes
()));
}
catch
(
IOException
e
)
{
return
otherFilePreview
.
notSupportedFile
(
model
,
fileAttribute
,
e
.
getLocalizedMessage
());
...
...
@@ -47,4 +47,23 @@ public class SimTextFilePreviewImpl implements FilePreview {
return
TXT_FILE_PREVIEW_PAGE
;
}
private
String
textData
(
String
baseUrll
)
throws
IOException
{
File
file
=
new
File
(
baseUrll
);
if
(!
file
.
exists
()
||
file
.
length
()
==
0
)
{
String
line
=
""
;
return
line
;
}
else
{
String
charset
=
EncodingDetects
.
getJavaEncode
(
baseUrll
);
System
.
out
.
println
(
charset
);
BufferedReader
br
=
new
BufferedReader
(
new
InputStreamReader
(
new
FileInputStream
(
baseUrll
),
charset
));
StringBuilder
result
=
new
StringBuilder
();
String
line
;
while
((
line
=
br
.
readLine
())
!=
null
)
{
result
.
append
(
line
).
append
(
"\r\n"
);
}
return
result
.
toString
();
}
}
}
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