后台主页面
填写周任务说明
网站周任务首页
周任务上传与下载
代码如下:
@RequestMapping("/addTask") public String addTask(Task task, @RequestParam("file")MultipartFile file, HttpSession session) throws IOException { //获取上传文件用户的id User user= (User) session.getAttribute("admin"); //处理周任务 Date date =new Date(); task.setReleaseDate(date); String userName=userService.findById(user.getId()).getUserName(); task.setUserName(userName); taskService.insertTask(task); //获取文件原始名称 String oldName=file.getOriginalFilename(); //获取文件后缀 String extension="."+FilenameUtils.getExtension(file.getOriginalFilename()); //生成新文件名 String newName=new SimpleDateFormat("yyyyMMddHHmmss").format(new Date())+ UUID.randomUUID().toString().replace("-","")+extension; //根据日期生成目录 String realPath= ResourceUtils.getURL("classpath:").getPath()+"/static/task"; String dateFormat =new SimpleDateFormat("yyyy-MM-dd").format(new Date()); String dateDirPath=realPath+"/"+dateFormat; File dateDir=new File(dateDirPath); if(!dateDir.exists()){ dateDir.mkdirs(); } System.out.println("周任务:"+task); System.out.println("文件"+file); //文件上传 file.transferTo(new File(dateDir,newName)); //将文件信息放入数据库中 UserFile userFile=new UserFile(); userFile.setOldName(oldName); userFile.setNewName(newName); userFile.setPath(dateFormat); userFile.setFlag(0); userFile.setTaskId(task.getId()); userFile.setUserId(user.getId()); fileService.addFile(userFile); return "redirect:/listTask"; }代码如下:
@RequestMapping("/downloadFile") public void downloadTask(Integer taskId, HttpServletResponse response) throws IOException { UserFile file=fileService.selectFileByTaskId(taskId); String path=ResourceUtils.getURL("classpath:").getPath()+"/static/task/"+file.getPath(); //获取文件输入流 FileInputStream inputStream=new FileInputStream(new File(path,file.getNewName())); //附件下载 response.setHeader("content-disposition","attachment;fileName="+ URLEncoder.encode(file.getOldName(),"UTF-8")); //获取响应输出流 ServletOutputStream outputStream=response.getOutputStream(); IOUtils.copy(inputStream,outputStream); IOUtils.closeQuietly(inputStream); IOUtils.closeQuietly(outputStream); }代码如下:
@RequestMapping("/uploadFile") public String uploadFile(MultipartFile newFile, HttpSession session) throws IOException { Integer taskId=(Integer) session.getAttribute("taskId"); User user= (User) session.getAttribute("admin"); UserFile oldUserFile=fileService.selectFileByTaskId(taskId); //删除旧文件 String realPath= ResourceUtils.getURL("classpath:").getPath()+"/static/task"+oldUserFile.getPath(); File oldFile=new File(realPath,oldUserFile.getNewName()); System.out.println(oldFile.exists()); if(oldFile.exists()) oldFile.delete(); //删除数据库中数据 fileService.deleteFileByTaskId(taskId); //重新上传数据 //获取文件原始名称 String oldName=newFile.getOriginalFilename(); //获取文件后缀 String extension="."+ FilenameUtils.getExtension(newFile.getOriginalFilename()); //生成新文件名 String newName=new SimpleDateFormat("yyyyMMddHHmmss").format(new Date())+ UUID.randomUUID().toString().replace("-","")+extension; //根据日期生成目录 String path= ResourceUtils.getURL("classpath:").getPath()+"/static/task"; String dateFormat =new SimpleDateFormat("yyyy-MM-dd").format(new Date()); String dateDirPath=path+"/"+dateFormat; File dateDir=new File(dateDirPath); if(!dateDir.exists()){ dateDir.mkdirs(); } //文件上传 newFile.transferTo(new File(dateDir,newName)); //将文件信息放入数据库中 UserFile userFile=new UserFile(); userFile.setOldName(oldName); userFile.setNewName(newName); userFile.setPath("/"+dateFormat); userFile.setFlag(0); userFile.setTaskId(taskId); userFile.setUserId(user.getId()); fileService.addFile(userFile); return "redirect:/listTask"; }