@RequestMapping(value = "hunter/addressbooks/upload", method = RequestMethod.POST) public Object upload( @RequestParam Long id, @RequestParam String companyName, @RequestParam(required = false) MultipartFile file) { Addressbook book = new Addressbook(); book.setId(id); String fileName = file.getOriginalFilename(); book.setAttachmentName(fileName); String pathname = path + companyName + "/"; book.setAttachmentPath(pathname); try { File directory = new File(pathname + fileName); if (!directory.exists()) { directory.mkdirs(); } file.transferTo(directory); } catch (Exception e) { throw new RuntimeException(e); } addressbookService.update(book); return true; }
@RequestMapping(value = "hunter/addressbooks/attachment/{id}", method = RequestMethod.GET) public Object getAttachments(@PathVariable Long id, @RequestParam String filename) { Addressbook book = addressbookService.getAttachment(id); int times = book.getDownloadtimes(); String today = DateFormatUtils.format(new Date(), "yyyy-MM-dd"); boolean isToday = today.equals(book.getDownloaddate()); if (isToday && times == 10) { try { String msg = new String("此目标公司通讯录今天下载次数已达10次,不能继续下载".getBytes("UTF-8"), "iso-8859-1"); return new ResponseEntity<>(msg, HttpStatus.OK); } catch (UnsupportedEncodingException e) { throw new RuntimeException(e); } } boolean downloadStatus = false; Map<String, Object> map = new HashMap<String, Object>(); map.put("id", id); while (!isToday || (isToday && times <= 10)) { map.put("downloaddate", book.getDownloaddate()); map.put("downloadtimes", times); if (!isToday) { map.put("newdate", today); map.put("newtimes", 1); } else { map.put("newtimes", times + 1); } if (addressbookService.updateDownloadtimes(map) > 0) { downloadStatus = true; break; } book = addressbookService.getAttachment(id); times = book.getDownloadtimes(); isToday = today.equals(book.getDownloaddate()); } if (!downloadStatus) { try { String msg = new String("此目标公司通讯录今天下载次数已达10次,不能继续下载".getBytes("UTF-8"), "iso-8859-1"); return new ResponseEntity<>(msg, HttpStatus.OK); } catch (UnsupportedEncodingException e) { throw new RuntimeException(e); } } addressbookService.insertDownloadLog(id, filename, book.getCompanyName()); String pathname = book.getAttachmentPath() + filename; File file = new File(pathname); HttpHeaders headers = new HttpHeaders(); try { String fileName = new String(filename.getBytes("UTF-8"), "iso-8859-1"); // 为了解决中文名称乱码问题 headers.setContentDispositionFormData("attachment", fileName); headers.setContentType(MediaType.APPLICATION_OCTET_STREAM); return new ResponseEntity<byte[]>( FileUtils.readFileToByteArray(file), headers, HttpStatus.OK); } catch (IOException e) { throw new RuntimeException(e); } }
@RequestMapping(value = "hunter/addressbooks/{id}", method = RequestMethod.PUT) public Object update(@PathVariable Long id, @RequestBody Addressbook book) { book.setId(id); addressbookService.update(book); return true; }