/** * 将用户提交的简介写入html文件中, 并将文件的url设置进的htmlurl属性中 * * @param request * @param choicenesscontent * @throws IOException */ private void saveHtmlUrl(HttpServletRequest request, YztChoicenesscontent choicenesscontent) throws IOException { // 保存简介的html String introduction = choicenesscontent.getIntroduction(); String htmlStr = "<!DOCTYPE html><head><meta charset=\"UTF-8\"><title>内容简介</title></head><body>" + introduction + "</body></html>"; /* String htmlStr = choicenesscontent.getIntroduction(); */ // 返回简介html 要存在服务器上的绝对路径 String path = "upload/choicenesscontent/" + choicenesscontent.getId() + ".html"; String realRootPath = request.getServletContext().getRealPath("/") + path; File file = new File(realRootPath); BufferedWriter bw = new BufferedWriter(new FileWriter(file)); bw.write(htmlStr); // 拼成一个url路径 String htmlurl = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + request.getContextPath() + "/" + path; choicenesscontent.setIntroductionHtmlUrl(htmlurl); bw.close(); }
@RequestMapping("/edit/{id}") public String edit(@PathVariable String id, ModelMap m) { YztChoicenesscontent yc = choicenesscontentService.selectByKey(id); m.put("DicList", dictionaryService.listDictionaryByPid("3")); m.put("archive_id", id); m.put("jxnr", yc); m.put("introduction", YztWEB.HttpRetrieve(yc.getIntroductionHtmlUrl())); return "system/jxnr/jxnrinfo"; }
/** * 修改页面提交表单 action="system/choicenesscontent/save" * * @param yc * @param m * @return * @throws IOException */ @RequestMapping("/save") public String save(YztChoicenesscontent yc, ModelMap m, HttpServletRequest request) { Json json = new Json(); int flag = 0; // 修改之前先判断是否 String introduction = yc.getIntroduction(); if (CommonUtil.notEmpty(yc.getId())) { // 不为空的时候是修改 yc.setModfiytime(new Date()); // 写入流 try { saveHtmlUrl(request, yc); } catch (IOException e) { e.printStackTrace(); } flag = choicenesscontentService.updateNotNull(yc); } else { // 为空的时候添加 try { saveHtmlUrl(request, yc); } catch (IOException e) { e.printStackTrace(); } yc.setId(GUIDGener.getGUID()); yc.setCreatetime(new Date()); yc.setPicCount(0); yc.setViewCount(1); flag = choicenesscontentService.save(yc); } return "redirect:view/" + yc.getId(); }