fix export bug

This commit is contained in:
copper 2021-05-23 16:55:59 +08:00
parent cebef1294d
commit 5e22ef0804
7 changed files with 47 additions and 16 deletions

View File

@ -106,7 +106,7 @@ public class RecordController {
@GetMapping("/report") @GetMapping("/report")
public void report(@RequestParam("uuid") String uuid, HttpServletResponse response) { public void report(@RequestParam("uuid") String uuid, HttpServletResponse response) {
// recordService.record2word(uuid, response); // recordService.record2word(uuid, response);
recordService.report(uuid, response);
} }
@PutMapping("/metaData") @PutMapping("/metaData")

View File

@ -23,7 +23,7 @@ public enum AuthEnum {
add("/project/project : DELETE"); add("/project/project : DELETE");
add("/project/record : DELETE"); add("/project/record : DELETE");
add("/project/record : PUT"); add("/project/record : PUT");
add("/record/report : GET");
add("/respondent/respondent : POST"); add("/respondent/respondent : POST");
add("/respondent/respondent : PUT"); add("/respondent/respondent : PUT");
@ -72,6 +72,7 @@ public enum AuthEnum {
add("/project/projectList : GET"); add("/project/projectList : GET");
add("/project/respondentCount : GET"); add("/project/respondentCount : GET");
add("/record/record2word : GET"); add("/record/record2word : GET");
add("/record/report : GET");
}}), }}),
MODIFY_PROJECT("修改项目的权限", new HashSet<String>() {{ MODIFY_PROJECT("修改项目的权限", new HashSet<String>() {{

View File

@ -10,6 +10,7 @@ import com.example.survey.enumeration.RecordStateEnum;
import com.example.survey.enumeration.ResultEnum; import com.example.survey.enumeration.ResultEnum;
import com.example.survey.exception.*; import com.example.survey.exception.*;
import com.example.survey.service.RecordService; import com.example.survey.service.RecordService;
import com.example.survey.util.DownloadUtil;
import com.example.survey.util.ThreadWordUtil; import com.example.survey.util.ThreadWordUtil;
import com.example.survey.util.WordUtil; import com.example.survey.util.WordUtil;
import com.example.survey.vo.RecordDetailVO; import com.example.survey.vo.RecordDetailVO;
@ -54,6 +55,9 @@ public class RecordServiceImpl implements RecordService {
@Value("${file.url}") @Value("${file.url}")
private String url; private String url;
@Value("${file.report}")
private String report;
@Autowired @Autowired
private ThreadWordUtil threadWordUtil; private ThreadWordUtil threadWordUtil;
@ -309,8 +313,27 @@ public class RecordServiceImpl implements RecordService {
@Override @Override
public void report(String uuid, HttpServletResponse response) { public void report(String uuid, HttpServletResponse response) {
// TODO Auto-generated method stub Record record = recordDao.getRecord(uuid);
if (record == null) {
throw new RecordException(ResultEnum.NOT_EXIST_RECORD);
}
if(!record.getMetaDataType().equals(MetaDataTypeEnum.REPORT_TEMP.getValue())) {
throw new RecordException(ResultEnum.NOT_EXIST_RECORD);
}
if(!record.getState().equals(RecordStateEnum.REVIEWED.getValue())){
throw new RecordException(ResultEnum.NOT_EXIST_RECORD);
}
String path = this.report + record.getUuid() + ".docx";
try {
DownloadUtil.downloadFile(path, response);
} catch (IOException e) {
response.setStatus(400);
// response.clo
}
} }
} }

View File

@ -25,14 +25,22 @@ public class ThreadWordUtil implements Runnable {
@Value("${file.path}") @Value("${file.path}")
private String path; private String path;
@Value("${file.report}")
private String report;
@Value("${file.template}")
private String template;
@Autowired @Autowired
private RecordDao recordDao; private RecordDao recordDao;
@Override @Override
public void run() { public void run() {
if(record != null) { if(record != null) {
String templatePath = this.path + record.getMetaData().getName() + ".docx"; System.out.println("=========Start Export============");
String outputPath = this.path + record.getUuid() + ".docx"; String templatePath = this.template + record.getMetaData().getName() + ".docx";
String outputPath = this.report + record.getUuid() + ".docx";
try{ try{
Map<String, Object> values = record.getValues(); Map<String, Object> values = record.getValues();
values.put("submit", record.getOperationList().get(0).getUser().getUsername()); values.put("submit", record.getOperationList().get(0).getUser().getUsername());
@ -51,10 +59,8 @@ public class ThreadWordUtil implements Runnable {
public void start() { public void start() {
if (t == null) { t = new Thread(this, threadName);
t = new Thread(this, threadName); t.start();
t.start();
}
} }
} }

View File

@ -35,14 +35,13 @@ public class WordUtil {
private HashMap<String, Method> key2methods; private HashMap<String, Method> key2methods;
private Map<String, Object> values; private Map<String, Object> values;
private String outputPath; private String outputPath;
private File temp;
public WordUtil(Map<String, Object> values, String templatePath, String outputPath) public WordUtil(Map<String, Object> values, String templatePath, String outputPath)
throws FileNotFoundException, IOException, NoSuchMethodException, SecurityException { throws FileNotFoundException, IOException, NoSuchMethodException, SecurityException {
File temp = new File(UUID.randomUUID() + "t.docx"); temp = new File(UUID.randomUUID() + "t.docx");
this.outputPath = outputPath; this.outputPath = outputPath;
FileUtils.copyFile(new File(templatePath), temp); FileUtils.copyFile(new File(templatePath), temp);
// response.addHeader("Content-Disposition", "attachment;filename=" +
// UUID.randomUUID().toString() + ".docx");
OPCPackage pack = POIXMLDocument.openPackage(temp.getAbsolutePath()); OPCPackage pack = POIXMLDocument.openPackage(temp.getAbsolutePath());
document = new CustomXWPFDocument(pack); document = new CustomXWPFDocument(pack);
this.values = values; this.values = values;
@ -105,7 +104,7 @@ public class WordUtil {
document.write(outputStream); document.write(outputStream);
outputStream.close(); outputStream.close();
document.close(); document.close();
FileUtils.forceDelete(temp);
// XWPFTemplate template = XWPFTemplate.compile(templatePath, // XWPFTemplate template = XWPFTemplate.compile(templatePath,
// configureBuilder.build()).render(values); // configureBuilder.build()).render(values);
// try (BufferedOutputStream os = new // try (BufferedOutputStream os = new

View File

@ -18,3 +18,5 @@ spring:
file: file:
path: ./upload/ path: ./upload/
url: /upload/ url: /upload/
report: ./report/
template: ./template/

View File

@ -1,7 +1,6 @@
spring: spring:
data: data:
mongodb: mongodb:
# uri: mongodb://cveo:cveo123456@120.78.177.67:27017/survey
#创建索引 #创建索引
auto-index-creation: true auto-index-creation: true
host: mongo host: mongo
@ -19,7 +18,8 @@ spring:
file: file:
path: ./data/upload/ path: ./data/upload/
url: /upload/ url: /upload/
report: ./data/report/
template: ./data/template/
# kafka: # kafka:
# bootstrap-servers: localhost:9092 # 指定kafka 代理地址,可以多个 # bootstrap-servers: localhost:9092 # 指定kafka 代理地址,可以多个
# producer: # 生产者 # producer: # 生产者