org.springframework.core.io.InputStreamResource Java Examples
The following examples show how to use
org.springframework.core.io.InputStreamResource.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: UploadController.java From Learning-Spring-Boot-2.0-Second-Edition with MIT License | 6 votes |
@GetMapping(value = BASE_PATH + "/" + FILENAME + "/raw", produces = MediaType.IMAGE_JPEG_VALUE) @ResponseBody public Mono<ResponseEntity<?>> oneRawImage( @PathVariable String filename) { // tag::try-catch[] return imageService.findOneImage(filename) .map(resource -> { try { return ResponseEntity.ok() .contentLength(resource.contentLength()) .body(new InputStreamResource( resource.getInputStream())); } catch (IOException e) { return ResponseEntity.badRequest() .body("Couldn't find " + filename + " => " + e.getMessage()); } }); // end::try-catch[] }
Example #2
Source File: HttpEntityMethodProcessorMockTests.java From spring-analysis-note with MIT License | 6 votes |
@Test //SPR-16754 public void disableRangeSupportForStreamingResponses() throws Exception { InputStream is = new ByteArrayInputStream("Content".getBytes(StandardCharsets.UTF_8)); InputStreamResource resource = new InputStreamResource(is, "test"); ResponseEntity<Resource> returnValue = ResponseEntity.ok(resource); servletRequest.addHeader("Range", "bytes=0-5"); given(resourceMessageConverter.canWrite(any(), eq(null))).willReturn(true); given(resourceMessageConverter.canWrite(any(), eq(APPLICATION_OCTET_STREAM))).willReturn(true); processor.handleReturnValue(returnValue, returnTypeResponseEntityResource, mavContainer, webRequest); then(resourceMessageConverter).should(times(1)).write( any(InputStreamResource.class), eq(APPLICATION_OCTET_STREAM), any(HttpOutputMessage.class)); assertEquals(200, servletResponse.getStatus()); assertThat(servletResponse.getHeader(HttpHeaders.ACCEPT_RANGES), Matchers.isEmptyOrNullString()); }
Example #3
Source File: VersionsFetcher.java From spring-cloud-release-tools with Apache License 2.0 | 6 votes |
InitializrProperties toProperties(String url) { return CACHE.computeIfAbsent(url, s -> { String retrievedFile = this.rawGithubRetriever.raw(s); if (StringUtils.isEmpty(retrievedFile)) { return null; } YamlPropertiesFactoryBean yamlProcessor = new YamlPropertiesFactoryBean(); yamlProcessor.setResources(new InputStreamResource(new ByteArrayInputStream( retrievedFile.getBytes(StandardCharsets.UTF_8)))); Properties properties = yamlProcessor.getObject(); return new Binder( new MapConfigurationPropertySource(properties.entrySet().stream() .collect(Collectors.toMap(e -> e.getKey().toString(), e -> e.getValue().toString())))) .bind("initializr", InitializrProperties.class) .get(); }); }
Example #4
Source File: HomeController.java From Learning-Spring-Boot-2.0-Second-Edition with MIT License | 6 votes |
@GetMapping(value = BASE_PATH + "/" + FILENAME + "/raw", produces = MediaType.IMAGE_JPEG_VALUE) @ResponseBody public Mono<ResponseEntity<?>> oneRawImage( @PathVariable String filename) { // tag::try-catch[] return imageService.findOneImage(filename) .map(resource -> { try { return ResponseEntity.ok() .contentLength(resource.contentLength()) .body(new InputStreamResource( resource.getInputStream())); } catch (IOException e) { return ResponseEntity.badRequest() .body("Couldn't find " + filename + " => " + e.getMessage()); } }); // end::try-catch[] }
Example #5
Source File: HomeController.java From Learning-Spring-Boot-2.0-Second-Edition with MIT License | 6 votes |
@GetMapping(value = BASE_PATH + "/" + FILENAME + "/raw", produces = MediaType.IMAGE_JPEG_VALUE) @ResponseBody public Mono<ResponseEntity<?>> oneRawImage( @PathVariable String filename) { // tag::try-catch[] return imageService.findOneImage(filename) .map(resource -> { try { return ResponseEntity.ok() .contentLength(resource.contentLength()) .body(new InputStreamResource( resource.getInputStream())); } catch (IOException e) { return ResponseEntity.badRequest() .body("Couldn't find " + filename + " => " + e.getMessage()); } }); // end::try-catch[] }
Example #6
Source File: UploadController.java From Learning-Spring-Boot-2.0-Second-Edition with MIT License | 6 votes |
@GetMapping(value = BASE_PATH + "/" + FILENAME + "/raw", produces = MediaType.IMAGE_JPEG_VALUE) @ResponseBody public Mono<ResponseEntity<?>> oneRawImage( @PathVariable String filename) { // tag::try-catch[] return imageService.findOneImage(filename) .map(resource -> { try { return ResponseEntity.ok() .contentLength(resource.contentLength()) .body(new InputStreamResource( resource.getInputStream())); } catch (IOException e) { return ResponseEntity.badRequest() .body("Couldn't find " + filename + " => " + e.getMessage()); } }); // end::try-catch[] }
Example #7
Source File: UploadController.java From Learning-Spring-Boot-2.0-Second-Edition with MIT License | 6 votes |
@GetMapping(value = BASE_PATH + "/" + FILENAME + "/raw", produces = MediaType.IMAGE_JPEG_VALUE) @ResponseBody public Mono<ResponseEntity<?>> oneRawImage( @PathVariable String filename) { // tag::try-catch[] return imageService.findOneImage(filename) .map(resource -> { try { return ResponseEntity.ok() .contentLength(resource.contentLength()) .body(new InputStreamResource( resource.getInputStream())); } catch (IOException e) { return ResponseEntity.badRequest() .body("Couldn't find " + filename + " => " + e.getMessage()); } }); // end::try-catch[] }
Example #8
Source File: HomeController.java From Learning-Spring-Boot-2.0-Second-Edition with MIT License | 6 votes |
@GetMapping(value = BASE_PATH + "/" + FILENAME + "/raw", produces = MediaType.IMAGE_JPEG_VALUE) @ResponseBody public Mono<ResponseEntity<?>> oneRawImage( @PathVariable String filename) { // tag::try-catch[] return imageService.findOneImage(filename) .map(resource -> { try { return ResponseEntity.ok() .contentLength(resource.contentLength()) .body(new InputStreamResource( resource.getInputStream())); } catch (IOException e) { return ResponseEntity.badRequest() .body("Couldn't find " + filename + " => " + e.getMessage()); } }); // end::try-catch[] }
Example #9
Source File: IndexController.java From nbp with Apache License 2.0 | 6 votes |
/** * get the ngc zip file * @param fileName * @return * @throws IOException */ @RequestMapping(value="/download/{fileName}", method= RequestMethod.GET) public ResponseEntity<InputStreamResource> downloadFile (@PathVariable("fileName") String fileName) throws IOException { logger.info(String.format("-----------------Begin download the zip file %s-----------------", fileName)); if (!vCenterService.isValidZipFileName(fileName)) { return ResponseEntity.notFound().build(); } MediaType mediaType = MediaTypeUtils.getMediaTypeForFileName(this.servletContext, fileName); File file = new File(vCenterService.getZipfilePath()); InputStreamResource resource = new InputStreamResource(new FileInputStream(file)); return ResponseEntity.ok() .header(HttpHeaders.CONTENT_DISPOSITION, "attachment;filename=" + file.getName()) // Content-Type .contentType(mediaType) // Content-Length .contentLength(file.length()) .body(resource); }
Example #10
Source File: UploadController.java From Learning-Spring-Boot-2.0-Second-Edition with MIT License | 6 votes |
@GetMapping(value = BASE_PATH + "/" + FILENAME + "/raw", produces = MediaType.IMAGE_JPEG_VALUE) @ResponseBody public Mono<ResponseEntity<?>> oneRawImage( @PathVariable String filename) { // tag::try-catch[] return imageService.findOneImage(filename) .map(resource -> { try { return ResponseEntity.ok() .contentLength(resource.contentLength()) .body(new InputStreamResource( resource.getInputStream())); } catch (IOException e) { return ResponseEntity.badRequest() .body("Couldn't find " + filename + " => " + e.getMessage()); } }); // end::try-catch[] }
Example #11
Source File: HomeController.java From Learning-Spring-Boot-2.0-Second-Edition with MIT License | 6 votes |
@GetMapping(value = BASE_PATH + "/" + FILENAME + "/raw", produces = MediaType.IMAGE_JPEG_VALUE) @ResponseBody public Mono<ResponseEntity<?>> oneRawImage( @PathVariable String filename) { // tag::try-catch[] return imageService.findOneImage(filename) .map(resource -> { try { return ResponseEntity.ok() .contentLength(resource.contentLength()) .body(new InputStreamResource( resource.getInputStream())); } catch (IOException e) { return ResponseEntity.badRequest() .body("Couldn't find " + filename + " => " + e.getMessage()); } }); // end::try-catch[] }
Example #12
Source File: HomeController.java From Learning-Spring-Boot-2.0-Second-Edition with MIT License | 6 votes |
@GetMapping(value = BASE_PATH + "/" + FILENAME + "/raw", produces = MediaType.IMAGE_JPEG_VALUE) @ResponseBody public Mono<ResponseEntity<?>> oneRawImage( @PathVariable String filename) { return imageService.findOneImage(filename) .map(resource -> { try { return ResponseEntity.ok() .contentLength(resource.contentLength()) .body(new InputStreamResource( resource.getInputStream())); } catch (IOException e) { return ResponseEntity.badRequest() .body("Couldn't find " + filename + " => " + e.getMessage()); } }); }
Example #13
Source File: HomeController.java From Learning-Spring-Boot-2.0-Second-Edition with MIT License | 6 votes |
@GetMapping(value = BASE_PATH + "/" + FILENAME + "/raw", produces = MediaType.IMAGE_JPEG_VALUE) @ResponseBody public Mono<ResponseEntity<?>> oneRawImage( @PathVariable String filename) { return imageService.findOneImage(filename) .map(resource -> { try { return ResponseEntity.ok() .contentLength(resource.contentLength()) .body(new InputStreamResource( resource.getInputStream())); } catch (IOException e) { return ResponseEntity.badRequest() .body("Couldn't find " + filename + " => " + e.getMessage()); } }); }
Example #14
Source File: UploadedImageController.java From zhcet-web with Apache License 2.0 | 6 votes |
@ResponseBody @GetMapping("/image:view/{image:.+}") public ResponseEntity<InputStreamResource> serveImage(@PathVariable UploadedImage image) { if (image == null) { log.warn("Trying to view non existent image"); return ResponseEntity.notFound().build(); } File file = systemStorageService.load(FileType.IMAGE, image.getFilename()).toFile(); if (!file.exists()) { log.warn("Trying to view non existent image {}", image); return ResponseEntity.notFound().build(); } try { return ResponseEntity.ok() .contentType(MediaType.parseMediaType(image.getContentType())) .contentLength(file.length()) .body(new InputStreamResource(new FileInputStream(file))); } catch (FileNotFoundException e) { return ResponseEntity.noContent().build(); } }
Example #15
Source File: HomeController.java From Learning-Spring-Boot-2.0-Second-Edition with MIT License | 6 votes |
@GetMapping(value = BASE_PATH + "/" + FILENAME + "/raw", produces = MediaType.IMAGE_JPEG_VALUE) @ResponseBody public Mono<ResponseEntity<?>> oneRawImage( @PathVariable String filename) { return imageService.findOneImage(filename) .map(resource -> { try { return ResponseEntity.ok() .contentLength(resource.contentLength()) .body(new InputStreamResource( resource.getInputStream())); } catch (IOException e) { return ResponseEntity.badRequest() .body("Couldn't find " + filename + " => " + e.getMessage()); } }); }
Example #16
Source File: UploadController.java From Learning-Spring-Boot-2.0-Second-Edition with MIT License | 6 votes |
@GetMapping(value = BASE_PATH + "/" + FILENAME + "/raw", produces = MediaType.IMAGE_JPEG_VALUE) @ResponseBody public Mono<ResponseEntity<?>> oneRawImage( @PathVariable String filename) { // tag::try-catch[] return imageService.findOneImage(filename) .map(resource -> { try { return ResponseEntity.ok() .contentLength(resource.contentLength()) .body(new InputStreamResource( resource.getInputStream())); } catch (IOException e) { return ResponseEntity.badRequest() .body("Couldn't find " + filename + " => " + e.getMessage()); } }); // end::try-catch[] }
Example #17
Source File: HttpRange.java From lams with GNU General Public License v2.0 | 6 votes |
/** * Turn a {@code Resource} into a {@link ResourceRegion} using the range * information contained in the current {@code HttpRange}. * @param resource the {@code Resource} to select the region from * @return the selected region of the given {@code Resource} * @since 4.3 */ public ResourceRegion toResourceRegion(Resource resource) { // Don't try to determine contentLength on InputStreamResource - cannot be read afterwards... // Note: custom InputStreamResource subclasses could provide a pre-calculated content length! Assert.isTrue(resource.getClass() != InputStreamResource.class, "Cannot convert an InputStreamResource to a ResourceRegion"); try { long contentLength = resource.contentLength(); Assert.isTrue(contentLength > 0, "Resource content length should be > 0"); long start = getRangeStart(contentLength); long end = getRangeEnd(contentLength); return new ResourceRegion(resource, start, end - start + 1); } catch (IOException ex) { throw new IllegalArgumentException("Failed to convert Resource to ResourceRegion", ex); } }
Example #18
Source File: UploadController.java From Learning-Spring-Boot-2.0-Second-Edition with MIT License | 6 votes |
@GetMapping(value = BASE_PATH + "/" + FILENAME + "/raw", produces = MediaType.IMAGE_JPEG_VALUE) @ResponseBody public Mono<ResponseEntity<?>> oneRawImage( @PathVariable String filename) { // tag::try-catch[] return imageService.findOneImage(filename) .map(resource -> { try { return ResponseEntity.ok() .contentLength(resource.contentLength()) .body(new InputStreamResource( resource.getInputStream())); } catch (IOException e) { return ResponseEntity.badRequest() .body("Couldn't find " + filename + " => " + e.getMessage()); } }); // end::try-catch[] }
Example #19
Source File: ResourceHttpMessageConverter.java From java-technology-stack with MIT License | 6 votes |
@Override protected Resource readInternal(Class<? extends Resource> clazz, HttpInputMessage inputMessage) throws IOException, HttpMessageNotReadableException { if (this.supportsReadStreaming && InputStreamResource.class == clazz) { return new InputStreamResource(inputMessage.getBody()) { @Override public String getFilename() { return inputMessage.getHeaders().getContentDisposition().getFilename(); } }; } else if (Resource.class == clazz || ByteArrayResource.class.isAssignableFrom(clazz)) { byte[] body = StreamUtils.copyToByteArray(inputMessage.getBody()); return new ByteArrayResource(body) { @Override @Nullable public String getFilename() { return inputMessage.getHeaders().getContentDisposition().getFilename(); } }; } else { throw new HttpMessageNotReadableException("Unsupported resource class: " + clazz, inputMessage); } }
Example #20
Source File: HomeController.java From Learning-Spring-Boot-2.0-Second-Edition with MIT License | 6 votes |
@GetMapping(value = BASE_PATH + "/" + FILENAME + "/raw", produces = MediaType.IMAGE_JPEG_VALUE) @ResponseBody public Mono<ResponseEntity<?>> oneRawImage( @PathVariable String filename) { return imageService.findOneImage(filename) .map(resource -> { try { return ResponseEntity.ok() .contentLength(resource.contentLength()) .body(new InputStreamResource( resource.getInputStream())); } catch (IOException e) { return ResponseEntity.badRequest() .body("Couldn't find " + filename + " => " + e.getMessage()); } }); }
Example #21
Source File: TeiidRSProvider.java From teiid-spring-boot with Apache License 2.0 | 5 votes |
public ResponseEntity<InputStreamResource> executeQuery(final String sql, boolean json, final boolean passthroughAuth) throws SQLException { Connection conn = null; try { conn = getConnection(); Statement statement = conn.createStatement(); final boolean hasResultSet = statement.execute(sql); Object result = null; if (hasResultSet) { ResultSet rs = statement.getResultSet(); if (rs.next()) { result = rs.getObject(1); } else { throw new ResponseStatusException(HttpStatus.BAD_REQUEST,"Only result producing procedures are allowed"); } } InputStream is = handleResult(Charset.defaultCharset().name(), result); InputStreamResource inputStreamResource = new InputStreamResource(is); HttpHeaders httpHeaders = new HttpHeaders(); return new ResponseEntity<InputStreamResource>(inputStreamResource, httpHeaders, HttpStatus.OK); } finally { try { if (conn != null) { conn.close(); } } catch (SQLException e) { } } }
Example #22
Source File: LoggingServiceImpl.java From flow-platform-x with Apache License 2.0 | 5 votes |
@Override public Resource get(String cmdId) { try { String fileName = getLogFile(cmdId); InputStream stream = fileManager.read(fileName, getLogDir(cmdId)); return new InputStreamResource(stream); } catch (IOException e) { throw new NotFoundException("Log not available"); } }
Example #23
Source File: JobController.java From flow-platform-x with Apache License 2.0 | 5 votes |
@GetMapping(value = "/{flow}/{buildNumber}/artifacts/{artifactId}") @Action(JobAction.DOWNLOAD_ARTIFACT) public ResponseEntity<Resource> downloadArtifact(@PathVariable String flow, @PathVariable String buildNumber, @PathVariable String artifactId) { Job job = get(flow, buildNumber); JobArtifact artifact = artifactService.fetch(job, artifactId); return ResponseEntity.ok() .contentType(MediaType.parseMediaType(MediaType.APPLICATION_OCTET_STREAM_VALUE)) .header(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=\"" + artifact.getFileName() + "\"") .body(new InputStreamResource(artifact.getSrc())); }
Example #24
Source File: FileController.java From api-layer with Eclipse Public License 2.0 | 5 votes |
@GetMapping(value = "/api/v1/get-file", produces = "image/png") public ResponseEntity<InputStreamResource> downloadImage() { String fileName = "api-catalog.png"; InputStream inputStream = getClass().getClassLoader().getResourceAsStream(fileName); InputStreamResource resource = new InputStreamResource(inputStream); String mineType = servletContext.getMimeType(fileName); MediaType mediaType = MediaType.parseMediaType(mineType); return ResponseEntity.ok() .contentType(mediaType) .header(HttpHeaders.CONTENT_DISPOSITION, "attachment;filename=" + fileName) .body(resource); }
Example #25
Source File: ResourceHttpMessageConverterTests.java From java-technology-stack with MIT License | 5 votes |
@Test // SPR-13443 public void shouldReadInputStreamResource() throws IOException { try (InputStream body = getClass().getResourceAsStream("logo.jpg") ) { MockHttpInputMessage inputMessage = new MockHttpInputMessage(body); inputMessage.getHeaders().setContentType(MediaType.IMAGE_JPEG); inputMessage.getHeaders().setContentDisposition( ContentDisposition.builder("attachment").filename("yourlogo.jpg").build()); Resource actualResource = converter.read(InputStreamResource.class, inputMessage); assertThat(actualResource, instanceOf(InputStreamResource.class)); assertThat(actualResource.getInputStream(), is(body)); assertEquals("yourlogo.jpg", actualResource.getFilename()); } }
Example #26
Source File: HttpRange.java From java-technology-stack with MIT License | 5 votes |
/** * Turn a {@code Resource} into a {@link ResourceRegion} using the range * information contained in the current {@code HttpRange}. * @param resource the {@code Resource} to select the region from * @return the selected region of the given {@code Resource} * @since 4.3 */ public ResourceRegion toResourceRegion(Resource resource) { // Don't try to determine contentLength on InputStreamResource - cannot be read afterwards... // Note: custom InputStreamResource subclasses could provide a pre-calculated content length! Assert.isTrue(resource.getClass() != InputStreamResource.class, "Cannot convert an InputStreamResource to a ResourceRegion"); long contentLength = getLengthFor(resource); long start = getRangeStart(contentLength); long end = getRangeEnd(contentLength); return new ResourceRegion(resource, start, end - start + 1); }
Example #27
Source File: ResourceHttpMessageWriter.java From java-technology-stack with MIT License | 5 votes |
private static long lengthOf(Resource resource) { // Don't consume InputStream... if (InputStreamResource.class != resource.getClass()) { try { return resource.contentLength(); } catch (IOException ignored) { } } return -1; }
Example #28
Source File: ResourceHttpMessageConverter.java From java-technology-stack with MIT License | 5 votes |
@Override protected Long getContentLength(Resource resource, @Nullable MediaType contentType) throws IOException { // Don't try to determine contentLength on InputStreamResource - cannot be read afterwards... // Note: custom InputStreamResource subclasses could provide a pre-calculated content length! if (InputStreamResource.class == resource.getClass()) { return null; } long contentLength = resource.contentLength(); return (contentLength < 0 ? null : contentLength); }
Example #29
Source File: CustomConnectorITCase.java From syndesis with Apache License 2.0 | 5 votes |
private static MultiValueMap<String, Object> multipartBody(final ConnectorSettings connectorSettings, final InputStream icon, final InputStream specification) { final MultiValueMap<String, Object> multipartData = multipartBody(connectorSettings, icon); multipartData.add("specification", new InputStreamResource(specification)); return multipartData; }
Example #30
Source File: ResourceHttpMessageConverter.java From lams with GNU General Public License v2.0 | 5 votes |
@Override protected Resource readInternal(Class<? extends Resource> clazz, HttpInputMessage inputMessage) throws IOException, HttpMessageNotReadableException { if (InputStreamResource.class == clazz) { return new InputStreamResource(inputMessage.getBody()); } else if (clazz.isAssignableFrom(ByteArrayResource.class)) { byte[] body = StreamUtils.copyToByteArray(inputMessage.getBody()); return new ByteArrayResource(body); } else { throw new IllegalStateException("Unsupported resource class: " + clazz); } }