修复文本文档以'~'结尾报错
parent
5704a4182e
commit
e9c4e134c6
|
@ -20,7 +20,7 @@ public class ConfigUtils {
|
||||||
userDir = userDir.substring(0, userDir.length() - 4);
|
userDir = userDir.substring(0, userDir.length() - 4);
|
||||||
} else {
|
} else {
|
||||||
String separator = File.separator;
|
String separator = File.separator;
|
||||||
if (userDir.contains(MAIN_DIRECTORY_NAME)) {
|
if (userDir.endsWith(MAIN_DIRECTORY_NAME)) {
|
||||||
userDir = userDir + separator + "src" + separator + "main";
|
userDir = userDir + separator + "src" + separator + "main";
|
||||||
} else {
|
} else {
|
||||||
userDir = userDir + separator + MAIN_DIRECTORY_NAME + separator + "src" + separator + "main";
|
userDir = userDir + separator + MAIN_DIRECTORY_NAME + separator + "src" + separator + "main";
|
||||||
|
|
|
@ -12,6 +12,9 @@ import org.springframework.ui.Model;
|
||||||
import org.springframework.web.util.HtmlUtils;
|
import org.springframework.web.util.HtmlUtils;
|
||||||
|
|
||||||
import java.io.*;
|
import java.io.*;
|
||||||
|
import java.nio.charset.StandardCharsets;
|
||||||
|
import java.nio.file.Files;
|
||||||
|
import java.nio.file.Paths;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Created by kl on 2018/1/17.
|
* Created by kl on 2018/1/17.
|
||||||
|
@ -28,16 +31,14 @@ public class SimTextFilePreviewImpl implements FilePreview {
|
||||||
private static final String FILE_DIR = ConfigConstants.getFileDir();
|
private static final String FILE_DIR = ConfigConstants.getFileDir();
|
||||||
@Override
|
@Override
|
||||||
public String filePreviewHandle(String url, Model model, FileAttribute fileAttribute) {
|
public String filePreviewHandle(String url, Model model, FileAttribute fileAttribute) {
|
||||||
|
|
||||||
String fileName = fileAttribute.getName();
|
String fileName = fileAttribute.getName();
|
||||||
String baseUrll = FILE_DIR + fileName;
|
String filePath = FILE_DIR + fileName;
|
||||||
// String suffix = fileAttribute.getSuffix();
|
|
||||||
ReturnResponse<String> response = DownloadUtils.downLoad(fileAttribute, fileName);
|
ReturnResponse<String> response = DownloadUtils.downLoad(fileAttribute, fileName);
|
||||||
if (response.isFailure()) {
|
if (response.isFailure()) {
|
||||||
return otherFilePreview.notSupportedFile(model, fileAttribute, response.getMsg());
|
return otherFilePreview.notSupportedFile(model, fileAttribute, response.getMsg());
|
||||||
}
|
}
|
||||||
try {
|
try {
|
||||||
String fileData = HtmlUtils.htmlEscape(textData(baseUrll));
|
String fileData = HtmlUtils.htmlEscape(textData(filePath));
|
||||||
model.addAttribute("textData", Base64.encodeBase64String(fileData.getBytes()));
|
model.addAttribute("textData", Base64.encodeBase64String(fileData.getBytes()));
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
return otherFilePreview.notSupportedFile(model, fileAttribute, e.getLocalizedMessage());
|
return otherFilePreview.notSupportedFile(model, fileAttribute, e.getLocalizedMessage());
|
||||||
|
@ -45,14 +46,16 @@ public class SimTextFilePreviewImpl implements FilePreview {
|
||||||
return TXT_FILE_PREVIEW_PAGE;
|
return TXT_FILE_PREVIEW_PAGE;
|
||||||
}
|
}
|
||||||
|
|
||||||
private String textData(String baseUrll) throws IOException {
|
private String textData(String filePath) throws IOException {
|
||||||
File file = new File(baseUrll);
|
File file = new File(filePath);
|
||||||
if(!file.exists() || file.length() == 0) {
|
if (!file.exists() || file.length() == 0) {
|
||||||
return "";
|
return "";
|
||||||
}else {
|
} else {
|
||||||
String charset = EncodingDetects.getJavaEncode(baseUrll);
|
String charset = EncodingDetects.getJavaEncode(filePath);
|
||||||
System.out.println(charset);
|
if ("ASCII".equals(charset)) {
|
||||||
BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(baseUrll), charset));
|
charset = StandardCharsets.US_ASCII.name();
|
||||||
|
}
|
||||||
|
BufferedReader br = new BufferedReader(new InputStreamReader(Files.newInputStream(Paths.get(filePath)), charset));
|
||||||
StringBuilder result = new StringBuilder();
|
StringBuilder result = new StringBuilder();
|
||||||
String line;
|
String line;
|
||||||
while ((line = br.readLine()) != null) {
|
while ((line = br.readLine()) != null) {
|
||||||
|
|
|
@ -402,7 +402,7 @@ class BytesEncodingDetect extends Encoding {
|
||||||
int row, column;
|
int row, column;
|
||||||
rawtextlen = rawtext.length;
|
rawtextlen = rawtext.length;
|
||||||
for (i = 0; i < rawtextlen; i++) {
|
for (i = 0; i < rawtextlen; i++) {
|
||||||
if (rawtext[i] == '~') {
|
if (rawtext[i] == '~' && i < rawtextlen - 1) {
|
||||||
if (rawtext[i + 1] == '{') {
|
if (rawtext[i + 1] == '{') {
|
||||||
hzstart++;
|
hzstart++;
|
||||||
i += 2;
|
i += 2;
|
||||||
|
@ -4641,4 +4641,4 @@ class Encoding {
|
||||||
nicename[OTHER] = "OTHER";
|
nicename[OTHER] = "OTHER";
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue