Skip to content

Commit bd237b2

Browse files
XealRedanpivovarit
authored andcommitted
master (eugenp#1455)
* Example code to return image with @responsebody * Example code showing how to return images using @responsebody * Example code showing how to return images using @responsebody
1 parent eab4a5f commit bd237b2

4 files changed

Lines changed: 53 additions & 0 deletions

File tree

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
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+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
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+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
This is a sample file containing text data
6.03 KB
Loading

0 commit comments

Comments
 (0)