Browse Source

四平OA上传附件问题

tchao 2 năm trước cách đây
mục cha
commit
bddfe0046a
1 tập tin đã thay đổi với 345 bổ sung350 xóa
  1. 345 350
      src/com/hotent/platform/controller/system/SysFileController.java

+ 345 - 350
src/com/hotent/platform/controller/system/SysFileController.java

@@ -13,7 +13,6 @@ import java.text.SimpleDateFormat;
 import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.Calendar;
-import java.util.Collection;
 import java.util.Date;
 import java.util.HashMap;
 import java.util.Iterator;
@@ -26,7 +25,6 @@ import javax.annotation.Resource;
 import javax.servlet.http.HttpServletRequest;
 import javax.servlet.http.HttpServletResponse;
 
-import com.google.gson.JsonArray;
 import com.hotent.platform.controller.util.Word2PDF;
 import com.hotent.platform.model.TaskFile.TaskFile;
 import com.hotent.platform.service.taskFile.taskFileService;
@@ -34,7 +32,6 @@ import net.sf.json.JSONArray;
 import org.apache.commons.lang.StringUtils;
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
-import org.apache.ibatis.annotations.Lang;
 import org.springframework.dao.DuplicateKeyException;
 import org.springframework.stereotype.Controller;
 import org.springframework.web.bind.annotation.PathVariable;
@@ -45,10 +42,7 @@ import org.springframework.web.multipart.MultipartFile;
 import org.springframework.web.multipart.MultipartHttpServletRequest;
 import org.springframework.web.servlet.ModelAndView;
 
-import com.alibaba.fastjson.JSON;
 import com.alibaba.fastjson.JSONObject;
-import com.fr.process.engine.processservice.TaskService;
-import com.fr.third.com.lowagie.text.pdf.PdfReader;
 import com.hotent.core.api.util.ContextUtil;
 import com.hotent.core.log.SysAuditThreadLocalHolder;
 import com.hotent.core.util.AppUtil;
@@ -1495,366 +1489,367 @@ SysFileController extends BaseController {
 		}
 	}
 
