// download arquivo @RequestMapping(value = "/{id}/download", method = RequestMethod.GET) public void getFile(@PathVariable("id") Long id, HttpServletResponse response) { try { Documento documento = serviceDocumento.find(Documento.class, id); if (documento != null) { InputStream is = new ByteArrayInputStream(documento.getArquivo()); response.setContentType(documento.getTipo()); response.setHeader( "Content-Disposition", "attachment; filename=" + documento.getNomeOriginal().replace(" ", "_")); IOUtils.copy(is, response.getOutputStream()); response.flushBuffer(); } } catch (IOException ex) { throw new RuntimeException("IOError writing file to output stream"); } }
// add arquivo @RequestMapping(method = RequestMethod.POST) public @ResponseBody Long handleFileUpload(@RequestParam("file") MultipartFile file) { log.debug("Documento - POST"); if (!file.isEmpty()) { try { if (file.getBytes() != null && file.getBytes().length != 0) { Documento documento = new Documento(); documento.setArquivo(file.getBytes()); documento.setNome(file.getOriginalFilename()); documento.setNomeOriginal(file.getOriginalFilename()); documento.setTipo(file.getContentType()); serviceDocumento.save(documento); // retornar o ID do documento return documento.getId(); } } catch (IOException ioe) { return (long) -1; } } return (long) -1; }
// excluir @RequestMapping(value = "{id}", method = RequestMethod.DELETE) public @ResponseBody ResponseStatusMessage delete(@PathVariable Long id) { log.debug("Documento - DELETE"); serviceDocumento.delete(new Documento(id)); return new ResponseStatusMessage(ResponseStatus.SUCCESS, "Documento removida com sucesso"); }
// get by id @RequestMapping(value = "{id}", method = RequestMethod.GET) public @ResponseBody Documento findById(@PathVariable Integer id) { log.debug("Documento - GET (id)"); return serviceDocumento.find(Documento.class, (long) id); }
// listar arquivos @RequestMapping(method = RequestMethod.GET) public @ResponseBody List<Documento> findAll() { log.debug("Documento - GET (all)"); return serviceDocumento.find(Documento.class); }