From 5450b99e377d67b9cf42cecda7c6873f51482ce6 Mon Sep 17 00:00:00 2001 From: Mlin Date: Thu, 5 Jan 2023 13:57:52 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BA=BA=E5=91=98=E7=AE=80=E5=8E=86=E5=AF=BC?= =?UTF-8?q?=E5=87=BA=E4=B8=8B=E8=BD=BD=EF=BC=8820230105=EF=BC=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../service/PersonnelResumeService.java | 6 +- .../impl/PersonnelResumeServiceImpl.java | 277 +++++++++++++++--- .../engine/organization/util/WordUtil.java | 84 ------ .../util/word/CustomXWPFDocument.java | 144 +++++++++ .../organization/util/word/WordUtil.java | 276 +++++++++++++++++ .../web/PersonnelResumeController.java | 26 +- .../wrapper/PersonnelResumeWrapper.java | 5 +- 7 files changed, 678 insertions(+), 140 deletions(-) delete mode 100644 src/com/engine/organization/util/WordUtil.java create mode 100644 src/com/engine/organization/util/word/CustomXWPFDocument.java create mode 100644 src/com/engine/organization/util/word/WordUtil.java diff --git a/src/com/engine/organization/service/PersonnelResumeService.java b/src/com/engine/organization/service/PersonnelResumeService.java index 7eebbcd8..deddbf74 100644 --- a/src/com/engine/organization/service/PersonnelResumeService.java +++ b/src/com/engine/organization/service/PersonnelResumeService.java @@ -2,6 +2,7 @@ package com.engine.organization.service; import com.engine.organization.entity.searchtree.SearchTreeParams; +import javax.servlet.http.HttpServletResponse; import java.util.Map; /** @@ -36,9 +37,10 @@ public interface PersonnelResumeService { /** * 人员简历下载 - * @param params + * @param type + * @param response * @return */ - Map downloadPerResume(SearchTreeParams params); + String downloadPerResume(Integer type, HttpServletResponse response) throws Exception; } diff --git a/src/com/engine/organization/service/impl/PersonnelResumeServiceImpl.java b/src/com/engine/organization/service/impl/PersonnelResumeServiceImpl.java index e6d81579..d62ca475 100644 --- a/src/com/engine/organization/service/impl/PersonnelResumeServiceImpl.java +++ b/src/com/engine/organization/service/impl/PersonnelResumeServiceImpl.java @@ -24,17 +24,28 @@ import com.engine.organization.mapper.resource.HrmResourceMapper; import com.engine.organization.service.PersonnelResumeService; import com.engine.organization.util.MenuBtn; import com.engine.organization.util.OrganizationAssert; -import com.engine.organization.util.WordUtil; import com.engine.organization.util.db.MapperProxyFactory; import com.engine.organization.util.detach.DetachUtil; import com.engine.organization.util.page.PageUtil; import com.engine.organization.util.tree.SearchTreeUtil; +import com.engine.organization.util.word.CustomXWPFDocument; +import com.engine.organization.util.word.WordUtil; import org.apache.commons.collections.CollectionUtils; +import org.apache.poi.xwpf.usermodel.XWPFParagraph; +import org.apache.xmlbeans.XmlOptions; +import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTBody; +import weaver.general.GCONST; import weaver.general.StringUtil; import weaver.general.Util; + +import javax.servlet.http.HttpServletResponse; +import java.io.*; +import java.text.SimpleDateFormat; import java.util.*; import java.util.stream.Collectors; +import java.util.zip.ZipEntry; +import java.util.zip.ZipOutputStream; /** * @author:dxfeng @@ -167,67 +178,237 @@ public class PersonnelResumeServiceImpl extends Service implements PersonnelResu } @Override - public Map downloadPerResume(SearchTreeParams params) { - // 调用word文档帮助类 - WordUtil wordUtil = new WordUtil(); - - // 模板文件存放的目录 - wordUtil.setBaseDir("E:/ww"); - - // 模板文件名称 - wordUtil.setTemplateFile("template.ftl"); - - // word生成的输出目录 - wordUtil.setOutputDir("E:/image/"); - - // 初始化数据map - Map dataMap = new HashMap<>(); - dataMap = getResumeList(23); - -// Object tables = dataMap.get("tables"); -// System.out.println(tables); - if (dataMap.containsKey("tables")){ - List tables = (List) dataMap.get("tables"); - - for (int t=0;t0){ - List> datas = personnelResumeTable.getDatas(); - if (datas.size()>0){ - List dataList = datas.get(0); - for (int i=0;i xwpfDocuments = new ArrayList<>(); + //模板地址 + String outPutPath = GCONST.getRootPath() + "hrm" + File.separator + "import" + File.separator + "template" + File.separator; + String filePath = outPutPath + "template0104.docx"; + //创建压缩包位置 + File fileZip = new File(outPutPath + "wordZip"); + if (!fileZip.exists()) { + fileZip.mkdirs(); + } + // 处理赋值的数据 + Map dataMap = new HashMap<>(); + Map resMap = getResumeList(22); + Map resMap2 = getResumeList(29); + + List> dataMapList = new ArrayList<>(); + dataMapList.add(resMap); + dataMapList.add(resMap2); + SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMdd"); + String filesPath = outPutPath + "wordZip" + File.separator + simpleDateFormat.format(new Date()); + String fileAllWordPath = outPutPath + "wordZip"; + String wordName = "全体人员简历合并导出"; + //处理所有数据 + for (int count = 0; count < dataMapList.size(); count++) { + //处理单人数据 + Map paramMap = new HashMap<>(); + for (Map.Entry entry : dataMapList.get(count).entrySet()) { + System.out.println("Key = " + entry.getKey() + ", Value = " + entry.getValue()); + paramMap.put("${" + entry.getKey() + "}", entry.getValue()); + } + if (dataMapList.get(count).containsKey("tables")) { + List tables = (List) dataMapList.get(count).get("tables"); + for (int t = 0; t < tables.size(); t++) { + PersonnelResumeTable personnelResumeTable = tables.get(t); + int length = personnelResumeTable.getColumns().size(); + if (personnelResumeTable.getDatas().size() > 0) { + List> datas = personnelResumeTable.getDatas(); + if (datas.size() > 0) { + List dataList = datas.get(0); + for (int i = 0; i < length; i++) { + paramMap.put("${col" + t + i + "}", Util.null2String(dataList.get(i).getValue())); + } } } } } + paramMap.put("${@image}", "1198"); + WordUtil wordUtil = new WordUtil(); + //返回一个新的xwpfDocument对象 + File file = new File(filePath); + CustomXWPFDocument doc = null; + InputStream is = new FileInputStream(file); + + doc = new CustomXWPFDocument(is); + wordUtil.replaceInPara(doc, paramMap); + wordUtil.replaceInTable(doc, paramMap); + xwpfDocuments.add(doc); + is.close(); + + File files = new File(filesPath); + if (!files.exists()) { + files.mkdirs(); + } else { + delFolder(files.getPath()); + files.mkdirs(); + } + String fileName = paramMap.get("${lastName}") + ".docx"; + System.out.println(fileName); + FileOutputStream os = null; + os = new FileOutputStream(filesPath + File.separator + new File(fileName)); + doc.write(os); } - // 录入表格数据(3条数据循环三次) - for (int i = 1; i <= 3; i++) { - dataMap.put("dataName"+i, "笔记本电脑"+i); - dataMap.put("dataBand"+i, "金山牌"+i); - dataMap.put("model"+i, "JHHJ6"+i); - dataMap.put("price"+i, 5000+i); - dataMap.put("quantity"+i, 3+i); - dataMap.put("total"+i, 15000+i); + if (xwpfDocuments.size() > 0) { + //这样第一步将所有word内容替换之后生成多个 xwpfDocument + //现在将多个xwpfDocument 进行合并 追加 生成word文件 + CustomXWPFDocument xwpfDocument = xwpfDocuments.get(0); + for (int i = 0; i < xwpfDocuments.size(); i++) { + //每次的追加为了避免样式和格式混乱 加上分页符 + //当是只有一条数据的时候 直接输出 + if (i == 0) { + xwpfDocument = xwpfDocuments.get(0); + xwpfDocument.write(new FileOutputStream(fileAllWordPath + File.separator + new File(wordName + ".docx"))); + continue; + } else { + //当存在多条时候 + xwpfDocument = mergeWord(xwpfDocument, xwpfDocuments.get(i)); + xwpfDocument.write(new FileOutputStream(fileAllWordPath + File.separator + new File(wordName + ".docx"))); + } + } + } + String resPath = fileAllWordPath + File.separator + new File(wordName + ".docx"); + if (type == 1) { + compressFileToZip(filesPath); + resPath = filesPath + ".zip"; } + return resPath; + } - //处理定价方式复选框 - dataMap.put("select", "☑境内采购" + "\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t" + "□境外采购"); + //两个对象进行追加 + public CustomXWPFDocument mergeWord(CustomXWPFDocument document, CustomXWPFDocument doucDocument2) throws Exception { + CustomXWPFDocument src1Document = document; + XWPFParagraph p = src1Document.createParagraph(); + //设置分页符 + p.setPageBreak(true); + CTBody src1Body = src1Document.getDocument().getBody(); + CustomXWPFDocument src2Document = doucDocument2; + CTBody src2Body = src2Document.getDocument().getBody(); + XWPFParagraph p2 = src2Document.createParagraph(); + XmlOptions optionsOuter = new XmlOptions(); + optionsOuter.setSaveOuter(); + String appendString = src2Body.xmlText(optionsOuter); + String srcString = src1Body.xmlText(); + String prefix = srcString.substring(0, srcString.indexOf(">") + 1); + String mainPart = srcString.substring(srcString.indexOf(">") + 1, srcString.lastIndexOf("<")); + String sufix = srcString.substring(srcString.lastIndexOf("<")); + String addPart = appendString.substring(appendString.indexOf(">") + 1, appendString.lastIndexOf("<")); + CTBody makeBody = CTBody.Factory.parse(prefix + mainPart + addPart + sufix); + src1Body.set(makeBody); + return src1Document; + } - String oldPath = wordUtil.createWord(dataMap); + public static boolean compressFileToZip(String compresspath) { + boolean bool = false; + try { + ZipOutputStream zipOutput = null; + File file = new File(compresspath); + if (file.isDirectory()) { + zipOutput = new ZipOutputStream(new BufferedOutputStream(new FileOutputStream(compresspath + ".zip"))); + compressZip(zipOutput, file, ""); + } else { + zipOutput = new ZipOutputStream(new BufferedOutputStream(new FileOutputStream(compresspath.substring(0, compresspath.lastIndexOf(".")) + ".zip"))); + } + zipOutput.closeEntry(); + zipOutput.close(); + bool = true; + } catch (Exception e) { + e.printStackTrace(); + } + return bool; + } - if (oldPath.equals("操作失败")){ - System.out.println("操作失败"); + private static void compressZip(ZipOutputStream zipOutput, File file, String suffixpath) { + File[] listFiles = file.listFiles();// 列出所有的文件 + for (File fi : listFiles) { + if (fi.isDirectory()) { + if (suffixpath.equals("")) { + compressZip(zipOutput, fi, fi.getName()); + } else { + compressZip(zipOutput, fi, suffixpath + File.separator + fi.getName()); + } + } else { + zip(zipOutput, fi, suffixpath); + } } + } - //输出生成后的文件路径 - System.out.println(oldPath); - return null; + public static void zip(ZipOutputStream zipOutput, File file, String suffixpath) { + try { + ZipEntry zEntry = null; + if (suffixpath.equals("")) { + zEntry = new ZipEntry(file.getName()); + } else { + zEntry = new ZipEntry(suffixpath + File.separator + file.getName()); + } + zipOutput.putNextEntry(zEntry); + BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file)); + byte[] buffer = new byte[1024]; + int read = 0; + while ((read = bis.read(buffer)) != -1) { + zipOutput.write(buffer, 0, read); + } + bis.close(); + } catch (Exception e) { + e.printStackTrace(); + } + } + + /*** + * 删除文件夹 + * + * @param folderPath 文件夹完整绝对路径 + */ + public static void delFolder(String folderPath) { + try { + delAllFile(folderPath); // 删除完里面所有内容 + String filePath = folderPath; + filePath = filePath.toString(); + java.io.File myFilePath = new java.io.File(filePath); + myFilePath.delete(); // 删除空文件夹 + } catch (Exception e) { + e.printStackTrace(); + } } + /*** + * 删除指定文件夹下所有文件 + * + * @param path 文件夹完整绝对路径 + * @return + */ + public static boolean delAllFile(String path) { + boolean flag = false; + File file = new File(path); + if (!file.exists()) { + return flag; + } + if (!file.isDirectory()) { + return flag; + } + String[] tempList = file.list(); + File temp = null; + for (int i = 0; i < tempList.length; i++) { + if (path.endsWith(File.separator)) { + temp = new File(path + tempList[i]); + } else { + temp = new File(path + File.separator + tempList[i]); + } + if (temp.isFile()) { + temp.delete(); + } + if (temp.isDirectory()) { + delAllFile(path + "/" + tempList[i]);// 先删除文件夹里面的文件 + delFolder(path + "/" + tempList[i]);// 再删除空文件夹 + flag = true; + } + } + return flag; + } public List getFilterDatas(String id, String type, String keyword) { List searchTree = new ArrayList<>(); // 通过分部、公司 组装数据 diff --git a/src/com/engine/organization/util/WordUtil.java b/src/com/engine/organization/util/WordUtil.java deleted file mode 100644 index 717ec6a2..00000000 --- a/src/com/engine/organization/util/WordUtil.java +++ /dev/null @@ -1,84 +0,0 @@ -package com.engine.organization.util; -import freemarker.template.Configuration; -import freemarker.template.Template; -import freemarker.template.TemplateException; -import lombok.Data; - -import java.io.*; -import java.util.Map; - -/** - * @author - * @date - * @apiNote 生成动态文档帮助类 - */ -@Data -public class WordUtil { - private Configuration configuration = null; - - /* - * 模板文件存放的目录 - */ - private String baseDir; - - /* - * 模板文件名称 - */ - private String templateFile; - - /* - * word生成的输出目录 - */ - private String outputDir; - - public WordUtil(){ - configuration = new Configuration(); - configuration.setDefaultEncoding("utf-8"); - - } - - /* - *

转换成word
- */ - public String createWord(Map dataMap){ - - configuration.setClassForTemplateLoading(this.getClass(), "");//模板文件所在路径 - - Template t = null; - - try { - //得到模板文件 - configuration.setDirectoryForTemplateLoading(new File(baseDir)); - } catch (IOException e) { - e.printStackTrace(); - } - try { - t = configuration.getTemplate(templateFile,"utf-8"); - } catch (IOException e) { - e.printStackTrace(); - } - - // GwUtil.getFileNo(""); 调用生成随机数的方法 - File outFile = new File(outputDir + "222.doc"); //导出文件 - - Writer out = null; - try { - try { - out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(outFile),"utf-8")); - } catch (UnsupportedEncodingException e) { - e.printStackTrace(); - } - } catch (FileNotFoundException e1) { - e1.printStackTrace(); - } - try { - t.process(dataMap, out); //将填充数据填入模板文件并输出到目标文件 - return outFile.getPath(); - } catch (TemplateException e) { - e.printStackTrace(); - } catch (IOException e) { - e.printStackTrace(); - } - return "文档生成失败"; - } -} diff --git a/src/com/engine/organization/util/word/CustomXWPFDocument.java b/src/com/engine/organization/util/word/CustomXWPFDocument.java new file mode 100644 index 00000000..9c889cba --- /dev/null +++ b/src/com/engine/organization/util/word/CustomXWPFDocument.java @@ -0,0 +1,144 @@ +package com.engine.organization.util.word; + +import org.apache.poi.openxml4j.opc.OPCPackage; +import org.apache.poi.xwpf.usermodel.XWPFDocument; +import org.apache.poi.xwpf.usermodel.XWPFParagraph; +import org.apache.xmlbeans.XmlException; +import org.apache.xmlbeans.XmlToken; +import org.openxmlformats.schemas.drawingml.x2006.main.CTNonVisualDrawingProps; +import org.openxmlformats.schemas.drawingml.x2006.main.CTPositiveSize2D; +import org.openxmlformats.schemas.drawingml.x2006.wordprocessingDrawing.CTInline; + +import java.io.IOException; +import java.io.InputStream; + +public class CustomXWPFDocument extends XWPFDocument { + + public CustomXWPFDocument(InputStream in) throws IOException { + super(in); + } + + public CustomXWPFDocument() { + super(); + } + + public CustomXWPFDocument(OPCPackage pkg) throws IOException { + super(pkg); + } + + /** + * @param id + * @param width 宽 + * @param height 高 + * @param paragraph 段落 + */ + public void createPicture(String blipId,int id, int width, int height, XWPFParagraph paragraph, CustomXWPFDocument doc) { + final int EMU = 9525; + width *= EMU; + height *= EMU; + CTInline inline = paragraph.createRun().getCTR().addNewDrawing().addNewInline(); + String picXml = "" + + "" + + " " + + " " + + " " + " " + + " " + + " " + + " " + + " " + + " " + + " " + + " " + + " " + + " " + + " " + + " " + + " " + + " " + + " " + + " " + + " " + + " " + + " " + + " " + ""; + + inline.addNewGraphic().addNewGraphicData(); + XmlToken xmlToken = null; + try { + xmlToken = XmlToken.Factory.parse(picXml); + } catch (XmlException xe) { + xe.printStackTrace(); + } + inline.set(xmlToken); + + CTPositiveSize2D extent = inline.addNewExtent(); + extent.setCx(width); + extent.setCy(height); + + CTNonVisualDrawingProps docPr = inline.addNewDocPr(); + docPr.setId(id); + docPr.setName("图片" + blipId); + docPr.setDescr("头像"); + } + + public void createPicture(String blipId, int id, int width, int height, XWPFParagraph paragraph) { + final int EMU = 9525; + width *= EMU; + height *= EMU; + //String blipId = getAllPictures().get(id).getPackageRelationship().getId(); + CTInline inline = paragraph.createRun().getCTR().addNewDrawing().addNewInline(); + + String picXml = "" + + "" + + " " + + " " + + " " + + " " + + " " + + " " + + " " + + " " + + " " + + " " + + " " + + " " + + " " + + " " + + " " + + " " + + " " + + " " + + " " + + " " + + " " + + " " + + " " + + ""; + + inline.addNewGraphic().addNewGraphicData(); + XmlToken xmlToken = null; + try { + xmlToken = XmlToken.Factory.parse(picXml); + } catch (XmlException xe) { + xe.printStackTrace(); + } + inline.set(xmlToken); + + CTPositiveSize2D extent = inline.addNewExtent(); + extent.setCx(width); + extent.setCy(height); + + CTNonVisualDrawingProps docPr = inline.addNewDocPr(); + docPr.setId(id); + docPr.setName("图片" + id); + docPr.setDescr("头像"); + } +} \ No newline at end of file diff --git a/src/com/engine/organization/util/word/WordUtil.java b/src/com/engine/organization/util/word/WordUtil.java new file mode 100644 index 00000000..485d24c1 --- /dev/null +++ b/src/com/engine/organization/util/word/WordUtil.java @@ -0,0 +1,276 @@ +package com.engine.organization.util.word; +import org.apache.poi.ooxml.POIXMLDocument; +import org.apache.poi.openxml4j.exceptions.InvalidFormatException; +import org.apache.poi.xwpf.usermodel.*; +import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTbl; +import org.springframework.beans.BeanUtils; +import weaver.file.ImageFileManager; +import weaver.general.Util; + +import javax.servlet.ServletOutputStream; +import javax.servlet.http.HttpServletResponse; +import java.io.*; +import java.net.HttpURLConnection; +import java.net.URL; +import java.util.Iterator; +import java.util.List; +import java.util.Map; +import java.util.Set; +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +public class WordUtil { + + /** + * 替换段落里面的变量 + * + * @param doc 要替换的文档 + * @param params 参数 + * @throws FileNotFoundException + * @throws InvalidFormatException + */ + public void replaceInPara(CustomXWPFDocument doc, Map params) throws InvalidFormatException, FileNotFoundException { + Iterator iterator = doc.getParagraphsIterator(); + XWPFParagraph para; + while (iterator.hasNext()) { + para = iterator.next(); + this.replaceInPara(para, params,doc); + } + } + + /** + * 替换段落里面的变量 + * + * @param para 要替换的段落 + * @param params 参数 + * @throws FileNotFoundException + * @throws InvalidFormatException + */ + public void replaceInPara(XWPFParagraph para, Map params, CustomXWPFDocument doc) throws InvalidFormatException, FileNotFoundException { + List runs; + Matcher matcher; + if (this.matcher(para.getParagraphText()).find()) { + runs = para.getRuns(); + + int start = -1; + int end = -1; + String str = ""; + String text= ""; + for (int i = 0; i < runs.size(); i++) { + text += runs.get(i).toString(); + } + for (int i = 0; i < runs.size(); i++) { + XWPFRun run = runs.get(i); + System.out.println("------>>>>>>>>>" + text); + if (text.contains("$")) { + start = text.indexOf("$"); + } + if ((start != -1)) { + str += text.substring(text.indexOf("$"), text.length()).trim(); + String paraList=runs.toString(); + System.out.println("未删除前"+paraList); + Object[] runArr = runs.toArray(); + int size=runs.size(); + int $Index=0; + for (int j = 0; j < runArr.length; j++) { + if (runArr[j].toString().contains("$")) { + $Index=j; + break; + } + } + int startIn=$Index; + while (startIn"+start); + System.out.println("end--->"+end); + System.out.println("str---->>>" + str); + + for (String key : params.keySet()) { + if (str.equals(key)) { + if(str.indexOf("@")==-1){ + String value= params.get(key).toString(); + para.createRun().setText(value); + break; + }else{ + String value= params.get(key).toString(); + int length = para.getRuns().size(); + if (length > 0) { + for (int i = (length - 1); i >= 0; i--) { + para.removeRun(i); + } + } + //网络图片取文件数据 + ImageFileManager manager = new ImageFileManager(); + manager.getImageFileInfoById(Util.getIntValue(value)); + InputStream inputStream = manager.getInputStream(); + + byte[] buff = new byte[8000]; + int bytesRead = 0; + ByteArrayOutputStream bao = new ByteArrayOutputStream(); + while(true) { + try { + if (!((bytesRead = inputStream.read(buff)) != -1)) break; + } catch (IOException e) { + e.printStackTrace(); + } + bao.write(buff, 0, bytesRead); + } + byte[] data = bao.toByteArray(); + ByteArrayInputStream byteInputStream = new ByteArrayInputStream(data); + + String blipId = doc.addPictureData(byteInputStream, CustomXWPFDocument.PICTURE_TYPE_PNG); + doc.createPicture(blipId,doc.getNextPicNameNumber(CustomXWPFDocument.PICTURE_TYPE_PNG), 120, 180,para); + break; + } + } + } + + + } + } + + /** + * 替换表格里面的变量 + * + * @param doc 要替换的文档 + * @param params 参数 + * @throws FileNotFoundException + * @throws InvalidFormatException + */ + public void replaceInTable(CustomXWPFDocument doc, Map params) throws InvalidFormatException, FileNotFoundException { + Iterator iterator = doc.getTablesIterator(); + XWPFTable table; + List rows; + List cells; + List paras; + while (iterator.hasNext()) { + table = iterator.next(); + rows = table.getRows(); + for (XWPFTableRow row : rows) { + cells = row.getTableCells(); + for (XWPFTableCell cell : cells) { + paras = cell.getParagraphs(); + for (XWPFParagraph para : paras) { + this.replaceInPara(para, params,doc); + } + } + } + } + } + + /** + * 正则匹配字符串 + * + * @param str + * @return + */ + private Matcher matcher(String str) { + Pattern pattern = Pattern.compile("\\$\\{(.+?)\\}", Pattern.CASE_INSENSITIVE); + Matcher matcher = pattern.matcher(str); + return matcher; + } + + /** + * 关闭输入流 + * + * @param is + */ + public void close(InputStream is) { + if (is != null) { + try { + is.close(); + } catch (IOException e) { + e.printStackTrace(); + } + } + } + + /** + * 关闭输出流 + * + * @param os + */ + public void close(OutputStream os) { + if (os != null) { + try { + os.close(); + } catch (IOException e) { + e.printStackTrace(); + } + } + } + + /** + * 功能描述:读取线上图片文件流 + * + * @param strUrl + * @return + * @see [相关类/方法](可选) + * @since [产品/模块版本](可选) + */ + public static byte[] getImageData(String strUrl) { + InputStream inStream = null; + try { + // new一个URL对象 + URL url = new URL(strUrl); + // 打开链接 + HttpURLConnection conn = (HttpURLConnection) url.openConnection(); + // 设置请求方式为"GET" + conn.setRequestMethod("GET"); + // 超时响应时间为5秒 + conn.setConnectTimeout(10 * 1000); + // 通过输入流获取图片数据 + inStream = conn.getInputStream(); + byte[] data = readInputStream(inStream); + return data; + } catch (Exception e) { + return null; + } finally { + if (inStream != null) { + try { + inStream.close(); + } catch (Exception e2) { + System.out.println("关闭流失败"); + } + } + } + + } + + /** + * 功能描述:读取文件流 + * + * @param inStream + * @return + * @throws Exception + * @see [相关类/方法](可选) + * @since [产品/模块版本](可选) + */ + public static byte[] readInputStream(InputStream inStream) throws Exception { + ByteArrayOutputStream outStream = new ByteArrayOutputStream(); + // 创建一个Buffer字符串 + byte[] buffer = new byte[1024]; + // 每次读取的字符串长度,如果为-1,代表全部读取完毕 + int len = 0; + // 使用一个输入流从buffer里把数据读取出来 + while ((len = inStream.read(buffer)) != -1) { + // 用输出流往buffer里写入数据,中间参数代表从哪个位置开始读,len代表读取的长度 + outStream.write(buffer, 0, len); + } + // 关闭输入流 + inStream.close(); + // 把outStream里的数据写入内存 + return outStream.toByteArray(); + } +} \ No newline at end of file diff --git a/src/com/engine/organization/web/PersonnelResumeController.java b/src/com/engine/organization/web/PersonnelResumeController.java index e10ff503..55618633 100644 --- a/src/com/engine/organization/web/PersonnelResumeController.java +++ b/src/com/engine/organization/web/PersonnelResumeController.java @@ -11,6 +11,7 @@ import weaver.general.Util; import weaver.hrm.HrmUserVarify; import weaver.hrm.User; +import javax.servlet.ServletOutputStream; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.ws.rs.GET; @@ -18,6 +19,11 @@ import javax.ws.rs.Path; import javax.ws.rs.Produces; import javax.ws.rs.core.Context; import javax.ws.rs.core.MediaType; +import javax.ws.rs.core.Response; +import javax.ws.rs.core.StreamingOutput; +import java.io.*; +import java.net.URLEncoder; +import java.time.LocalDate; import java.util.Map; /** @@ -84,11 +90,23 @@ public class PersonnelResumeController { */ @GET @Path("/downloadPerResume") - @Produces(MediaType.APPLICATION_JSON) - public Map downloadPerResume(@Context HttpServletRequest request, @Context HttpServletResponse response){ + @Produces(MediaType.APPLICATION_OCTET_STREAM) + public Response downloadPerResume(@Context HttpServletRequest request, @Context HttpServletResponse response) throws Exception { User user = HrmUserVarify.getUser(request, response); Map map = ParamUtil.request2Map(request); - SearchTreeParams params = JSONObject.toJavaObject((JSON) JSONObject.toJSON(map), SearchTreeParams.class); - return getPersonnelResumeWrapper(user).downloadPerResume(params); + // type: 0:合并 1:不合并 + Integer type = Integer.parseInt(Util.null2String(map.get("type"))); + + String realPath = getPersonnelResumeWrapper(user).downloadPerResume(type,response); + File file = new File(realPath); + // 如果文件不存在,提示404 + if (!file.exists()) { + return Response.status(Response.Status.NOT_FOUND).build(); + } + String fileName = URLEncoder.encode(file.getName(), "UTF-8"); + return Response + .ok(file) + .header("Content-disposition", "attachment;filename=" + fileName) + .header("Cache-Control", "no-cache").build(); } } diff --git a/src/com/engine/organization/wrapper/PersonnelResumeWrapper.java b/src/com/engine/organization/wrapper/PersonnelResumeWrapper.java index f4718af9..4ce1df04 100644 --- a/src/com/engine/organization/wrapper/PersonnelResumeWrapper.java +++ b/src/com/engine/organization/wrapper/PersonnelResumeWrapper.java @@ -8,6 +8,7 @@ import com.engine.organization.util.OrganizationWrapper; import tebie.applib.api.O; import weaver.hrm.User; +import javax.servlet.http.HttpServletResponse; import java.util.Map; /** @@ -32,7 +33,7 @@ public class PersonnelResumeWrapper extends OrganizationWrapper { return getPersonnelResumeService(user).getResumeList(uId); } - public Map downloadPerResume(SearchTreeParams params) { - return getPersonnelResumeService(user).downloadPerResume(params); + public String downloadPerResume(Integer type, HttpServletResponse response) throws Exception { + return getPersonnelResumeService(user).downloadPerResume(type, response); } }