File tree Expand file tree Collapse file tree
java/com/baeldung/produceimage
resources/com/baeldung/produceimage Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ package com .baeldung .produceimage ;
2+
3+ import org .springframework .boot .SpringApplication ;
4+ import org .springframework .boot .autoconfigure .EnableAutoConfiguration ;
5+ import org .springframework .context .annotation .ComponentScan ;
6+ import org .springframework .context .annotation .PropertySource ;
7+
8+ @ EnableAutoConfiguration
9+ @ ComponentScan ("com.baeldung.produceimage" )
10+ public class Application {
11+ public static void main (final String [] args ) {
12+ SpringApplication .run (Application .class , args );
13+ }
14+ }
Original file line number Diff line number Diff line change 1+ package com .baeldung .produceimage .controller ;
2+
3+ import org .apache .commons .io .IOUtils ;
4+ import org .springframework .http .MediaType ;
5+ import org .springframework .stereotype .Controller ;
6+ import org .springframework .web .bind .annotation .GetMapping ;
7+ import org .springframework .web .bind .annotation .ResponseBody ;
8+
9+ import java .io .IOException ;
10+ import java .io .InputStream ;
11+
12+ @ Controller
13+ public class DataProducerController {
14+
15+ @ GetMapping ("/get-text" )
16+ public @ ResponseBody String getText () {
17+ return "Hello world" ;
18+ }
19+
20+ @ GetMapping (value = "/get-image" )
21+ public @ ResponseBody byte [] getImage () throws IOException {
22+ final InputStream in = getClass ().getResourceAsStream ("/com/baeldung/produceimage/image.jpg" );
23+ return IOUtils .toByteArray (in );
24+ }
25+
26+ @ GetMapping (value = "/get-image-with-media-type" , produces = MediaType .IMAGE_JPEG_VALUE )
27+ public @ ResponseBody byte [] getImageWithMediaType () throws IOException {
28+ final InputStream in = getClass ().getResourceAsStream ("/com/baeldung/produceimage/image.jpg" );
29+ return IOUtils .toByteArray (in );
30+ }
31+
32+ @ GetMapping (value = "/get-file" , produces = MediaType .APPLICATION_OCTET_STREAM_VALUE )
33+ public @ ResponseBody byte [] getFile () throws IOException {
34+ final InputStream in = getClass ().getResourceAsStream ("/com/baeldung/produceimage/data.txt" );
35+ return IOUtils .toByteArray (in );
36+ }
37+
38+ }
Original file line number Diff line number Diff line change 1+ This is a sample file containing text data
You can’t perform that action at this time.
0 commit comments