2018-01-17 17:51:53 +08:00
|
|
|
package cn.keking.service.impl;
|
|
|
|
|
|
|
|
|
|
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-25 20:41:34 +08:00
|
|
|
import org.apache.commons.io.FileUtils;
|
2018-01-17 17:51:53 +08:00
|
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
import org.springframework.ui.Model;
|
2020-12-25 20:41:34 +08:00
|
|
|
import org.springframework.util.Base64Utils;
|
2018-01-17 17:51:53 +08:00
|
|
|
|
2019-10-25 14:37:31 +08:00
|
|
|
import java.io.File;
|
|
|
|
|
import java.io.IOException;
|
2020-12-25 20:41:34 +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-26 19:13:50 +08:00
|
|
|
public static final String TEXT_TYPE = "textType";
|
|
|
|
|
public static final String DEFAULT_TEXT_TYPE = "simText";
|
2018-01-17 17:51:53 +08:00
|
|
|
|
2020-12-26 19:13:50 +08:00
|
|
|
private final DownloadUtils downloadUtils;
|
2020-12-25 22:47:30 +08:00
|
|
|
|
2020-05-15 18:09:19 +08:00
|
|
|
public SimTextFilePreviewImpl(DownloadUtils downloadUtils) {
|
|
|
|
|
this.downloadUtils = downloadUtils;
|
|
|
|
|
}
|
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) {
|
2020-05-15 18:09:19 +08:00
|
|
|
String fileName = fileAttribute.getName();
|
|
|
|
|
ReturnResponse<String> response = downloadUtils.downLoad(fileAttribute, fileName);
|
2018-01-17 17:51:53 +08:00
|
|
|
if (0 != response.getCode()) {
|
|
|
|
|
model.addAttribute("msg", response.getMsg());
|
2020-12-25 21:02:51 +08:00
|
|
|
model.addAttribute("fileType", fileAttribute.getSuffix());
|
2018-01-17 17:51:53 +08:00
|
|
|
return "fileNotSupported";
|
|
|
|
|
}
|
2019-10-25 14:37:31 +08:00
|
|
|
try {
|
|
|
|
|
File originFile = new File(response.getContent());
|
2020-12-25 21:02:51 +08:00
|
|
|
String xmlString = FileUtils.readFileToString(originFile, StandardCharsets.UTF_8);
|
|
|
|
|
model.addAttribute("textData", Base64Utils.encodeToString(xmlString.getBytes()));
|
2019-10-25 14:37:31 +08:00
|
|
|
} catch (IOException e) {
|
|
|
|
|
model.addAttribute("msg", e.getLocalizedMessage());
|
2020-12-25 21:02:51 +08:00
|
|
|
model.addAttribute("fileType", fileAttribute.getSuffix());
|
2019-10-25 14:37:31 +08:00
|
|
|
return "fileNotSupported";
|
|
|
|
|
}
|
2020-12-25 22:47:30 +08:00
|
|
|
if (!model.containsAttribute(TEXT_TYPE)) {
|
|
|
|
|
model.addAttribute(TEXT_TYPE, DEFAULT_TEXT_TYPE);
|
|
|
|
|
}
|
2018-01-17 17:51:53 +08:00
|
|
|
return "txt";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|