This commit is contained in:
zhuyy 2023-07-20 17:31:09 +08:00
parent 6cf13d7c03
commit 5e4d683e88
3 changed files with 36 additions and 21 deletions

View File

@ -68,7 +68,7 @@ public class RecruitmentController extends BaseController {
@GetMapping
@ApiOperation("招聘导出")
public void export(String id, HttpServletRequest request, HttpServletResponse response) {
recruitmentService.export(id, request, response);
public void export(String id, HttpServletResponse response) {
recruitmentService.export(id, response);
}
}

View File

@ -30,6 +30,6 @@ public interface IRecruitmentService {
*/
AjaxResult selectRecruitmentById(String id);
void export(String id, HttpServletRequest request, HttpServletResponse response);
void export(String id, HttpServletResponse response);
}

View File

@ -16,6 +16,7 @@ import com.ydool.common.cache.ConfigCache;
import com.ydool.common.constant.ArgsConst;
import com.ydool.common.data.dto.AjaxResult;
import com.ydool.common.exception.ResultException;
import com.ydool.common.utils.UploadUtils;
import com.ydool.staff.dto.RecruitmentDto;
import com.ydool.staff.entity.*;
import com.ydool.common.base.BaseService;
@ -35,11 +36,10 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;
import org.springframework.util.ResourceUtils;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.util.*;
import java.util.concurrent.atomic.AtomicInteger;
@ -56,8 +56,6 @@ import java.util.concurrent.atomic.AtomicInteger;
public class RecruitmentServiceImpl extends BaseService<RecruitmentMapper, Recruitment> implements IRecruitmentService {
private static final AtomicInteger counter = new AtomicInteger(0);
private final String filepath = System.getProperty("user.dir");
@Autowired
RecruitmentMapper recruitmentMapper;
@Autowired
@ -139,23 +137,28 @@ public class RecruitmentServiceImpl extends BaseService<RecruitmentMapper, Recru
}
@Override
public void export(String id, HttpServletRequest request, HttpServletResponse response) {
public void export(String id, HttpServletResponse response) {
if (StrUtil.isBlank(id)) {
throw new ResultException("id不能为空");
}
String wordDir = "";
if (!filepath.endsWith("/")) {
wordDir = "/upload/word";
}
// String wordDir = "/upload/word";
File tf = null;
try {
// 获取模板路径
tf = new File(URLDecoder.decode(Objects.requireNonNull(
Thread.currentThread().getContextClassLoader().getResource("doc/象山县国有企业人员招聘报备表.docx")).getPath(), "UTF-8"));
} catch (UnsupportedEncodingException e) {
// 本地获取模板路径
// tf = new File(URLDecoder.decode(Objects.requireNonNull(
// Thread.currentThread().getContextClassLoader().getResource("doc/app.docx")).getPath(), "UTF-8"));
tf = new File(ResourceUtils.getURL("classpath:doc/app.docx").getPath());
System.out.println("tf.getPath() = " + tf.getPath());
// } catch (UnsupportedEncodingException e) {
// throw new RuntimeException(e);
} catch (FileNotFoundException e) {
throw new RuntimeException(e);
}
System.out.println("tf.getPath() = " + tf.getPath());
String wordDir = UploadUtils.getWebRoot();
File to = new File(wordDir);
if (!to.exists()) {
to.mkdirs();
}
Organization organization = organizationMapper.selectById(id);
CompanyName companyName = companyNameMapper.selectById(organization.getCompanyId());
List<Recruitment> recruitments = recruitmentMapper.selectList(Wrappers.lambdaQuery(Recruitment.class).eq(Recruitment::getCompanyId, organization.getId()));
@ -169,12 +172,15 @@ public class RecruitmentServiceImpl extends BaseService<RecruitmentMapper, Recru
params.put("list", recruitments);
String fileName = "象山县国有企业人员招聘报备表.docx";
// 导出word
String word = exportWord(tf.getPath(), wordDir, fileName, params);
String pdfFileName = "象山县国有企业人员招聘报备表.pdf";
System.out.println("word = " + word);
// 转pdf
Document document = new Document();
document.loadFromFile(word);
document.saveToFile(wordDir + "/" + pdfFileName, FileFormat.PDF);
// fileNamepdf路径
document.saveToFile(wordDir + pdfFileName, FileFormat.PDF);
try {
response.setContentType("application/force-download");// 设置强制下载不打开
@ -187,7 +193,7 @@ public class RecruitmentServiceImpl extends BaseService<RecruitmentMapper, Recru
BufferedInputStream bis = null;
OutputStream outputStream = null;
try {
fis = new FileInputStream(wordDir + File.separator + pdfFileName);
fis = new FileInputStream(wordDir + pdfFileName);
bis = new BufferedInputStream(fis);
outputStream = response.getOutputStream();
int i = bis.read(buffer);
@ -213,8 +219,18 @@ public class RecruitmentServiceImpl extends BaseService<RecruitmentMapper, Recru
e.printStackTrace();
}
}
// 下载
// UploadUtils.download(response, wordDir, pdfFileName);
}
/**
* @param templatePath 模板路径
* @param saveDir 临时文件路径
* @param fileName 导出文件名
* @param params 数据
* @return 路径
*/
public static String exportWord(String templatePath, String saveDir, String fileName, Map<String, Object> params) {
org.springframework.util.Assert.notNull(templatePath, "模板路径不能为空");
org.springframework.util.Assert.notNull(saveDir, "临时文件路径不能为空");
@ -223,7 +239,6 @@ public class RecruitmentServiceImpl extends BaseService<RecruitmentMapper, Recru
if (!saveDir.endsWith("/")) {
saveDir = saveDir + File.separator;
}
File dir = new File(saveDir);
if (!dir.exists()) {
dir.mkdirs();