-    /**
-     * 合并附件
-     *
-     * @param request
-     * @param response
-     * @throws IOException
-     */
-    @RequestMapping("mergePdfFiles")
-    @ResponseBody
-    public void mergePdfFiles(@RequestParam(value = "fileId[]") Long[] fileId, String datastr, String defId, String runId,
-					HttpServletRequest request, HttpServletResponse response) throws IOException {
-	  JSONObject jsStr = JSONObject.parseObject(datastr);
-	  Map<String, String> jsonMap = new HashMap<>();
-	  Set<String> keys = jsStr.keySet();
-	  for (String s : keys) {
-		jsonMap.put(s, jsStr.get(s).toString());
-	  }
-	  Properties prop = new Properties();
-	  InputStream in;
-	  in = this.getClass().getResourceAsStream("/conf/app.properties");
-	  prop.load(in);     ///加载属性列表
-	  List<Long> list = Arrays.asList(fileId);
-	  PrintWriter writer = response.getWriter();
-	  String attachPath = ServiceUtil.getBasePath().replace("/", File.separator);
-	  String filePaths = "";
-	  String realPath = "";
-	  String sbr = "";
-	  String savepath;
-	  SysFile sysFile = null;
-	  String filePath;
-	  for (Long id : list) {
-		//insert sys_task_file
-		TaskFile taskFile = new TaskFile();
-		taskFile.setID(UniqueIdUtil.genId());
-		taskFile.setRUNID(Long.parseLong(runId));
-		taskFile.setFILEID(id);
-		taskFile.setTYPE(taskFile.PDF_FILE);
-		taskFileService.add(taskFile);
-
-		sysFile = sysFileService.getById(id);
-		filePath = sysFile.getFilePath();
-		if (StringUtil.isEmpty(attachPath)) {
-		    filePath = AppUtil.getRealPath(filePath);
+	/**
+	 * 合并附件
+	 *
+	 * @param request
+	 * @param response
+	 * @throws IOException
+	 */
+	@RequestMapping("mergePdfFiles")
+	@ResponseBody
+	public void mergePdfFiles(@RequestParam(value = "fileId[]") Long[] fileId, String datastr, String defId, String runId,
+							  HttpServletRequest request, HttpServletResponse response) throws IOException {
+		JSONObject jsStr = JSONObject.parseObject(datastr);
+		Map<String, String> jsonMap = new HashMap<>();
+		Set<String> keys = jsStr.keySet();
+		for (String s : keys) {
+			jsonMap.put(s, jsStr.get(s).toString());
 		}
-		filePaths = StringUtil.trimSufffix(attachPath, File.separator);
-		realPath = filePaths+File.separator+sysFile.getFilePath().replace("/", File.separator);
-		realPath = realPath.replaceAll("\\\\", "/");
-		sbr += "file:///"+realPath;
-		sbr += ",";
-	  }
-	  if (sbr.endsWith(",")) {
-		sbr = sbr.substring(0, sbr.length()-1);
-	  }
-	  String[] filesArray = sbr.split(",");
-	  Long key = UniqueIdUtil.genId();
-	  Long formid = UniqueIdUtil.genId();
-	  String title = jsonMap.get("title");
-	  if (title.length() > 10) {
-		title = title.substring(0, 10);
-		title = title.replaceAll("<", "");
-		title = title.replaceAll(">", "");
-		title = title.replaceAll(" ", "");
-	  }
-	  savepath = realPath.substring(0, realPath.lastIndexOf("/"))+File.separator+
-		    title+formid+"."+sysFile.getExt();
-	  try {
-		boolean form = Html2PdfUtil.pdfout(jsonMap, filePaths+prop.get("template_path")+prop.get(defId), filePaths+File.separator+title+formid+".pdf");
-		if(form){
-		    List<String> array = new ArrayList<>();
-		    if (defId.equals("10000003480541")) {
-			  for (int i = 0; i < filesArray.length; i++) {
-				array.add(i, filesArray[i]);
-			  }
-		    } else {
-			  for (int i = -1; i < filesArray.length; i++) {
-				if (i == -1) {
-				    array.add("file:///"+filePaths+File.separator+title+formid+".pdf");
-				    if(insertContent(Long.parseLong(runId)) != ""){
-					  array.add("file:///"+insertContent(Long.parseLong(runId)));
-				    }
-				} else {
-				    array.add(filesArray[i]);
-				}
-			  }
-		    }
-		    Long newPdfId=UniqueIdUtil.genId();
-		    String newMergePdfPath=mergeFilePath(attachPath,newPdfId,"pdf");
-		    boolean result = MergePdfFilesUtil.mergePdfFiles(array.toArray(), attachPath+File.separator+newMergePdfPath);
-		    if (result) {
-			  sysFile.setFileId(key);
-			  sysFile.setFileName(title+formid);
-			  sysFile.setFilePath(newMergePdfPath);
-			  sysFileService.add(sysFile);
-			  writer.println("{\"success\":\"true\",\"id\":\""+key+"\",\"name\":\""+sysFile.getFileName()+"."+sysFile.getExt()+"\"}");
-		    }
+		Properties prop = new Properties();
+		InputStream in;
+		in = this.getClass().getResourceAsStream("/conf/app.properties");
+		prop.load(in);     ///加载属性列表
+		List<Long> list = Arrays.asList(fileId);
+		PrintWriter writer = response.getWriter();
+		String attachPath = ServiceUtil.getBasePath().replace("/", File.separator);
+		String filePaths = "";
+		String realPath = "";
+		String sbr = "";
+		String savepath;
+		SysFile sysFile = null;
+		String filePath;
+		for (Long id : list) {
+			//insert sys_task_file
+			TaskFile taskFile = new TaskFile();
+			taskFile.setID(UniqueIdUtil.genId());
+			taskFile.setRUNID(Long.parseLong(runId));
+			taskFile.setFILEID(id);
+			taskFile.setTYPE(taskFile.PDF_FILE);
+			taskFileService.add(taskFile);
+
+			sysFile = sysFileService.getById(id);
+			filePath = sysFile.getFilePath();
+			if (StringUtil.isEmpty(attachPath)) {
+				filePath = AppUtil.getRealPath(filePath);
+			}
+			filePaths = StringUtil.trimSufffix(attachPath, File.separator);
+			realPath = filePaths+File.separator+sysFile.getFilePath().replace("/", File.separator);
+			realPath = realPath.replaceAll("\\\\", "/");
+			sbr += "file:///"+realPath;
+			sbr += ",";
 		}
-	  } catch (Exception e) {
-		e.printStackTrace();
-		writer.println("{\"success\":\"false\"}");
-	  } finally {
-		writer.close();
-	  }
-    }
-
-    /**
-     * 创建PDF表单附件
-     *
-     * @param request
-     * @param response
-     * @throws IOException
-     */
-    @RequestMapping("createPdfFiles")
-    @ResponseBody
-    public void createPdfFiles(String defId, String datastr, String runId,
-					 HttpServletRequest request, HttpServletResponse response) throws IOException {
-	  JSONObject jsStr = JSONObject.parseObject(datastr);
-	  Map<String, String> jsonMap = new HashMap<>();
-	  Set<String> keys = jsStr.keySet();
-	  String filePaths;
-	  String attachPath = ServiceUtil.getBasePath().replace("/", File.separator);
-	  SysFile sysFile;
-	  String savepath;
-	  String realPath;
-	  PrintWriter writer = response.getWriter();
-	  for (String s : keys) {
-		jsonMap.put(s, jsStr.get(s).toString());
-	  }
-	  Properties prop = new Properties();
-	  InputStream in;
-	  in = this.getClass().getResourceAsStream("/conf/app.properties");
-	  prop.load(in);     ///加载属性列表
-	  try {
-		sysFile = sysFileService.getAll().get(0);
-		filePaths = StringUtil.trimSufffix(attachPath, File.separator);
+		if (sbr.endsWith(",")) {
+			sbr = sbr.substring(0, sbr.length()-1);
+		}
+		String[] filesArray = sbr.split(",");
+		Long key = UniqueIdUtil.genId();
 		Long formid = UniqueIdUtil.genId();
-		realPath = filePaths+File.separator+sysFile.getFilePath().replace("/", File.separator);
-		realPath = realPath.replaceAll("\\\\", "/");
 		String title = jsonMap.get("title");
 		if (title.length() > 10) {
-		    title = title.substring(0, 10);
-		    title = title.replaceAll("<", "");
-		    title = title.replaceAll(">", "");
-		    title = title.replaceAll(" ", "");
+			title = title.substring(0, 10);
+			title = title.replaceAll("<", "");
+			title = title.replaceAll(">", "");
+			title = title.replaceAll(" ", "");
 		}
 		savepath = realPath.substring(0, realPath.lastIndexOf("/"))+File.separator+
-			  title+formid+".pdf";
-		boolean isForm = Html2PdfUtil.pdfout(jsonMap, filePaths+prop.get("template_path")+prop.get(defId), savepath);
-		List<String> array = new ArrayList<>();
-		if (isForm) {
-		    array.add("file:///"+savepath);
-		    if(insertContent(Long.parseLong(runId)) != ""){
-		        array.add("file:///"+insertContent(Long.parseLong(runId)));
-		    }
-		    Long newPdfId=UniqueIdUtil.genId();
-		    String newMergePdfPath=mergeFilePath(attachPath,newPdfId,"pdf");
-		    boolean result = MergePdfFilesUtil.mergePdfFiles(array.toArray(), attachPath+File.separator+newMergePdfPath);
-		    if (result) {
-			  sysFile.setFileId(formid);
-			  sysFile.setFileName(title+formid);
-			  sysFile.setExt("pdf");
-			  sysFile.setFilePath(newMergePdfPath);
-			  sysFileService.add(sysFile);
-			  writer.println("{\"success\":\"true\",\"id\":\""+formid+"\",\"name\":\""+sysFile.getFileName()+".pdf\"}");
-		    }
+				title+formid+"."+sysFile.getExt();
+		try {
+			boolean form = Html2PdfUtil.pdfout(jsonMap, filePaths+prop.get("template_path")+prop.get(defId), filePaths+File.separator+title+formid+".pdf");
+			if(form){
+				List<String> array = new ArrayList<>();
+				if (defId.equals("10000003480541")) {
+					for (int i = 0; i < filesArray.length; i++) {
+						array.add(i, filesArray[i]);
+					}
+				} else {
+					for (int i = -1; i < filesArray.length; i++) {
+						if (i == -1) {
+							array.add("file:///"+filePaths+File.separator+title+formid+".pdf");
+							if(insertContent(Long.parseLong(runId)) != ""){
+								array.add("file:///"+insertContent(Long.parseLong(runId)));
+							}
+						} else {
+							array.add(filesArray[i]);
+						}
+					}
+				}
+				Long newPdfId=UniqueIdUtil.genId();
+				String newMergePdfPath=mergeFilePath(attachPath,newPdfId,"pdf");
+				boolean result = MergePdfFilesUtil.mergePdfFiles(array.toArray(), attachPath+File.separator+newMergePdfPath);
+				if (result) {
+					sysFile.setFileId(key);
+					sysFile.setFileName(title+formid);
+					sysFile.setFilePath(newMergePdfPath);
+					sysFileService.add(sysFile);
+					writer.println("{\"success\":\"true\",\"id\":\""+key+"\",\"name\":\""+sysFile.getFileName()+"."+sysFile.getExt()+"\"}");
+				}
+			}
+		} catch (Exception e) {
+			e.printStackTrace();
+			writer.println("{\"success\":\"false\"}");
+		} finally {
+			writer.close();
 		}
-	  } catch (Exception e) {
-		e.printStackTrace();
-		writer.println("{\"success\":\"false\"}");
-	  } finally {
-		writer.close();
-	  }
-    }
-
-    private String mergeFilePath(String attachPath,Long newPdfId,String FileExt){
-	  Calendar cal = Calendar.getInstance();
-	  Integer year = cal.get(Calendar.YEAR);
-	  Integer month = cal.get(Calendar.MONTH) + 1;
-	  String newMergePdfPath=year+File.separator+month+File.separator+newPdfId+"."+FileExt;
-	  System.out.println("合并后的文件为:"+attachPath+File.separator+newMergePdfPath);
-	  return newMergePdfPath;
-    }
-
-    private String insertContent(Long runId) {
-	  List<SysFile> contentFiles = sysFileService.getContentFileByRunId(runId);
-	  if (contentFiles != null && contentFiles.size() > 0) {//说明产生了正文,需要转为PDF
-		SysFile contentFile = contentFiles.get(0);
-		String attachPathContent = ServiceUtil.getBasePath().replace("/", File.separator);
-		String ContentRealPath;
-		ContentRealPath = attachPathContent+File.separator+contentFile.getFilePath();
-		ContentRealPath = ContentRealPath.replaceAll("\\\\", "/");
-		//将附件转化为PDF格式
-		String pdfPath = ContentRealPath.replace(contentFile.getExt(), "pdf");
-		int time = Word2PDF.convert2PDF(ContentRealPath, pdfPath);
-		if (Word2PDF.readConvert2PDFResult(time)) {
-		    return pdfPath;
+	}
+
+	/**
+	 * 创建PDF表单附件
+	 *
+	 * @param request
+	 * @param response
+	 * @throws IOException
+	 */
+	@RequestMapping("createPdfFiles")
+	@ResponseBody
+	public void createPdfFiles(String defId, String datastr, String runId,
+							   HttpServletRequest request, HttpServletResponse response) throws IOException {
+		JSONObject jsStr = JSONObject.parseObject(datastr);
+		Map<String, String> jsonMap = new HashMap<>();
+		Set<String> keys = jsStr.keySet();
+		String filePaths;
+		String attachPath = ServiceUtil.getBasePath().replace("/", File.separator);
+		SysFile sysFile;
+		String savepath;
+		String realPath;
+		PrintWriter writer = response.getWriter();
+		for (String s : keys) {
+			jsonMap.put(s, jsStr.get(s).toString());
 		}
-		return "";
-	  }
-	  return "";
-    }
-
-    /**
-     * 合并附件
-     *
-     * @param request
-     * @param response
-     * @throws IOException
-     */
-    @RequestMapping("mergeTaskPdfFiles")
-    @ResponseBody
-    public void mergeTaskPdfFiles(@RequestParam(value = "fileId[]") Long[] fileId, String runId,
-					    HttpServletRequest request, HttpServletResponse response) throws Exception {
-	  PrintWriter writer = response.getWriter();
-	  List<Long> list = Arrays.asList(fileId);
-	  //D:\apache-tomcat-7.0.64\webapps\fileupload
-	  String attachPath = ServiceUtil.getBasePath().replace("/", File.separator);
-	  List<String> array = new ArrayList<>();
-	  String realPath;
-	  Long formId = list.get(0);
-	  SysFile sysFile = sysFileService.getById(formId);
-	  String filePath = StringUtil.trimSufffix(attachPath, File.separator);
-	  realPath = filePath+File.separator+sysFile.getFilePath().replace("/", File.separator);
-	  String formPath = realPath.substring(0, realPath.lastIndexOf(File.separator));
-	  //拆出表单页
-	  String formName = UniqueIdUtil.genId()+".pdf";
-	  for (int i = 0; i < list.size(); i++) {
-		if (i == 0) {
-		    //form
-		    boolean boo = MergePdfFilesUtil.partitionPdfFile(realPath, formName, 1, 1);
-		    if (boo) {
-			  //拆出表单页,并添加至ARRAY中
-			  array.add("file:///"+formPath+File.separator+formName);
-			  //保存到SYSFILE
-			  Long partitionPdfFormId = UniqueIdUtil.genId();
-			  sysFile.setFileId(partitionPdfFormId);
-			  sysFile.setCreatetime(new Date());
-			  sysFile.setFilePath(sysFile.getFilePath().substring(0, sysFile.getFilePath().lastIndexOf("\\"))+File.separator+formName);
-			  sysFileService.add(sysFile);
-			  //保存到TASKFILE
-			  TaskFile taskFile = new TaskFile();
-			  taskFile.setID(UniqueIdUtil.genId());
-			  taskFile.setRUNID(Long.parseLong(runId));
-			  taskFile.setFILEID(partitionPdfFormId);
-			  taskFile.setTYPE(taskFile.PDF_FORM);
-			  taskFileService.add(taskFile);
-
-			  List<SysFile> contentFiles = sysFileService.getContentFileByRunId(Long.parseLong(runId));
-			  //说明产生了正文,需要转为PDF
-			  if (contentFiles != null && contentFiles.size() > 0) {
-				SysFile contentFile = contentFiles.get(0);
-				String attachPath_ = ServiceUtil.getBasePath().replace("/", File.separator);
-				String realPath_;
-				realPath_ = attachPath_+File.separator+contentFile.getFilePath();
-				realPath_ = realPath_.replaceAll("\\\\", "/");
-				//正文转换的PDF
-				String pdfPath = realPath_.replace(contentFile.getExt(), "pdf");
-				//将正文转化为PDF格式
-				int time = Word2PDF.convert2PDF(realPath_, pdfPath);
-				if (Word2PDF.readConvert2PDFResult(time)) {
-				    //正文添加至ARRAY中
-				    array.add("file:///"+pdfPath);
+		Properties prop = new Properties();
+		InputStream in;
+		in = this.getClass().getResourceAsStream("/conf/app.properties");
+		prop.load(in);     ///加载属性列表
+		try {
+			List<SysFile> a =sysFileService.getAll();
+			sysFile = a.get(0) ;
+			filePaths = StringUtil.trimSufffix(attachPath, File.separator);
+			Long formid = UniqueIdUtil.genId();
+			realPath = filePaths+File.separator+sysFile.getFilePath().replace("/", File.separator);
+			realPath = realPath.replaceAll("\\\\", "/");
+			String title = jsonMap.get("title");
+			if (title.length() > 10) {
+				title = title.substring(0, 10);
+				title = title.replaceAll("<", "");
+				title = title.replaceAll(">", "");
+				title = title.replaceAll(" ", "");
+			}
+			savepath = realPath.substring(0, realPath.lastIndexOf("/"))+File.separator+
+					title+formid+".pdf";
+			boolean isForm = Html2PdfUtil.pdfout(jsonMap, filePaths+prop.get("template_path")+prop.get(defId), savepath);
+			List<String> array = new ArrayList<>();
+			if (isForm) {
+				array.add("file:///"+savepath);
+				if(insertContent(Long.parseLong(runId)) != ""){
+					array.add("file:///"+insertContent(Long.parseLong(runId)));
 				}
-			  }
-
-			  //添加历史附件
-			  List<Map<String, Object>> TaskFileList = taskFileService.getByRunId(Long.parseLong(runId));
-			  for (int j = 0; j < TaskFileList.size(); j++) {
-				array.add("file:///"+attachPath+File.separator
-					  +TaskFileList.get(j).get("FILEPATH").toString().replace("/", File.separator));
-			  }
-		    }
-		} else {
-		    String path = StringUtil.trimSufffix(attachPath, File.separator)
-				+File.separator+sysFileService.getById(list.get(i)).getFilePath().replace("/", File.separator);
-		    array.add("file:///"+path);
-		    TaskFile taskFile = new TaskFile();
-		    taskFile.setID(UniqueIdUtil.genId());
-		    taskFile.setRUNID(Long.parseLong(runId));
-		    taskFile.setFILEID(list.get(i));
-		    taskFile.setTYPE(taskFile.PDF_FILE);
-		    taskFileService.add(taskFile);
+				Long newPdfId=UniqueIdUtil.genId();
+				String newMergePdfPath=mergeFilePath(attachPath,newPdfId,"pdf");
+				boolean result = MergePdfFilesUtil.mergePdfFiles(array.toArray(), attachPath+File.separator+newMergePdfPath);
+				if (result) {
+					sysFile.setFileId(formid);
+					sysFile.setFileName(title+formid);
+					sysFile.setExt("pdf");
+					sysFile.setFilePath(newMergePdfPath);
+					sysFileService.add(sysFile);
+					writer.println("{\"success\":\"true\",\"id\":\""+formid+"\",\"name\":\""+sysFile.getFileName()+".pdf\"}");
+				}
+			}
+		} catch (Exception e) {
+			e.printStackTrace();
+			writer.println("{\"success\":\"false\"}");
+		} finally {
+			writer.close();
 		}
-	  }
-	  //合并
-	  String mergePdfPath = realPath.substring(0, realPath.lastIndexOf(File.separator));
-	  String mergePdfName = UniqueIdUtil.genId()+".pdf";
-	  boolean result = MergePdfFilesUtil.mergePdfFiles(array.toArray(), mergePdfPath+File.separator+mergePdfName);
-	  if (result) {
-		Long mergePdfFileId = UniqueIdUtil.genId();
-		sysFile.setFileId(mergePdfFileId);
-		sysFile.setCreatetime(new Date());
-		sysFile.setFilePath(sysFile.getFilePath().substring(0, sysFile.getFilePath().lastIndexOf("\\"))+File.separator+mergePdfName);
-		sysFileService.add(sysFile);
-		writer.println("{\"success\":\"true\",\"id\":\""+sysFile.getFileId()+"\",\"name\":\""+sysFile.getFileName()+"."+sysFile.getExt()+"\"}");
-	  } else {
-		writer.println("{\"success\":\"false\"}");
-	  }
-	  writer.close();
-    }
-
-    /**
-     * 手动合并文件
-     *
-     * @param request
-     * @param response
-     * @throws IOException
-     */
-    @RequestMapping("mergeTaskFiles")
-    @ResponseBody
-    public void mergeTaskFiles(HttpServletRequest request , HttpServletResponse response) throws IOException {
-	  PrintWriter writer = response.getWriter();
-	  String fileIdAry = RequestUtil.getString(request,"fileIdAry");
-	  JSONArray jsonArray = JSONArray.fromObject(fileIdAry);
-	  String attachPath = ServiceUtil.getBasePath().replace("/", File.separator);
-	  String filePath = StringUtil.trimSufffix(attachPath, File.separator);
-	  String realPath;
-	  List<String> array = new ArrayList<>();
-	  for(int i=0;i<jsonArray.size();i++){
-		net.sf.json.JSONObject jsonObject = net.sf.json.JSONObject.fromObject(jsonArray.get(i));
-		SysFile sysFile = sysFileService.getById(Long.parseLong(jsonObject.get("fileId").toString()));
-
-		if(jsonObject.get("fileType").toString().equals("1")){//正文需转PDF
-		    realPath = filePath+File.separator+sysFile.getFilePath().replace("/", File.separator);
-		    //将正文转化为PDF格式
-		    String pdfPath = realPath.replace(sysFile.getExt(), "pdf");
-		    int time = Word2PDF.convert2PDF(realPath, pdfPath);
-		    if (Word2PDF.readConvert2PDFResult(time)) {
-			  //附件转PDF转换成功
-			  System.out.println("转化成功,用时:  "+time+"s...");
-			  array.add("file:///"+pdfPath);
-		    }
-		}else{
-		    realPath = filePath+File.separator+sysFile.getFilePath().replace("/", File.separator);
-		    array.add("file:///"+realPath);
+	}
+
+	private String mergeFilePath(String attachPath,Long newPdfId,String FileExt){
+		Calendar cal = Calendar.getInstance();
+		Integer year = cal.get(Calendar.YEAR);
+		Integer month = cal.get(Calendar.MONTH) + 1;
+		String newMergePdfPath=year+File.separator+month+File.separator+newPdfId+"."+FileExt;
+		System.out.println("合并后的文件为:"+attachPath+File.separator+newMergePdfPath);
+		return newMergePdfPath;
+	}
+
+	private String insertContent(Long runId) {
+		List<SysFile> contentFiles = sysFileService.getContentFileByRunId(runId);
+		if (contentFiles != null && contentFiles.size() > 0) {//说明产生了正文,需要转为PDF
+			SysFile contentFile = contentFiles.get(0);
+			String attachPathContent = ServiceUtil.getBasePath().replace("/", File.separator);
+			String ContentRealPath;
+			ContentRealPath = attachPathContent+File.separator+contentFile.getFilePath();
+			ContentRealPath = ContentRealPath.replaceAll("\\\\", "/");
+			//将附件转化为PDF格式
+			String pdfPath = ContentRealPath.replace(contentFile.getExt(), "pdf");
+			int time = Word2PDF.convert2PDF(ContentRealPath, pdfPath);
+			if (Word2PDF.readConvert2PDFResult(time)) {
+				return pdfPath;
+			}
+			return "";
 		}
-	  }
+		return "";
+	}
+
+	/**
+	 * 合并附件
+	 *
+	 * @param request
+	 * @param response
+	 * @throws IOException
+	 */
+	@RequestMapping("mergeTaskPdfFiles")
+	@ResponseBody
+	public void mergeTaskPdfFiles(@RequestParam(value = "fileId[]") Long[] fileId, String runId,
+								  HttpServletRequest request, HttpServletResponse response) throws Exception {
+		PrintWriter writer = response.getWriter();
+		List<Long> list = Arrays.asList(fileId);
+		//D:\apache-tomcat-7.0.64\webapps\fileupload
+		String attachPath = ServiceUtil.getBasePath().replace("/", File.separator);
+		List<String> array = new ArrayList<>();
+		String realPath;
+		Long formId = list.get(0);
+		SysFile sysFile = sysFileService.getById(formId);
+		String filePath = StringUtil.trimSufffix(attachPath, File.separator);
+		realPath = filePath+File.separator+sysFile.getFilePath().replace("/", File.separator);
+		String formPath = realPath.substring(0, realPath.lastIndexOf(File.separator));
+		//拆出表单页
+		String formName = UniqueIdUtil.genId()+".pdf";
+		for (int i = 0; i < list.size(); i++) {
+			if (i == 0) {
+				//form
+				boolean boo = MergePdfFilesUtil.partitionPdfFile(realPath, formName, 1, 1);
+				if (boo) {
+					//拆出表单页,并添加至ARRAY中
+					array.add("file:///"+formPath+File.separator+formName);
+					//保存到SYSFILE
+					Long partitionPdfFormId = UniqueIdUtil.genId();
+					sysFile.setFileId(partitionPdfFormId);
+					sysFile.setCreatetime(new Date());
+					sysFile.setFilePath(sysFile.getFilePath().substring(0, sysFile.getFilePath().lastIndexOf("\\"))+File.separator+formName);
+					sysFileService.add(sysFile);
+					//保存到TASKFILE
+					TaskFile taskFile = new TaskFile();
+					taskFile.setID(UniqueIdUtil.genId());
+					taskFile.setRUNID(Long.parseLong(runId));
+					taskFile.setFILEID(partitionPdfFormId);
+					taskFile.setTYPE(taskFile.PDF_FORM);
+					taskFileService.add(taskFile);
+
+					List<SysFile> contentFiles = sysFileService.getContentFileByRunId(Long.parseLong(runId));
+					//说明产生了正文,需要转为PDF
+					if (contentFiles != null && contentFiles.size() > 0) {
+						SysFile contentFile = contentFiles.get(0);
+						String attachPath_ = ServiceUtil.getBasePath().replace("/", File.separator);
+						String realPath_;
+						realPath_ = attachPath_+File.separator+contentFile.getFilePath();
+						realPath_ = realPath_.replaceAll("\\\\", "/");
+						//正文转换的PDF
+						String pdfPath = realPath_.replace(contentFile.getExt(), "pdf");
+						//将正文转化为PDF格式
+						int time = Word2PDF.convert2PDF(realPath_, pdfPath);
+						if (Word2PDF.readConvert2PDFResult(time)) {
+							//正文添加至ARRAY中
+							array.add("file:///"+pdfPath);
+						}
+					}
 
-	try{
-		Long newPdfId=UniqueIdUtil.genId();
-		String FileExt = "pdf";
-		String newMergePdfPath=mergeFilePath(attachPath,newPdfId,FileExt);
-		boolean result = MergePdfFilesUtil.mergePdfFiles(array.toArray(), attachPath+File.separator+newMergePdfPath);
+					//添加历史附件
+					List<Map<String, Object>> TaskFileList = taskFileService.getByRunId(Long.parseLong(runId));
+					for (int j = 0; j < TaskFileList.size(); j++) {
+						array.add("file:///"+attachPath+File.separator
+								+TaskFileList.get(j).get("FILEPATH").toString().replace("/", File.separator));
+					}
+				}
+			} else {
+				String path = StringUtil.trimSufffix(attachPath, File.separator)
+						+File.separator+sysFileService.getById(list.get(i)).getFilePath().replace("/", File.separator);
+				array.add("file:///"+path);
+				TaskFile taskFile = new TaskFile();
+				taskFile.setID(UniqueIdUtil.genId());
+				taskFile.setRUNID(Long.parseLong(runId));
+				taskFile.setFILEID(list.get(i));
+				taskFile.setTYPE(taskFile.PDF_FILE);
+				taskFileService.add(taskFile);
+			}
+		}
+		//合并
+		String mergePdfPath = realPath.substring(0, realPath.lastIndexOf(File.separator));
+		String mergePdfName = UniqueIdUtil.genId()+".pdf";
+		boolean result = MergePdfFilesUtil.mergePdfFiles(array.toArray(), mergePdfPath+File.separator+mergePdfName);
 		if (result) {
-		    net.sf.json.JSONObject jsonObject = net.sf.json.JSONObject.fromObject(jsonArray.get(0));
-		    SysFile sysFile = sysFileService.getById(Long.parseLong(jsonObject.get("fileId").toString()));
-		    sysFile.setFileId(newPdfId);
-		    sysFile.setFileName(sysFile.getFileName());
-		    sysFile.setFilePath(newMergePdfPath);
-		    sysFile.setExt(FileExt);
-		    sysFile.setCreatetime(new Date());
-		    sysFileService.add(sysFile);
-		    writer.println("{\"success\":\"true\",\"id\":\""+newPdfId+"\",\"name\":\""+sysFile.getFileName()+"."+FileExt+"\"}");
+			Long mergePdfFileId = UniqueIdUtil.genId();
+			sysFile.setFileId(mergePdfFileId);
+			sysFile.setCreatetime(new Date());
+			sysFile.setFilePath(sysFile.getFilePath().substring(0, sysFile.getFilePath().lastIndexOf("\\"))+File.separator+mergePdfName);
+			sysFileService.add(sysFile);
+			writer.println("{\"success\":\"true\",\"id\":\""+sysFile.getFileId()+"\",\"name\":\""+sysFile.getFileName()+"."+sysFile.getExt()+"\"}");
+		} else {
+			writer.println("{\"success\":\"false\"}");
 		}
-	  } catch (Exception e) {
-		e.printStackTrace();
-		writer.println("{\"success\":\"false\"}");
-	  } finally {
 		writer.close();
-	  }
-    }
+	}
+
+	/**
+	 * 手动合并文件
+	 *
+	 * @param request
+	 * @param response
+	 * @throws IOException
+	 */
+	@RequestMapping("mergeTaskFiles")
+	@ResponseBody
+	public void mergeTaskFiles(HttpServletRequest request , HttpServletResponse response) throws IOException {
+		PrintWriter writer = response.getWriter();
+		String fileIdAry = RequestUtil.getString(request,"fileIdAry");
+		JSONArray jsonArray = JSONArray.fromObject(fileIdAry);
+		String attachPath = ServiceUtil.getBasePath().replace("/", File.separator);
+		String filePath = StringUtil.trimSufffix(attachPath, File.separator);
+		String realPath;
+		List<String> array = new ArrayList<>();
+		for(int i=0;i<jsonArray.size();i++){
+			net.sf.json.JSONObject jsonObject = net.sf.json.JSONObject.fromObject(jsonArray.get(i));
+			SysFile sysFile = sysFileService.getById(Long.parseLong(jsonObject.get("fileId").toString()));
+
+			if(jsonObject.get("fileType").toString().equals("1")){//正文需转PDF
+				realPath = filePath+File.separator+sysFile.getFilePath().replace("/", File.separator);
+				//将正文转化为PDF格式
+				String pdfPath = realPath.replace(sysFile.getExt(), "pdf");
+				int time = Word2PDF.convert2PDF(realPath, pdfPath);
+				if (Word2PDF.readConvert2PDFResult(time)) {
+					//附件转PDF转换成功
+					System.out.println("转化成功,用时:  "+time+"s...");
+					array.add("file:///"+pdfPath);
+				}
+			}else{
+				realPath = filePath+File.separator+sysFile.getFilePath().replace("/", File.separator);
+				array.add("file:///"+realPath);
+			}
+		}
+
+		try{
+			Long newPdfId=UniqueIdUtil.genId();
+			String FileExt = "pdf";
+			String newMergePdfPath=mergeFilePath(attachPath,newPdfId,FileExt);
+			boolean result = MergePdfFilesUtil.mergePdfFiles(array.toArray(), attachPath+File.separator+newMergePdfPath);
+			if (result) {
+				net.sf.json.JSONObject jsonObject = net.sf.json.JSONObject.fromObject(jsonArray.get(0));
+				SysFile sysFile = sysFileService.getById(Long.parseLong(jsonObject.get("fileId").toString()));
+				sysFile.setFileId(newPdfId);
+				sysFile.setFileName(sysFile.getFileName());
+				sysFile.setFilePath(newMergePdfPath);
+				sysFile.setExt(FileExt);
+				sysFile.setCreatetime(new Date());
+				sysFileService.add(sysFile);
+				writer.println("{\"success\":\"true\",\"id\":\""+newPdfId+"\",\"name\":\""+sysFile.getFileName()+"."+FileExt+"\"}");
+			}
+		} catch (Exception e) {
+			e.printStackTrace();
+			writer.println("{\"success\":\"false\"}");
+		} finally {
+			writer.close();
+		}
+	}
 
 
 }