Browse Source

修复文本文档以'~'结尾报错

pull/29/MERGE
陈精华 2 years ago
parent
commit
e9c4e134c6
No known key found for this signature in database
GPG Key ID: 30BDC970902B755D
  1. 2
      office-plugin/src/main/java/org/artofsolving/jodconverter/util/ConfigUtils.java
  2. 25
      server/src/main/java/cn/keking/service/impl/SimTextFilePreviewImpl.java
  3. 4
      server/src/main/java/cn/keking/utils/EncodingDetects.java

2
office-plugin/src/main/java/org/artofsolving/jodconverter/util/ConfigUtils.java

@ -20,7 +20,7 @@ public class ConfigUtils {
userDir = userDir.substring(0, userDir.length() - 4);
} else {
String separator = File.separator;
if (userDir.contains(MAIN_DIRECTORY_NAME)) {
if (userDir.endsWith(MAIN_DIRECTORY_NAME)) {
userDir = userDir + separator + "src" + separator + "main";
} else {
userDir = userDir + separator + MAIN_DIRECTORY_NAME + separator + "src" + separator + "main";

25
server/src/main/java/cn/keking/service/impl/SimTextFilePreviewImpl.java

@ -12,6 +12,9 @@ import org.springframework.ui.Model;
import org.springframework.web.util.HtmlUtils;
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.
@ -28,16 +31,14 @@ public class SimTextFilePreviewImpl implements FilePreview {
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();
String filePath = FILE_DIR + fileName;
ReturnResponse<String> response = DownloadUtils.downLoad(fileAttribute, fileName);
if (response.isFailure()) {
return otherFilePreview.notSupportedFile(model, fileAttribute, response.getMsg());
}
try {
String fileData = HtmlUtils.htmlEscape(textData(baseUrll));
String fileData = HtmlUtils.htmlEscape(textData(filePath));
model.addAttribute("textData", Base64.encodeBase64String(fileData.getBytes()));
} catch (IOException e) {
return otherFilePreview.notSupportedFile(model, fileAttribute, e.getLocalizedMessage());
@ -45,14 +46,16 @@ 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) {
private String textData(String filePath) throws IOException {
File file = new File(filePath);
if (!file.exists() || file.length() == 0) {
return "";
}else {
String charset = EncodingDetects.getJavaEncode(baseUrll);
System.out.println(charset);
BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(baseUrll), charset));
} else {
String charset = EncodingDetects.getJavaEncode(filePath);
if ("ASCII".equals(charset)) {
charset = StandardCharsets.US_ASCII.name();
}
BufferedReader br = new BufferedReader(new InputStreamReader(Files.newInputStream(Paths.get(filePath)), charset));
StringBuilder result = new StringBuilder();
String line;
while ((line = br.readLine()) != null) {

4
server/src/main/java/cn/keking/utils/EncodingDetects.java

@ -402,7 +402,7 @@ class BytesEncodingDetect extends Encoding {
int row, column;
rawtextlen = rawtext.length;
for (i = 0; i < rawtextlen; i++) {
if (rawtext[i] == '~') {
if (rawtext[i] == '~' && i < rawtextlen - 1) {
if (rawtext[i + 1] == '{') {
hzstart++;
i += 2;
@ -4641,4 +4641,4 @@ class Encoding {
nicename[OTHER] = "OTHER";
}
}
}

Loading…
Cancel
Save