2018-01-17 17:51:53 +08:00
|
|
|
package cn.keking.service.impl;
|
|
|
|
|
|
2021-04-15 17:46:44 +08:00
|
|
|
import cn.keking.config.ConfigConstants;
|
2018-01-17 17:51:53 +08:00
|
|
|
import cn.keking.model.FileAttribute;
|
2018-01-19 14:51:18 +08:00
|
|
|
import cn.keking.model.ReturnResponse;
|
2018-01-17 17:51:53 +08:00
|
|
|
import cn.keking.service.FilePreview;
|
2020-05-15 18:09:19 +08:00
|
|
|
import cn.keking.utils.DownloadUtils;
|
2020-12-28 18:21:35 +08:00
|
|
|
import cn.keking.utils.KkFileUtils;
|
2021-01-09 15:38:21 +08:00
|
|
|
import jodd.io.FileUtil;
|
2020-12-29 18:32:24 +08:00
|
|
|
import org.apache.commons.codec.binary.Base64;
|
2018-01-17 17:51:53 +08:00
|
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
import org.springframework.ui.Model;
|
2021-03-18 20:12:50 +08:00
|
|
|
import org.springframework.web.util.HtmlUtils;
|
2018-01-17 17:51:53 +08:00
|
|
|
|
2021-04-15 17:46:44 +08:00
|
|
|
import java.io.*;
|
2021-03-18 20:12:50 +08:00
|
|
|
import java.nio.charset.StandardCharsets;
|
2019-10-25 14:37:31 +08:00
|
|
|
|
2018-01-17 17:51:53 +08:00
|
|
|
/**
|
|
|
|
|
* Created by kl on 2018/1/17.
|
|
|
|
|
* Content :处理文本文件
|
|
|
|
|
*/
|
|
|
|
|
@Service
|
2020-05-15 18:09:19 +08:00
|
|
|
public class SimTextFilePreviewImpl implements FilePreview {
|
2018-01-17 17:51:53 +08:00
|
|
|
|
2020-12-27 14:47:28 +08:00
|
|
|
private final OtherFilePreviewImpl otherFilePreview;
|
|
|
|
|
|
|
|
|
|
public SimTextFilePreviewImpl(OtherFilePreviewImpl otherFilePreview) {
|
|
|
|
|
this.otherFilePreview = otherFilePreview;
|
|
|
|
|
}
|
2021-04-15 17:46:44 +08:00
|
|
|
private static final String FILE_DIR = ConfigConstants.getFileDir();
|
2018-01-17 17:51:53 +08:00
|
|
|
@Override
|
2020-12-25 21:02:51 +08:00
|
|
|
public String filePreviewHandle(String url, Model model, FileAttribute fileAttribute) {
|
2021-04-15 17:46:44 +08:00
|
|
|
|
2020-05-15 18:09:19 +08:00
|
|
|
String fileName = fileAttribute.getName();
|
2021-04-15 17:46:44 +08:00
|
|
|
String baseUrll = FILE_DIR + fileName;
|
|
|
|
|
// String suffix = fileAttribute.getSuffix();
|
2020-12-27 01:43:50 +08:00
|
|
|
ReturnResponse<String> response = DownloadUtils.downLoad(fileAttribute, fileName);
|
2020-12-27 14:06:06 +08:00
|
|
|
if (response.isFailure()) {
|
2020-12-27 14:47:28 +08:00
|
|
|
return otherFilePreview.notSupportedFile(model, fileAttribute, response.getMsg());
|
2018-01-17 17:51:53 +08:00
|
|
|
}
|
2019-10-25 14:37:31 +08:00
|
|
|
try {
|
2021-04-15 17:46:44 +08:00
|
|
|
String fileData = HtmlUtils.htmlEscape(textData(baseUrll));
|
2020-12-29 18:32:24 +08:00
|
|
|
model.addAttribute("textData", Base64.encodeBase64String(fileData.getBytes()));
|
2019-10-25 14:37:31 +08:00
|
|
|
} catch (IOException e) {
|
2020-12-27 14:47:28 +08:00
|
|
|
return otherFilePreview.notSupportedFile(model, fileAttribute, e.getLocalizedMessage());
|
2019-10-25 14:37:31 +08:00
|
|
|
}
|
2020-12-27 15:14:32 +08:00
|
|
|
return TXT_FILE_PREVIEW_PAGE;
|
2018-01-17 17:51:53 +08:00
|
|
|
}
|
|
|
|
|
|
2021-04-15 17:46:44 +08:00
|
|
|
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();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2018-01-17 17:51:53 +08:00
|
|
|
}
|