13.5.7 下载中心
处理下载中心的DocumentController
代码如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178
| package org.fkit.hrm.controller;
import java.io.File; import java.util.List; import javax.servlet.http.HttpSession; import org.apache.commons.io.FileUtils; import org.fkit.hrm.domain.Document; import org.fkit.hrm.domain.User; import org.fkit.hrm.service.HrmService; import org.fkit.hrm.util.common.HrmConstants; import org.fkit.hrm.util.tag.PageModel; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.http.HttpHeaders; import org.springframework.http.HttpStatus; import org.springframework.http.MediaType; import org.springframework.http.ResponseEntity; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.ModelAttribute; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.servlet.ModelAndView;
@Controller public class DocumentController {
@Autowired @Qualifier("hrmService") private HrmService hrmService;
@RequestMapping(value = "/document/selectDocument") public String selectDocument(Model model, Integer pageIndex, @ModelAttribute Document document) { PageModel pageModel = new PageModel(); if (pageIndex != null) { pageModel.setPageIndex(pageIndex); } List<Document> documents = hrmService.findDocument(document, pageModel); model.addAttribute("documents", documents); model.addAttribute("pageModel", pageModel); return "document/document";
}
@RequestMapping(value = "/document/addDocument") public ModelAndView addDocument(String flag, @ModelAttribute Document document, ModelAndView mv, HttpSession session) throws Exception { if (flag.equals("1")) { mv.setViewName("document/showAddDocument"); } else { String path = session.getServletContext().getRealPath("/upload/"); System.out.println(path); String fileName = document.getFile().getOriginalFilename(); document.getFile() .transferTo(new File(path + File.separator + fileName));
document.setFileName(fileName); User user = (User) session.getAttribute(HrmConstants.USER_SESSION); document.setUser(user); hrmService.addDocument(document); mv.setViewName("redirect:/document/selectDocument"); } return mv; }
@RequestMapping(value = "/document/removeDocument") public ModelAndView removeDocument(String ids, ModelAndView mv) { String[] idArray = ids.split(","); for (String id : idArray) { hrmService.removeDocumentById(Integer.parseInt(id)); } mv.setViewName("redirect:/document/selectDocument"); return mv; }
@RequestMapping(value = "/document/updateDocument") public ModelAndView updateDocument(String flag, @ModelAttribute Document document, ModelAndView mv) { if (flag.equals("1")) { Document target = hrmService.findDocumentById(document.getId()); mv.addObject("document", target); mv.setViewName("document/showUpdateDocument"); } else { hrmService.modifyDocument(document); mv.setViewName("redirect:/document/selectDocument"); } return mv; }
@RequestMapping(value = "/document/downLoad") public ResponseEntity<byte[]> downLoad(Integer id, HttpSession session) throws Exception { Document target = hrmService.findDocumentById(id); String fileName = target.getFileName(); String path = session.getServletContext().getRealPath("/upload/"); File file = new File(path + File.separator + fileName); HttpHeaders headers = new HttpHeaders(); String downloadFielName = new String(fileName.getBytes("UTF-8"), "iso-8859-1"); headers.setContentDispositionFormData("attachment", downloadFielName); headers.setContentType(MediaType.APPLICATION_OCTET_STREAM); return new ResponseEntity<byte[]>(FileUtils.readFileToByteArray(file), headers, HttpStatus.CREATED); } }
|
单击左侧菜单下载中心下面的上传文档命令,跳转到上传文档界面。
输入需要上传的文档标题和文档概述
,选择要上传的文档,单击上传按钮,若上传成功则跳转到文档查询界面,显示所有文档信息。
选择每一行最后的下载按钮,会弹出下载页面,选择下载文档保存的路径,即可以下载文档。
输入文档标题,单击搜索按钮可以完成模糊查询功能
选择每一行的操作按钮,可以进入修改页面,对选中的文档进行修改操作
。
选择每一行第一列的复选框,单击删除按钮,则可以对选中的文档进行删除操作
。