*/
@RequestMapping(params = "saveWordFiles", method = RequestMethod.POST)
@ResponseBody
public AjaxJson saveWordFiles(HttpServletRequest request,
HttpServletResponse response, CgformFtlEntity cgformFtl) {
AjaxJson j = new AjaxJson();
Map<String, Object> attributes = new HashMap<String, Object>();
LogUtil.info("-------------------------step.1-------------------------------------");
String fileKey = oConvertUtils.getString(request.getParameter("id"));// 文件ID
String cgformId = oConvertUtils.getString(request
.getParameter("cgformId"));// formid
String cgformName = oConvertUtils.getString(request
.getParameter("cgformName"));// formname
String ftlStatus = oConvertUtils.getString(request
.getParameter("ftlStatus"));// formStatus
if (oConvertUtils.isEmpty(ftlStatus)) {
ftlStatus = "0";
}
if (StringUtil.isNotEmpty(fileKey)) {
cgformFtl.setId(fileKey);
cgformFtl = systemService.getEntity(CgformFtlEntity.class, fileKey);
} else {
cgformFtl.setFtlVersion(cgformFtlService.getNextVarsion(cgformId));
}
LogUtil.info("-------------------------step.2-------------------------------------");
cgformFtl.setCgformId(cgformId);
cgformFtl.setCgformName(cgformName);
cgformFtl.setFtlStatus(ftlStatus);
UploadFile uploadFile = new UploadFile(request, cgformFtl);
uploadFile.setCusPath("forms");
message = null;
try {
uploadFile.getMultipartRequest().setCharacterEncoding("UTF-8");
MultipartHttpServletRequest multipartRequest = uploadFile
.getMultipartRequest();
String uploadbasepath = uploadFile.getBasePath();// 文件上传根目录
if (uploadbasepath == null) {
uploadbasepath = ResourceUtil.getConfigByName("uploadpath");
}
Map<String, MultipartFile> fileMap = multipartRequest.getFileMap();
// 文件数据库保存路径
String path = uploadbasepath + "\\";// 文件保存在硬盘的相对路径
String realPath = uploadFile.getMultipartRequest().getSession()
.getServletContext().getRealPath("\\")
+ path;// 文件的硬盘真实路径
File file = new File(realPath);
if (!file.exists()) {
file.mkdir();// 创建根目录
}
if (uploadFile.getCusPath() != null) {
realPath += uploadFile.getCusPath() + "\\";
path += uploadFile.getCusPath() + "\\";
file = new File(realPath);
if (!file.exists()) {
file.mkdir();// 创建文件自定义子目录
}
} else {
realPath += DataUtils.getDataString(DataUtils.yyyyMMdd) + "\\";
path += DataUtils.getDataString(DataUtils.yyyyMMdd) + "\\";
file = new File(realPath);
if (!file.exists()) {
file.mkdir();// 创建文件时间子目录
}
}
LogUtil.info("-------------------------step.3-------------------------------------");
String fileName = "";
for (Map.Entry<String, MultipartFile> entity : fileMap.entrySet()) {
MultipartFile mf = entity.getValue();// 获取上传文件对象
fileName = mf.getOriginalFilename();// 获取文件名
String extend = FileUtils.getExtend(fileName);// 获取文件扩展名
String myfilename = "";
String myhtmlfilename = "";
String noextfilename = "";// 不带扩展名
if (uploadFile.isRename()) {
noextfilename = DataUtils
.getDataString(DataUtils.yyyymmddhhmmss)
+ StringUtil.random(8);// 自定义文件名称
myfilename = noextfilename + "." + extend;// 自定义文件名称
} else {
myfilename = fileName;
}
String savePath = realPath + myfilename;// 文件保存全路径
cgformFtl.setFtlWordUrl(fileName);
File savefile = new File(savePath);
FileCopyUtils.copy(mf.getBytes(), savefile);
myhtmlfilename = realPath + noextfilename + ".html";
String myftlfilename = realPath + noextfilename + ".ftl";
LogUtil.info("-------------------------step.4-------------------------------------");
// 开始转换表单文件
OfficeHtmlUtil officeHtml = new OfficeHtmlUtil();
// 方式一:jacob.jar方式word转html
officeHtml.wordToHtml(savePath, myhtmlfilename);
String htmlStr = officeHtml.getInfo(myhtmlfilename);
htmlStr = officeHtml.doHtml(htmlStr);
// 方式二:poi方式word转html
// officeHtml.WordConverterHtml(savePath, myhtmlfilename);
// String htmlStr = officeHtml.getInfo(myhtmlfilename);
// htmlStr = officeHtml.doPoiHtml(htmlStr);
officeHtml.stringToFile(htmlStr, myftlfilename);
// js plugin start
StringBuilder script = new StringBuilder("");
script.append("<script type=\"text/javascript\">");
script.append("${js_plug_in?if_exists}");
script.append("</script>");
htmlStr = htmlStr.replace("</html>", script.toString()
+ "</html>");
// js plugin end
cgformFtl.setFtlContent(htmlStr);
cgformFtlService.saveOrUpdate(cgformFtl);
LogUtil.info("-------------------------step.5-------------------------------------");
}
} catch (Exception e1) {
LogUtil.error(e1.toString());
message = e1.toString();
}
attributes.put("id", cgformFtl.getId());
if (StringUtil.isNotEmpty(message))
j.setMsg("Word转Ftl失败," + message);
else
j.setMsg("Word转Ftl成功");
j.setAttributes(attributes);
return j;
}