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 @GetMapping
@ApiOperation("招聘导出") @ApiOperation("招聘导出")
public void export(String id, HttpServletRequest request, HttpServletResponse response) { public void export(String id, HttpServletResponse response) {
recruitmentService.export(id, request, response); recruitmentService.export(id, response);
} }
} }

View File

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