<dependency>
<groupId>cn.afterturn</groupId>
<artifactId>easypoi-base</artifactId>
<version>3.2.0</version>
</dependency>
<dependency>
<groupId>cn.afterturn</groupId>
<artifactId>easypoi-web</artifactId>
<version>3.2.0</version>
</dependency>
<dependency>
<groupId>cn.afterturn</groupId>
<artifactId>easypoi-annotation</artifactId>
<version>3.2.0</version>
</dependency>
public void exportWord(String projectId, HttpServletResponse response) throws Exception{
//获取项目根目录
File path = new File(ResourceUtils.getURL("classpath:").getPath());
//导出前的检查
beforeExportWord(path);
//业务代码
//根据项目id获取项目信息
ProjectEntity entity = projectRepository.getOne(projectId);
response.setCharacterEncoding("UTF-8");
response.setContentType("application/x-download");
//把项目名称当作zip包的名字
String fileName = entity.getProjectName()+".zip";
fileName = URLEncoder.encode(fileName, "UTF-8");
response.addHeader("Content-Disposition", "attachment;filename=" + fileName);
//获取该项目下评测机构的list
List<PgzjEntity> pgzjList = pgzjRepository.findByProjectIdOrderByPgzjIdAsc(projectId);
List<PgzjCompanyEntity> companList = new ArrayList<>();
if(pgzjList.size()>0){
for(PgzjEntity pgzjItem: pgzjList){
PgjgInfoUserEntity pgjgInfoUser = pgjgInfoUserRepository.findByPgjgLoginIdAndProjectId(pgzjItem.getLoginId(), pgzjItem.getProjectId()).get(0);
PgjgInfoEntity pgjgInfo = pgjgInfoRepository.getOne(pgjgInfoUser.getPgjgInfoId());
//获取每个机构下评测的产品
companList = pgzjCompanyRepository.findByPgzjId(pgzjItem.getPgzjId());
if(companList.size()>0){
for(PgzjCompanyEntity companItem:companList){
//异步执行生成word方法
asyncService.caretWord(companItem,pgjgInfo,path,pgzjItem);
//生成的word文档放到word文件夹下
}
}
}
}
//业务代码结束
//超两分钟跳出循环,执行打,防止无限循环
Long startId = System.currentTimeMillis();
while (System.currentTimeMillis()-startId<120*1000){
int a = 0;
File word = new File(path.getAbsolutePath(), "/word");
System.out.println("开始:");
File[] arr = word.listFiles();
if(arr.length==(pgzjList.size()*companList.size())){
break;
}
System.out.println("等待:"+arr.length);
}
exportZip(path,response);
}
public void beforeExportWord(File path) throws Exception {
//获取临时存放生成word文件夹
File word = new File(path.getAbsolutePath(), "/word");
//判断文件夹是否存在,存在清空文件夹,不存在创建文件夹
if (word.exists()) {
for (File f : word.listFiles()) {
f.delete();
}
} else {
word.mkdir();
}
//获取临时存放生成zip文件夹
File zip = new File(path.getAbsolutePath(), "/zip");
//判断文件夹是否存在,存在清空文件夹,不存在创建文件夹
if (zip.exists()) {
for (File f : zip.listFiles()) {
f.delete();
}
} else {
zip.mkdir();
}
}
public void exportZip(File path,HttpServletResponse response) throws Exception{
FileOutputStream fos1 = new FileOutputStream(new File(path.getAbsolutePath()+"/zip/模板.zip"));
ZipUtils.toZip(path.getAbsolutePath()+"/word", fos1, true);
OutputStream fos = null;
BufferedInputStream bis = null;
BufferedOutputStream bos = null;
try {
fos = response.getOutputStream();
bis = new BufferedInputStream(new FileInputStream(path.getAbsolutePath()+"/zip/模板.zip"));
bos = new BufferedOutputStream(fos);
byte[] buff = new byte[2048];
int bytesRead;
while (-1 != (bytesRead = bis.read(buff, 0, buff.length))) {
bos.write(buff, 0, bytesRead);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
fos.flush();
bis.close();
bos.close();
fos.close();
}
}
}