@Test public void headers() throws URISyntaxException { URI location = new URI("location"); long contentLength = 67890; MediaType contentType = MediaType.TEXT_PLAIN; ResponseEntity<Void> responseEntity = ResponseEntity.ok(). allow(HttpMethod.GET). lastModified(12345L). location(location). contentLength(contentLength). contentType(contentType). build(); assertNotNull(responseEntity); assertEquals(HttpStatus.OK, responseEntity.getStatusCode()); HttpHeaders responseHeaders = responseEntity.getHeaders(); assertEquals("GET", responseHeaders.getFirst("Allow")); assertEquals("Thu, 1 Jan 1970 00:00:12 GMT", responseHeaders.getFirst("Last-Modified")); assertEquals(location.toASCIIString(), responseHeaders.getFirst("Location")); assertEquals(String.valueOf(contentLength), responseHeaders.getFirst("Content-Length")); assertEquals(contentType.toString(), responseHeaders.getFirst("Content-Type")); assertNull(responseEntity.getBody()); }
.ok() .headers(headers) .contentLength(outFile.length()) .contentType(MediaType.parseMediaType("application/octet-stream")) .body(new InputStreamResource(new FileInputStream(outFile))); .ok() .headers(headers) .contentLength(error.length()) .contentType(MediaType.parseMediaType("application/octet-stream")) .body(new InputStreamResource(new FileInputStream(error)));
@Secured({"ROLE_ADMIN"}) @RequestMapping(value = "/internalapi/backup/backup", method = RequestMethod.GET, produces = MediaType.APPLICATION_OCTET_STREAM_VALUE) @Transactional public Object backupAndDownload() throws Exception { try { File backupFile = backup.backup(); logger.debug("Sending contents of file {}", backupFile.getAbsolutePath()); return ResponseEntity .ok() .header(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=" + backupFile.getName()) .contentLength(backupFile.length()) .body(new FileSystemResource(backupFile)); } catch (Exception e) { logger.error("Error while creating backup", e); return GenericResponse.notOk(e.getMessage()); } }
.contentLength(bytes.length) // .body(resource);
return ResponseEntity.ok().contentLength(result.getContent().length) .header("content-disposition", "attachment; filename = " + result.getFileName()) .contentType(MediaType.parseMediaType(result.getMediatype()))
@RequestMapping(value = "/download", method = RequestMethod.GET) @ApiOperation(value = "文件下載", httpMethod = "GET", response = byte.class) @ApiImplicitParams({ @ApiImplicitParam(name = "fileName", value = "文件名称", paramType = "query"), @ApiImplicitParam(name = "fileType", value = "文件类型", paramType = "query") }) public ResponseEntity<InputStreamResource> downloadFile(String fileName, String fileType) throws IOException { String filePath = fileName+"."+fileType; ClassPathResource file = new ClassPathResource("down/"+filePath); HttpHeaders headers = new HttpHeaders(); headers.add("Cache-Control", "no-cache, no-store, must-revalidate"); headers.add("Content-Disposition", String.format("attachment; filename=\"%s\"", file.getFilename())); headers.add("Pragma", "no-cache"); headers.add("Expires", "0"); return ResponseEntity .ok() .headers(headers) .contentLength(file.contentLength()) .contentType(MediaType.parseMediaType("application/octet-stream")) .body(new InputStreamResource(file.getInputStream())); } }
/** * Download a temporary file which is not yet commited (uploaded or modified through an operation). * * @param topologyId The if of the topology. * @param artifactId The id of the temporary artifact. * @return The response entity with the input stream of the file. */ @ApiIgnore @PreAuthorize("isAuthenticated()") @RequestMapping(value = "/{topologyId:.+}/file/{artifactId:.+}", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE) public ResponseEntity<InputStreamResource> downloadTempFile(@PathVariable String topologyId, @PathVariable String artifactId) { editorService.checkAuthorization(topologyId); HttpHeaders headers = new HttpHeaders(); headers.add("Cache-Control", "no-cache, no-store, must-revalidate"); headers.add("Pragma", "no-cache"); headers.add("Expires", "0"); long length = artifactRepository.getFileLength(artifactId); return ResponseEntity.ok().headers(headers).contentLength(length).contentType(MediaType.parseMediaType("application/octet-stream")) .body(new InputStreamResource(artifactRepository.getFile(artifactId))); }
/******************************************************************************************************************* * ******************************************************************************************************************/ @Nonnull private ResponseEntity<byte[]> bytesResponse (final @Nonnull byte[] bytes, final @Nonnull String type, final @Nonnull String subtype, final @Nonnull String contentDisposition) { return ResponseEntity.ok() .contentType(new MediaType(type, subtype)) .contentLength(bytes.length) .header(CONTENT_DISPOSITION, contentDisposition(contentDisposition)) .body(bytes); }
private ResponseEntity<Resource> response(FilePointer filePointer, HttpStatus status, Resource body) { final ResponseEntity.BodyBuilder responseBuilder = ResponseEntity .status(status) .eTag(filePointer.getEtag()) .contentLength(filePointer.getSize()) .lastModified(filePointer.getLastModified().toEpochMilli()); filePointer .getMediaType() .map(this::toMediaType) .ifPresent(responseBuilder::contentType); return responseBuilder.body(body); }
protected ResponseEntity<ByteArrayResource> loadBytesForWeb(DSDocument dsDocument) { byte[] data = dsDocument.getDocumentContent().getValue(); return ResponseEntity.ok() // Content-Type .contentType(MediaType.APPLICATION_JSON_UTF8) // // Content-Lengh .contentLength(data.length) // .body(new ByteArrayResource(data)); }
public static ResponseEntity<InputStreamResource> responseEntity(HttpHeaders rtHeaders, long contentLength, String contentType, InputStream input) { MediaType parseMediaType = MediaType.parseMediaType(contentType); // 数据流 return ResponseEntity.ok().headers(rtHeaders).contentLength(contentLength).contentType(parseMediaType) .body(new InputStreamResource(input)); }
protected ResponseEntity<ByteArrayResource> loadBytesForWeb(DSDocument dsDocument, MediaType mediaType) { byte[] data = dsDocument.getDocumentContent().getValue(); return ResponseEntity.ok() // Content-Type .contentType(mediaType) // // Content-Lengh .contentLength(data.length) // .body(new ByteArrayResource(data)); }
protected ResponseEntity<ByteArrayResource> loadBytesForWeb(DSDocument dsDocument, MediaType mediaType) { byte[] data = dsDocument.getDocumentContent().getValue(); return ResponseEntity.ok() // Content-Type .contentType(mediaType) // // Content-Lengh .contentLength(data.length) // .body(new ByteArrayResource(data)); }
@ApiOperation(value = "Download a CSAR given its id.", notes = "Returns zipped content of a CSAR.") @RequestMapping(value = "/{csarId:.+?}/download", method = RequestMethod.GET, produces = MediaType.APPLICATION_OCTET_STREAM_VALUE) @PreAuthorize("isAuthenticated()") @SneakyThrows public ResponseEntity<InputStreamResource> download(@PathVariable String csarId) { Csar csar = csarService.getOrFail(csarId); csarAuthorizationFilter.checkReadAccess(csar); Path csarToDownload = archiveRepositry.getCSAR(csar.getName(), csar.getVersion()); return ResponseEntity.ok().contentLength(csarToDownload.toFile().length()).contentType(MediaType.APPLICATION_OCTET_STREAM) .body(new InputStreamResource(Files.newInputStream(csarToDownload))); }
protected ResponseEntity<ByteArrayResource> loadBytesForWeb(DSDocument dsDocument) { byte[] data = dsDocument.getDocumentContent().getValue(); return ResponseEntity.ok() // Content-Type .contentType(MediaType.APPLICATION_JSON_UTF8) // // Content-Lengh .contentLength(data.length) // .body(new ByteArrayResource(data)); }