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;
|
|
|
|
|
import cn.keking.utils.FileUtils;
|
|
|
|
|
import cn.keking.utils.SimTextUtil;
|
|
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
import org.springframework.ui.Model;
|
|
|
|
|
|
2019-10-25 14:37:31 +08:00
|
|
|
import java.io.File;
|
|
|
|
|
import java.io.IOException;
|
|
|
|
|
import java.nio.file.Files;
|
|
|
|
|
|
2018-01-17 17:51:53 +08:00
|
|
|
/**
|
|
|
|
|
* Created by kl on 2018/1/17.
|
|
|
|
|
* Content :处理文本文件
|
|
|
|
|
*/
|
|
|
|
|
@Service
|
|
|
|
|
public class SimTextFilePreviewImpl implements FilePreview{
|
|
|
|
|
|
|
|
|
|
@Autowired
|
|
|
|
|
SimTextUtil simTextUtil;
|
|
|
|
|
|
|
|
|
|
@Autowired
|
|
|
|
|
FileUtils fileUtils;
|
|
|
|
|
|
|
|
|
|
@Override
|
2019-06-17 14:21:16 +08:00
|
|
|
public String filePreviewHandle(String url, Model model, FileAttribute fileAttribute){
|
2018-01-17 17:51:53 +08:00
|
|
|
String decodedUrl=fileAttribute.getDecodedUrl();
|
|
|
|
|
String fileName=fileAttribute.getName();
|
|
|
|
|
ReturnResponse<String> response = simTextUtil.readSimText(decodedUrl, fileName);
|
|
|
|
|
if (0 != response.getCode()) {
|
|
|
|
|
model.addAttribute("msg", response.getMsg());
|
2018-03-25 13:26: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());
|
|
|
|
|
File previewFile = new File(response.getContent() + ".txt");
|
2019-11-05 13:38:27 +08:00
|
|
|
if (previewFile.exists()) {
|
|
|
|
|
previewFile.delete();
|
|
|
|
|
}
|
2019-10-25 14:37:31 +08:00
|
|
|
Files.copy(originFile.toPath(), previewFile.toPath());
|
|
|
|
|
} catch (IOException e) {
|
|
|
|
|
model.addAttribute("msg", e.getLocalizedMessage());
|
|
|
|
|
model.addAttribute("fileType",fileAttribute.getSuffix());
|
|
|
|
|
return "fileNotSupported";
|
|
|
|
|
}
|
|
|
|
|
model.addAttribute("ordinaryUrl", response.getMsg() + ".txt");
|
2018-01-17 17:51:53 +08:00
|
|
|
return "txt";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|