File src/main/java/com/darren/tommserver/controller/ImageController.java added (mode: 100644) (index 0000000..a92aed2) |
|
1 |
|
package com.darren.tommserver.controller; |
|
2 |
|
|
|
3 |
|
import javax.imageio.ImageIO; |
|
4 |
|
import java.awt.image.BufferedImage; |
|
5 |
|
import java.io.*; |
|
6 |
|
|
|
7 |
|
import org.apache.commons.io.IOUtils; |
|
8 |
|
import org.springframework.http.HttpHeaders; |
|
9 |
|
import org.springframework.http.HttpStatus; |
|
10 |
|
import org.springframework.http.MediaType; |
|
11 |
|
import org.springframework.http.ResponseEntity; |
|
12 |
|
import org.springframework.util.ResourceUtils; |
|
13 |
|
import org.springframework.web.bind.annotation.*; |
|
14 |
|
|
|
15 |
|
/** |
|
16 |
|
* 图片 控制层 |
|
17 |
|
* |
|
18 |
|
* @author Darren |
|
19 |
|
*/ |
|
20 |
|
@RestController |
|
21 |
|
@RequestMapping("image") |
|
22 |
|
public class ImageController { |
|
23 |
|
/** |
|
24 |
|
* 获取图片总数 |
|
25 |
|
* |
|
26 |
|
* @return 图片总数 |
|
27 |
|
*/ |
|
28 |
|
@GetMapping(value = "count") |
|
29 |
|
public ResponseEntity<?> getImageCount() { |
|
30 |
|
|
|
31 |
|
return new ResponseEntity<>(1, HttpStatus.OK); |
|
32 |
|
} |
|
33 |
|
|
|
34 |
|
/** |
|
35 |
|
* 获取图片 |
|
36 |
|
* |
|
37 |
|
* @param imageName 图片名 |
|
38 |
|
* @return 图片 |
|
39 |
|
*/ |
|
40 |
|
@RequestMapping(value = "i/{image_name}", produces = MediaType.IMAGE_PNG_VALUE) |
|
41 |
|
public byte[] getImage(@PathVariable("image_name") String imageName) { |
|
42 |
|
|
|
43 |
|
byte[] imageContent = new byte[0]; |
|
44 |
|
try { |
|
45 |
|
// C:/wl_doc/IDEAWorkspace/Repos/ToMmServer/src/main/resources/upload |
|
46 |
|
// /home/web/caddy/filebrowser/root/mm/img |
|
47 |
|
// String path = ResourceUtils.getURL("classpath:upload/").getPath() + imageName; |
|
48 |
|
File file = ResourceUtils.getFile(ResourceUtils.CLASSPATH_URL_PREFIX + "upload/" + imageName); |
|
49 |
|
FileInputStream inputStream = new FileInputStream(file); |
|
50 |
|
|
|
51 |
|
byte[] bytes; |
|
52 |
|
bytes = new byte[inputStream.available()]; |
|
53 |
|
inputStream.read(bytes, 0, inputStream.available()); |
|
54 |
|
imageContent = bytes; |
|
55 |
|
IOUtils.toByteArray(inputStream); |
|
56 |
|
} catch (IOException e) { |
|
57 |
|
e.printStackTrace(); |
|
58 |
|
} |
|
59 |
|
final HttpHeaders headers = new HttpHeaders(); |
|
60 |
|
headers.setContentType(MediaType.IMAGE_PNG); |
|
61 |
|
// return new ResponseEntity<>(imageContent, headers, HttpStatus.OK); |
|
62 |
|
|
|
63 |
|
return imageContent; |
|
64 |
|
} |
|
65 |
|
|
|
66 |
|
|
|
67 |
|
/** |
|
68 |
|
* 获取Vps图片 |
|
69 |
|
* |
|
70 |
|
* @param imageName 图片名 |
|
71 |
|
* @return 图片 |
|
72 |
|
*/ |
|
73 |
|
@RequestMapping(value = "r/{image_name}", produces = MediaType.IMAGE_PNG_VALUE) |
|
74 |
|
public byte[] getVpsImage(@PathVariable("image_name") String imageName) { |
|
75 |
|
|
|
76 |
|
byte[] imageContent = new byte[0]; |
|
77 |
|
try { |
|
78 |
|
// C:/wl_doc/IDEAWorkspace/Repos/ToMmServer/src/main/resources/upload |
|
79 |
|
// /home/web/caddy/filebrowser/root/mm/img |
|
80 |
|
// String path = ResourceUtils.getURL("classpath:upload/").getPath() + imageName; |
|
81 |
|
// File file = ResourceUtils.getFile(ResourceUtils.CLASSPATH_URL_PREFIX + "upload/" + imageName); |
|
82 |
|
File file = ResourceUtils.getFile("/home/web/caddy/filebrowser/root/mm/img/" + imageName); |
|
83 |
|
FileInputStream inputStream = new FileInputStream(file); |
|
84 |
|
|
|
85 |
|
byte[] bytes; |
|
86 |
|
bytes = new byte[inputStream.available()]; |
|
87 |
|
inputStream.read(bytes, 0, inputStream.available()); |
|
88 |
|
imageContent = bytes; |
|
89 |
|
IOUtils.toByteArray(inputStream); |
|
90 |
|
} catch (IOException e) { |
|
91 |
|
e.printStackTrace(); |
|
92 |
|
} |
|
93 |
|
final HttpHeaders headers = new HttpHeaders(); |
|
94 |
|
headers.setContentType(MediaType.IMAGE_PNG); |
|
95 |
|
// return new ResponseEntity<>(imageContent, headers, HttpStatus.OK); |
|
96 |
|
|
|
97 |
|
return imageContent; |
|
98 |
|
} |
|
99 |
|
|
|
100 |
|
/** |
|
101 |
|
* 图片转字节 |
|
102 |
|
* |
|
103 |
|
* @param img 图片文件 |
|
104 |
|
* @return 字节 |
|
105 |
|
*/ |
|
106 |
|
private static byte[] fileToByte(File img) { |
|
107 |
|
// imageContent = fileToByte(new File(path1)); |
|
108 |
|
byte[] bytes = null; |
|
109 |
|
try (ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream()) { |
|
110 |
|
BufferedImage bi = ImageIO.read(img); |
|
111 |
|
ImageIO.write(bi, "png", byteArrayOutputStream); |
|
112 |
|
bytes = byteArrayOutputStream.toByteArray(); |
|
113 |
|
} catch (Exception e) { |
|
114 |
|
e.printStackTrace(); |
|
115 |
|
} |
|
116 |
|
return bytes; |
|
117 |
|
} |
|
118 |
|
} |
|
119 |
|
|